program story

ElasticSearch-고유 값 반환

inputbox 2020. 8. 12. 08:14
반응형

ElasticSearch-고유 값 반환


languages레코드에서 모든 값을 가져 와서 고유하게 만드는 방법은 무엇입니까 ?

기록

PUT items/1
{ "language" : 10 }

PUT items/2
{ "language" : 11 }

PUT items/3
{ "language" : 10 }

질문

GET items/_search
{ ... }

# => Expected Response
[10, 11]

어떤 도움이라도 좋을 것입니다.


집계라는 용어 를 사용할 수 있습니다 .

{
"size": 0,
"aggs" : {
    "langs" : {
        "terms" : { "field" : "language",  "size" : 500 }
    }
}}

검색하면 다음과 같은 결과가 반환됩니다.

{
"took" : 16,
"timed_out" : false,
"_shards" : {
  "total" : 2,
  "successful" : 2,
  "failed" : 0
},
"hits" : {
"total" : 1000000,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
  "langs" : {
    "buckets" : [ {
      "key" : "10",
      "doc_count" : 244812
    }, {
      "key" : "11",
      "doc_count" : 136794

    }, {
      "key" : "12",
      "doc_count" : 32312
       } ]
    }
  }
}

size집계 지정 내의 파라미터 용어의 최대 수는 집계 결과에 포함한다. 모든 결과가 필요한 경우 데이터의 고유 용어 수보다 큰 값으로 설정하십시오.


Elasticsearch 1.1+에는 고유 한 개수를 제공 하는 카디널리티 집계 가 있습니다.

실제로는 근사치이며 높은 카디널리티 데이터 세트로 정확도가 떨어질 수 있지만 일반적으로 테스트에서는 꽤 정확합니다.

You can also tune the accuracy with the precision_threshold parameter. The trade-off, or course, is memory usage.

This graph from the docs shows how a higher precision_threshold leads to much more accurate results.


Relative error vs threshold


I am looking for this kind of solution for my self as well. I found reference in terms aggregation.

So, according to that following is the proper solution.

{
"aggs" : {
    "langs" : {
        "terms" : { "field" : "language",  
                    "size" : 500 }
    }
}}

But if you ran into following error:

"error": {
        "root_cause": [
            {
                "type": "illegal_argument_exception",
                "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [fastest_method] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
            }
        ]}

In that case, you have to add "KEYWORD" in the request, like following:

   {
    "aggs" : {
        "langs" : {
            "terms" : { "field" : "language.keyword",  
                        "size" : 500 }
        }
    }}

참고URL : https://stackoverflow.com/questions/25465215/elasticsearch-return-unique-values

반응형