'Routing'에 해당되는 글 6건

  1. 2021.08.12 [Elasticsearch] _routing 활용에 따른 특정 shard 만 _forcemerge 하고 싶을 경우.
  2. 2015.12.10 [Elasticsearch] Shard Allocation Filtering 설정 시 주의사항. (on 2.1)
  3. 2015.08.27 [Elasticsearch] _id 와 _routing 이해하기.
  4. 2014.08.27 [ElasticSearch] Hash Partition 테스트
  5. 2013.01.16 elasticsearch 색인 생성 스크립트 예제
  6. 2013.01.03 elasticsearch routing 적용하기 1

[Elasticsearch] _routing 활용에 따른 특정 shard 만 _forcemerge 하고 싶을 경우.

Elastic/Elasticsearch 2021. 8. 12. 14:07

그냥 pull request 날렸습니다.

https://github.com/elastic/elasticsearch/pull/76400


그러나 Contributor Agreement 를 안했다고 하는데 이것 참.. 
했는데 왜 저게 나오는지 도대체 Docu Sign 을 몇 번을 했는지 모르겠네요 ㅡ.ㅡ;


코드를 확인해 보니 _forcemerge API 에는 특정 shard 만 지정 하는 기능은 없었습니다.

뭐 어떻게 보면 당연한 이야기지만, 그래도 _routing 을 사용 하면서 특정 shard 로 operation 이 집중되게 되면 해당 shard 의  segments 파일이 많이 생성 될 수도 있습니다.

 

그래서 코드를 좀 고쳤습니다.

contribute 을 위해 pull request 를 올려야 하는데 이 귀차니즘을 어찌 하죠.

암튼 별건 아니라서 일단 코드 부터 올려 놓습니다. 

 

 

:

[Elasticsearch] Shard Allocation Filtering 설정 시 주의사항. (on 2.1)

Elastic/Elasticsearch 2015. 12. 10. 11:01

hot-warm architecture 구성시 경험했던 팁 공유 합니다.

아주 사소한 팁입니다.


원문링크)

https://www.elastic.co/guide/en/elasticsearch/reference/2.1/shard-allocation-filtering.html


index settings 기능을 이용해서 "index.routing.allocation.{attribute}.{attribute}" 설정을 하게 됩니다.

이 과정에서 사용하는 REST API 가 두 가지가 있습니다.


[_settings API]

$ curl -XPUT "http://localhost:9200/db/_settings" -d'

{

    "index.routing.allocation.require.box_type":"warm"

}'


[Request body에 settings]

$ curl -XPUT "http://localhost:9200/db" -d'

{

  "settings": {

    "index.routing.allocation.require.box_type":"warm"

  }

}'


개인적으로는 위 두 가지 방식이 다 동작해야 한다고 생각해서 실행을 시켰습니다.

해보시면 아시겠지만 아래 방식은 index_already_exists_exception 에러가 발생을 합니다.

Elasticsearch에 확인해본 결과로는 에러 메시지를 잘못 return 해준 경우라고 하내요. 즉, trivial 정도의 bug(?) 라고 봐도 될 것 같긴 합니다.

어쨌든 수정할 거라고 하니 나중에는 반영 되리라 기대 합니다.


그리고 http method 사용시 보시면 아시겠지만 PUT method 를 사용하셔야 합니다.

제가 POST 를 사용했는데요.

이 경우에는 PUT 을 사용하는게 맞다고 합니다.

제가 삽질한 이유가 되겠습니다. ㅡ.ㅡ;;

:

[Elasticsearch] _id 와 _routing 이해하기.

Elastic/Elasticsearch 2015. 8. 27. 11:55

Elasticsearch에서 routing 기능은 다양하게 활용이 가능 합니다.

이게 무엇인지는 아래 elastic official document 를 먼저 읽어 보시면 좋을 것 같습니다.


[Reference. _routing]

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


위 문서의 요약 내용은 아래와 같습니다.

- store set to true

- index set to not_analyzed


여기서 not_analyzed로 설정 하는 이유는 shard routing 을 위한 shard id 를 구하기 위해서 입니다.

즉, 단일 구성의 key로 만들어야 routing 정보가 정해지기 때문입니다.


[Reference. _id]

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

※ _routing 과 동일한 mapping 속성을 갖습니다.


_id와 _routing 의 이해


기본적으로 문서를 색인하기 위해서는 문서의 unique id 가 필요 합니다.

elasticsearch 에서는 _id 라는 필드를 이용해서 문서의 uniqueness 를 보장해 주고 있는데요.

이 값은 hash 알고리즘을 이용해서 색인할 shard id 를 생성 합니다. 

또한, _id 값은 Get API 를 이용해서  문서를 직접 access 할 수 있게 해줍니다.


[Get API]

$ curl -XGET http://localhost:9200/INDEX/TYPE/_id


_routing은 쉽게는 문서에 대한 그룹이나 분류에 활용을 할 수 있는 기능이라고 이해 하시면 좋습니다.

즉, 문서를 색인 할 때 같은 분류에 속하는 문서들을 특정 shard로 색인을 하고 검색 시에도 모든 shard를 대상으로 하지 않기 때문에 성능적인 부분이나 활용에서도 유용합니다.


_routing도 _id 와 동일하게 해당 값을 이용해서 shard id 를 구한다는 점에서 동일하며 mapping 설정도 동일 합니다.

차이점이 있다면 _id 는 문서 자체에 대한 routing 이고, _routing 은 문서 집단 또는 그룹에 대한 routing 이라는 것입니다.

또한 검색 입장에서는 _id 는 문서 하나를 검색해서 가져오지만, _routing 은 지정된 key 값에 의한 shard 들을 대상으로 검색을 하게 됩니다.


아래는 REST API 형태의 예제 입니다.


※ _id 가 1 인 문서 검색

$ curl -XGET http://localhost:9200/INDEX/TYPE/1


※ _routing 을 하나 만 지정한 문서 검색 (routing 값은 sports)

$ curl -XGET http://localhost:9200/_search?routing=sports

sports 에 해당하는 shard id 가 0 이라면 0 번 shard 로만 검색 질의가 실행 됩니다.


※ _routing 을 두 개 지정한 문서 검색 (routing 값은 sports, entertainment)

$ curl -XGET http://localhost:9200/_search?routing=sports,entertainment

sports 에 해당하는 shard id 가 0이고 entertainment 에 해당하는 shard id 가 1 이라면 0과 1번 shard 로만 검색 질의가 실행 됩니다.


:

[ElasticSearch] Hash Partition 테스트

Elastic/Elasticsearch 2014. 8. 27. 14:15

간혹 특정 shard 로 색인 문서가 몰리는 경우가 있습니다.

이럴경우 _id 값에 대한 key 조합을 확인해야 할 필요가 있는데요.

es 내부에서 사용하는 hash 함수를 이용해서 간단하게 테스트 해볼수 있습니다.


[테스트 코드]

public class EsHashPartitionTest {

    private static final Logger log = LoggerFactory.getLogger(EsHashPartitionTest.class);

    private HashFunction hashFunction = new DjbHashFunction();

    


    @Test

    public void testHashPartition() {

        int shardSize = 120;

        List<Long> shards = new ArrayList<Long>();

        long[] partSize = new long[shardSize];

        

        for ( int i=0; i<shardSize; i++ ) {

            shards.add((long) 0);

            partSize[i] = 0;

        }

        

        for ( int i=0; i<1000000; i++ ) {

            int shardId = MathUtils.mod(hash(String.valueOf(i)), shardSize);

            shards.add(shardId, (long) ++partSize[shardId]);

        }

        

        for ( int i=0; i<shardSize; i++ ) {

            log.debug("["+i+"] {}", partSize[i]);

        }

    }

    

    public int hash(String routing) {

        return hashFunction.hash(routing);

    }

}


[Hash 함수 원본 코드]

/**

 * This class implements the efficient hash function

 * developed by <i>Daniel J. Bernstein</i>.

 */

public class DjbHashFunction implements HashFunction {


    public static int DJB_HASH(String value) {

        long hash = 5381;


        for (int i = 0; i < value.length(); i++) {

            hash = ((hash << 5) + hash) + value.charAt(i);

        }


        return (int) hash;

    }


    public static int DJB_HASH(byte[] value, int offset, int length) {

        long hash = 5381;


        final int end = offset + length;

        for (int i = offset; i < end; i++) {

            hash = ((hash << 5) + hash) + value[i];

        }


        return (int) hash;

    }


    @Override

    public int hash(String routing) {

        return DJB_HASH(routing);

    }


    @Override

    public int hash(String type, String id) {

        long hash = 5381;


        for (int i = 0; i < type.length(); i++) {

            hash = ((hash << 5) + hash) + type.charAt(i);

        }


        for (int i = 0; i < id.length(); i++) {

            hash = ((hash << 5) + hash) + id.charAt(i);

        }


        return (int) hash;

    }

}

- https://github.com/elasticsearch/elasticsearch/tree/master/src/main/java/org/elasticsearch/cluster/routing/operation/hash


이와 관련된 자세한 내용은 아래 링크 참고하세요.

http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/routing-value.html

:

elasticsearch 색인 생성 스크립트 예제

Elastic/Elasticsearch 2013. 1. 16. 14:54

그냥 예제 입니다. ㅎㅎ

생성 시 적용된 내용은
- replica 설정
- shards 설정
- refresh interval 설정
- term index interval 설정
- field store 시 compression 설정
- analyzer 설정
- synonym 설정
- routing 설정
- _all disable(?) 설정

# index 삭제

curl -XDELETE 'http://localhost:9200/index0/'


# index 생성

curl -XPUT 'http://localhost:9200/index0' -d '{

    "settings" : {

        "number_of_shards" : 50,

        "number_of_replicas" : 1,

        "index" : {

            "refresh_interval" : "60s",

            "term_index_interval" : "1",

            "store" : {

                "compress" : {

                    "stored" : true,

                    "tv" : true

                }

            },

            "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" : "indexType.user_uniq_id"

        }

    },

    "mappings" : {

        "indexType" : {

            "properties" : {

                "docid" : { "type" : "string", "store" : "yes", "index" : "not_analyzed", "include_in_all" : false },

                "rm_seq" : { "type" : "long", "store" : "yes", "index" : "no", "include_in_all" : false },

                "rm_join_seq" : { "type" : "long", "store" : "yes", "index" : "no", "include_in_all" : false },

                "rm_title" : { "type" : "string", "store" : "yes", "index" : "analyzed", "term_vector" : "yes", "analyzer" : "kr_analyzer", "include_in_all" : false },

                "user_uniq_id" : { "type" : "string", "store" : "yes", "index" : "not_analyzed", "include_in_all" : false },

                "mb_nm" : { "type" : "string", "store" : "yes", "index" : "analyzed", "term_vector" : "yes", "analyzer" : "kr_analyzer", "include_in_all" : false },

                "mb_count" : { "type" : "integer", "store" : "yes", "index" : "no", "include_in_all" : false },

                "rm_ymdt" : { "type" : "date", "format" : "yyyyMMddHHmmss", "store" : "yes", "index" : "not_analyzed", "include_in_all" : false },

                "data_size" : { "type" : "long", "store" : "yes", "index" : "no", "include_in_all" : false },

                "msgs" : {

                    "properties" : {

                        "msg_seq" : { "type" : "long", "store" : "no", "index" : "no", "include_in_all" : false },

                        "msg" : { "type" : "string", "store" : "yes", "index" : "analyzed", "term_vector" : "yes", "analyzer" : "kr_analyzer", "include_in_all" : false },

                        "send_user_uniq_id" : { "type" : "string", "store" : "yes", "index" : "not_analyzed", "include_in_all" : false },

                        "send_user_nick_nm" : { "type" : "string", "store" : "yes", "index" : "not_analyzed", "term_vector" : "yes", "analyzer" : "kr_analyzer", "include_in_all" : false },

                        "recv_ymdt" : { "type" : "date", "format" : "yyyyMMddHHmmss", "store" : "yes", "index" : "not_analyzed", "include_in_all" : false },

                        "cfn_yn" : { "type" : "string", "store" : "no", "index" : "no", "include_in_all" : false },

                        "send_yn" : { "type" : "string", "store" : "yes", "index" : "not_analyzed", "include_in_all" : false },

                        "msg_type" : { "type" : "integer", "store" : "yes", "index" : "not_analyzed", "include_in_all" : false }

                    }

                }

            }

        }

    }

}'


:

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




: