일래스틱서치가 요청을 받으면, 응답 결과가 아무것도 안오는 경우가 있다. 


그것은  일래스틱서치로 보내는 요청이 잘 못 될 가능성이 있다. 이럴 때에는 헤드(head) 플러그인에서 validate JSON을 해본다.


아래 요청을 복사해서..


{

    "sort" : [

        { "date" : {"order" : "asc"}},

    ],

    "query" : {

        "term" : { "user" : "kimchy" }

    }

}




헤드 플러그인에 Any Request로 붙여둔다.



Unexpected token ] 였다. 잘못되어 있었다면 요청을 수정하면 된다. 

JSON오류를 수정하였다. 이 요청으로 일래스틱서치에 보내면 정상적인 응답을 받을 수 있다. 





Posted by '김용환'
,



일래스틱서치 데이터를 복구(또는 복사)하는 방법은 크게 2 가지가 있다.


- scan/scroll & bulk create

0.9부터 사용할 수 있다. scroll 로 얻은 JSON 포맷을 파일로 저장한 후 bulk index/create를 여러 개수(대충 1000개)씩 색인에 저장할 수 있다. 큰 장점은 서로 다른 ES에도 쉽게 복사할 수 있으며, 데이터가 클수록 시간이 많이 소요된다. 개별 단위로 색인되지 않고, refresh interval을 너무 작게 설정하지 않는 것이 중요하다.


- snapshot & backup

1.0 이후에 쓰일 수 있으며, 정말 빠르다. 또한 하위 버전에서 상위버전 이상(동일 버전 가능)으로 갈 때만 복구할 수 있다. 오직 상향식 복구가 가능하다. 

단점은 alising을 쓰는 색인을 복구(또는 복사)하기가 쉽지 않다. 원래 색인 이름으로만 복구가 된다. aliasing을 안 쓸 때 복구(복사)하기 참 좋다.


Posted by '김용환'
,



일래스틱서치가 플러그인에 의지하는 경우가 있다. 플러그인을 설치하고 사용 가능할 때에만 노드는 실행하도록 설정할 수 있다. 

elasticsearch.yml 설정 파일의 plugin.mandatory 지시자를 이용한다.


예를 들어 일래스틱서치에서 반드시 필요한 플러그인이 chosung이라면 다음과 같은 설정을 elasticsearch.yml에 포함시켜야 한다.


plugin.mandatory: chosung




plugin.mandatory: <plugin1_name>, <plugin2_name>




https://www.elastic.co/guide/en/elasticsearch/reference/1.6/modules-plugins.html

Posted by '김용환'
,



일래스틱서치 모니터링 API를 소개한다.



(1) 전체 클러스터 health 체크

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



여러 개의 index만 볼 수 있다. 

http://localhost:9200/_cluster/health/<node1_name>,<node2_name> ?pretty



(2) 노드 통계 체크


http://localhost:9200/_nodes/stats?all=true


다음 정보를 포함한다.

  • 클러스터 이름
  • 색인
  • 운영체제
  • 일래스틱서치 처리 통계
  • JVM
  • Thread Pool
  • 파일 시스템
  • 통신
  • HTTP
  • 필드 데이터 캐시





http://localhost:9200/_nodes/<nodeId1>,<nodeId2>/stats?all=true


(3) 클러스터 상태 체크 


http://localhost:9200/_cluster/state


클러스터 데이터와 메타 데이터, 라우팅 테이블, 라우팅 노드에 대한 정보를 포함한다.


(4) 클러스터 상태 확인


http://localhost:9200/_cluster/state






(5) 이슈 있는 쓰레드 확인



http://localhost:9200/_nodes/hot_threads



또는 노드 별로 구할 수 있다. 

http://localhost:9200/_nodes/<node_name>/hot_threads




(6) 큐에 쌓인 태스크 확인


http://localhost:9200/_cluster/pending_tasks





참조


https://www.elastic.co/guide/en/elasticsearch/reference/1.7/cluster-health.html

https://www.elastic.co/guide/en/elasticsearch/reference/1.7/cluster-state.html

https://www.elastic.co/guide/en/elasticsearch/reference/1.7/cluster-nodes-stats.html

https://www.elastic.co/guide/en/elasticsearch/reference/1.7/cluster-pending.html

https://www.elastic.co/guide/en/elasticsearch/reference/1.7/cluster-nodes-hot-threads.html



Posted by '김용환'
,


 아래와 같이 배열로 일래스틱서치에 path.data 매개변수의 값을 raid 0과 같은 구성을 할 수 있다. (참고로 여러 개 지정가능하다.




path.data: ["/data/dir1", "/data/dir2"]


* 참고

https://www.elastic.co/guide/en/elasticsearch/reference/1.6/setup-dir-layout.html

https://ko.wikipedia.org/wiki/RAID




Posted by '김용환'
,



Elasticsearch 의 snapshot/restore 사용시 버전을 잘 확인해야 한다.


일반적으로 잘 모르니. 동일 버전에서만 사용할텐데, 소스를 보면 잘 알 수 있다.

snapshot한 일래스틱서치 버전이 restore할 일래스틱서치 버전보다 하위이면 에러가 발생한다.


즉 snapshot한 버전이 1.4이고, restore할 버전이 1.5 이면 문제가 되지 않지만,

  snapshot한 버전이 1.5이고, restore할 버전이 1.4 이면 문제가 된다.


자세한 것은 아래 소스를 살펴본다.


https://github.com/elastic/elasticsearch/blob/bfbee383bd2b2e4a928006d1703c22c0a2aae155/core/src/main/java/org/elasticsearch/snapshots/RestoreService.java


 private void validateSnapshotRestorable(SnapshotId snapshotId, Snapshot snapshot) {

        if (!snapshot.state().restorable()) {

            throw new SnapshotRestoreException(snapshotId, "unsupported snapshot state [" + snapshot.state() + "]");

        }

        if (Version.CURRENT.before(snapshot.version())) {

            throw new SnapshotRestoreException(snapshotId, "the snapshot was created with Elasticsearch version [" +

                    snapshot.version() + "] which is higher than the version of this node [" + Version.CURRENT + "]");

        }

    }





참고로 snapshot 파일만 다른 서버로 보내고, 메타데이터 설정만 동기화하면 바로 restore로 복구된다!!!

'Elasticsearch' 카테고리의 다른 글

[elasticsearch] 모니터링 API  (0) 2015.08.05
[elasticsearch] raid 0 사용 가능  (0) 2015.08.04
[Elasticsearch] 스냅샷 저장 및 복구  (0) 2015.07.29
[elasticsearch] JSONP 지원  (0) 2015.07.27
[elasticsearch] alias (색인 별명)  (0) 2015.07.22
Posted by '김용환'
,



일래스틱서치의 스냅샷 저장 및 복구하기 





스냅샷을 정의하고 어느 디렉토리에 저장될지 결정한다. 


curl -XPUT 'http://localhost:9200/_snapshot/poi' -d '{

    "type": "fs",

    "settings": {

        "location": "/home/www/backup",

        "compress": true

    }

}'




제대로 되어 있는지 확인한다.


curl -XGET 'http://localhost:9200/_snapshot/poi?pretty'



정상적인지 검증한다. 


curl -XPOST 'http://localhost:9200/_snapshot/poi/_verify'


스냅샷으로 덤프한다. 스냅샷으로 정의된 디렉토리(/home/www/backup)에 덤프된 스냅샾 파일을 볼 수 있다.


curl -XPUT "http://localhost:9200/_snapshot/poi/snap_1?wait_for_completion=true" -d '{

    "indices": "poi",

    "ignore_unavailable": "true",

    "include_global_state": false

}'



wait_for_completion 매개변수가 없으면 비동기로 실행되기 때문에 스냅샷 파일의 완료 시점을 알기 쉽지 않다. 동기 결과를 받기 위해 일부러 사용하는 것이 좋다.




기존 색인을 닫는다.


curl -XPOST http://localhost:9200/poi/_close


만약 기존 색인을 닫지 않으면, 다음과 같은 에러가 발생한다

{"error":"SnapshotRestoreException[[poi:location_poi_20150728] cannot restore index [poi] because it's open]","status":500}


복구(restore)한다.

curl -XPOST "localhost:9200/_snapshot/poi/snap_1/_restore?pretty" -d '{
  "indices": "poi",
  "ignore_unavailable": "true",
  "include_global_state": "false"
}'



* 팁 : 만약 복구할 때, 새로운 이름의 색인으로 변경하려면 다음고 같이 rename_pattern과 rename_replacement를 이용하면 된다. poi라는 색인 대신 aaaaaaaaaaa 라는 색인을 만들 수 있다.

curl -XPOST "localhost:9200/_snapshot/poi/snap_1/_restore?pretty" -d '{
  "indices": "poi",
  "ignore_unavailable": "true",
  "include_global_state": "false",
  "rename_pattern": "poi",
  "rename_replacement": "aaaaaaaaaaa"
}'



Posted by '김용환'
,





일래스틱서치는 JSONP를 지원한다.  모든 REST API는 callback 매개변수를 받을 수 있다. 

그러나 디폴트 값은 false로 되어 있어서(보안상) 사용하렴면 config.yaml 에 다음을 추가해야 한다.


http.jsonp.enable: true 



참고로, BigDesk플러그인을 설치한 후, jsonp 설정이 enable되어야 플러그인을 사용할 수 있다.

Posted by '김용환'
,


일래스틱서치의 색인의 별명이 유용할 때가 있다.


* 배치가 돌고 많은 데이터(랭킹 정보, 검색 정보)를 색인에 저장하면서, 색인 시간이 소요되는 경우에 유용하다.

(단순히 데이터를 일래스틱 서치에 저장하고 검색하는 경우에는 의미없을 수 없다.)



색인 별명(indices aliases)는 get, create, delete를 할 수 있다.


suggest_v1, suggest_v2 색인이 있는데, suggest_v2에 suggest라는 별명을 create한다.



$curl -XPOST 'http://abc.google.com:9200/_aliases' -d '
{
    "actions" : [
        { "add" : { "index" : "suggest_v2", "alias" : "suggest" } }
    ]
}'





애플리케이션 입장에서 suggest 색인을 볼 경우, 별명이 가르키고 있는 suggest_v2 색인을 바라보게 된다.


$ curl -XGET 'http://abc.google.com:9200/_alias/suggest?pretty=true'
{
  "suggest_v2" : {
    "aliases" : {
      "suggest" : { }
    }
  }
}


배치 프로그램에서 suggest_v1 색인 데이터를 교체하고 v1 색인을 suggest에 추가한다. 

$curl -XPOST 'http://abc.google.com:9200/_aliases' -d '
{
    "actions" : [
        { "add" : { "index" : "suggest_v1", "alias" : "suggest" } }
    ]
}'

그러면, 다음과 같이 suggest 색인에는 suggest_v1, suggest_v2 색인이 하나로 묶이게 된다.

$ curl -XGET 'http://abc.google.com:9200/_alias/suggest?pretty=true'
{
  "suggest_v1" : {
    "aliases" : {
      "suggest" : { }
    }
  },
  "suggest_v2" : {
    "aliases" : {
      "suggest" : { }
    }
  }
}



그리고, 바로 suggest_v2 색인을 제거하면, 애플리케이션 장애 없이 잘 동작하게 할 수 있다. 
(트래픽이 많은 경우 아주 잠깐의 데이터 누락처럼 보일 수 있겠지만.. ;;; 서비스 에러는 발생하지 않을 것이다.)


$ curl -XPOST 'http://abc.google.com:9200/_aliases' -d '
{
    "actions" : [
        { "remove" : { "index" : "suggest_v2", "alias" : "suggest" } }
    ]
}'

$ curl -XGET 'http://abc.google.com:9200/_alias/suggest?pretty=true'
{
  "suggest_v1" : {
    "aliases" : {
      "suggest" : { }
    }
  }
}


출처 :

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html




Posted by '김용환'
,



ctx 매개변수에 대해서 일래스틱서치가 자세히 설명된 페이지는 없다. 자주 사용하는 매개변수는 다음과 같다.


  • ctx._source: 다큐먼트의 원본을 포함한다. ctx._source.counter 는 다큐먼트의 원본의 counter를 의미한다.
    • ctx._source.remove("필드이름") : 다큐먼트 원본의 필드 이름을 삭제한다.
    • ctx._source.필드명.contains("값") : 다큐먼트 원본 필드안에 해당 값이 있는지 체크한다.
  • ctx._timestamp : 값이 정의되면, 해당 값은 다큐먼트 타임스탬프로 설정된다.
  • ctx._ttl: ttl 값으로 설정된다.
  • ctx.op : 주요 operation type을 정의한다. 
    • index (기본값): 레코드를 변경 값으로 재색인한다.
    • delete: 다큐먼트를 삭제한다.
    • none: 다큐먼트를 재색인하지 않고, 생략한다.


그리고, _ctx맵에는 상기 적어 놓은 _source, _timestamp, ttl, op말고 _index, _type, _id, _version, _routing, _parent이 있다. 



https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html

https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html

https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-timestamp-field.html

https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-ttl-field.html

Posted by '김용환'
,