정리

정리 (Elasticsearch)

Velody 2020. 5. 26. 14:03

설정관련

MAC 기준 Elasticsearch 설정파일 경로 

 

Data:    /usr/local/var/lib/elasticsearch/
Logs:    /usr/local/var/log/elasticsearch/elasticsearch_yoonjongsung.log
Plugins: /usr/local/var/elasticsearch/plugins/
Config:  /usr/local/etc/elasticsearch/

 

MAC 기준 Kibana 설정파일 경로 

Home : /usr/local/var/homebrew/linked/kibana-full
Bin : /usr/local/var/homebrew/linked/kibana-full/bin
Conf : /usr/local/etc/kibana
Data : /usr/local/var/lib/kibana
Logs : /usr/local/var/log/kibana
Plugins : /usr/local/var/homebrew/linked/kibana-full/plugins

 

CORS 문제해결

/usr/local/etc/elasticsearch/elasticsearch.yml 하단 코드 추가

http.cors.enabled : true
http.cors.allow-origin : "*"
http.cors.allow-methods : OPTIONS, HEAD, GET, POST, PUT, DELETE
http.cors.allow-headers : X-Requested-With,X-Auth-Token,Content-Type, Content-Length

 

Restful API 관련

GENERAL : _doc

PUT : _create

POST : _update

BULK : _bulk

SEARCH : _search

 

PUT

//같은 Doc이 존재시 덮어씀
PUT my_index/_doc/1
{
  "name":"Jongmin Kim",
  "message":"안녕하세요 Elasticsearch"
}

//같은 Doc 존재시 409 Code Return
PUT my_index/_create/1
{
  "name":"Jongmin Kim",
  "message":"안녕하세요 Elasticsearch"
}

 

GET

GET my_index/_doc/1

 

Delete

DELETE my_index/_doc/1		//도큐먼트 삭제
DELETE my_index			//인덱스 전체삭제

 

POST

//POST 명령으로 my_index/_doc 까지만 입력한다면 id자동생성
POST my_index/_doc
{
  "name":"Jongmin Kim",
  "message":"안녕하세요 Elasticsearch"
}

// 특정필드만 업데이트
POST my_index/_update/1
{
  "doc": {
    "message":"안녕하세요 Kibana"
  }
}

Bulk 

POST _bulk
{"index":{"_index":"test", "_id":"1"}}
{"field":"value one"}
{"index":{"_index":"test", "_id":"2"}}
{"field":"value two"}
{"delete":{"_index":"test", "_id":"2"}}
{"create":{"_index":"test", "_id":"3"}}
{"field":"value three"}
{"update":{"_index":"test", "_id":"1"}}
{"doc":{"field":"value two"}}

//모든 명령어가 동일할 경우에는 아래와 같이 가능
POST test/_bulk
{"index":{"_id":"1"}}
{"field":"value one"}
{"index":{"_id":"2"}}
{"field":"value two"}
{"delete":{"_id":"2"}}
{"create":{"_id":"3"}}
{"field":"value three"}
{"update":{"_id":"1"}}
{"doc":{"field":"value two"}}

//파일을 이용한 Bulk API Call 
curl -XPOST "http://localhost:9200/_bulk" -H 'Content-Type: application/json' --data-binary @bulk.json

 

URI 검색 

GET test/_search?q=value

//AND를 이용한 검색(AND, OR, NOT 사용가능)
GET test/_search?q=value AND three

//특정 필드에서 검색하고 싶다면
GET test/_search?q=field:value

 

Data Body 검색 

GET test/_search
{
  "query": {
    "match": {
      "field": "value"
    }
  }
}

 

Multitenancy 검색

// 쉼표 검색
GET logs-2018-01,2018-02,2018-03/_search

// 와일드카드 검색
GET logs-2018-*/_search

 

 

기타 유용한 입력값들

General

// 클러스터 조회
http://localhost:9200/

// Query all doc
http://localhost:9200/_all/_search?pretty=true&q=*:*

// Cluster Info
http://localhost:9200/_cluster/health?pretty

 

샤드관련

// 프라이머리 샤드 5, 복제본 1 인 books 인덱스 생성
$ curl -XPUT "http://localhost:9200/books" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "number_of_shards": 5,
    "number_of_replicas": 1
  }
}'

// books 인덱스의 복제본 개수를 0 으로 변경
$ curl -XPUT "http://localhost:9200/books/_settings" -H 'Content-Type: application/json' -d'
{
  "number_of_replicas": 0
}'