elasticsearch routing 적용하기

Elastic/Elasticsearch 2013. 1. 3. 14:47

Reference URL

Routing 이란?
  - 색인 시 특정 shard 로 색인을 하고, 검색 시 지정된 shard 로만 검색 할 수 있도록 지원
  - 색인 field 중 unique key 에 해당하는 값을 가지고 routing path 를 지정
  - 검색 시 지정한 path 를 query 에 주어 분산된 indices 를 모두 검색 하지 않고 지정된 indices 만 검색
  - routing field 는 store yes, index not_analyzed 로 설정 되어야 함
 
  - 기본 설정
        "routing" : {
            "required" : true,
            "path" : "test.user_uniq_id"
        }
Routing 설정하기
  - index 생성 시 routing 설정을 포함 시켜 생성 (replica 1, shards 50)
    "settings" : {
        "number_of_shards" : 50,
        "number_of_replicas" : 1,
        "index" : {
            "analysis" : {
                "analyzer" : {
                    "kr_analyzer" : {
                        "type" : "custom",
                        "tokenizer" : "kr_tokenizer",
                        "filter" : ["trim", "kr_filter", "kr_synonym"]
                    },
                    "kr_analyzer" : {
                        "type" : "custom",
                        "tokenizer" : "kr_tokenizer",
                        "filter" : ["trim", "kr_filter", "kr_synonym"]
                    }
                },
                "filter" : {
                    "kr_synonym" : {
                        "type" : "synonym",
                        "synonyms_path" : "analysis/synonym.txt"
                    }
                }
            }
        },
        "routing" : {
            "required" : true,
            "path" : "test.user_uniq_id"
        }
    },
    "mappings" : {
        "test" : {
            "properties" : {
                "docid" : { "type" : "string", "store" : "yes", "index" : "not_analyzed"},
                "title" : { "type" : "string", "store" : "yes", "index" : "analyzed", "term_vector" : "yes", "analyzer" : "kr_analyzer" },
                "user_uniq_id" : { "type" : "string", "store" : "yes", "index" : "not_analyzed" },
                "ymdt" : { "type" : "date", "format" : "yyyyMMddHHmmss", "store" : "yes", "index" : "not_analyzed" }
                }
            }
        }
    }
}'
 
  - document 색인 시 setRouting 설정을 해줘야 정상 동작 함
    IndexRequestBuilder requestBuilder = client.prepareIndex(indexName, indexType);
    requestBuilder.setId(docId);
    requestBuilder.setRouting(docMeta.getUserUniqId());
    requestBuilder.setSource(jsonBuilder);
routing search query uri
  - http://localhost:9200/index0/_search?source={"query":{"bool":{"must":[{"term":{"user_uniq_id":"honggildong@elastic.com"}}],"must_not":[],"should":[]}},"from":0,"size":50,"sort":[{"ymdt":"asc"}],"facets":{}}&routing=honggildong@elastic.com&pretty=true




: