Search for documents with the same value in Elasticsearch

白昼怎懂夜的黑 提交于 2019-12-11 11:34:53

问题


I have a schema that looks something like this:

{
  "mappings": {
    "entity": {
      "properties": {
      "a": {
        "type": "text"
      },
      "b": {
        "type": "text"
      }
    }
  }

I want to find all the values of b which have a value of a which is shared by 2 or more entities:

Querying against:

[{"a": "a1", "b": "b1"}, 
 {"a": "a1", "b": "b2"}, 
 {"a": "a2", "b": "b3"}]

Should return b1 and b2.


回答1:


You can do a terms aggregation on the a field with a min_doc_count of 2 and then add a top_hits sub-aggregation to find the matching b fields:

{
  "size": 0,
  "aggs": {
    "dups": {
      "terms": {
        "field": "a",
        "min_doc_count": 2
      },
      "aggs": {
        "b_hits": {
          "top_hits": {
            "_source": "b"
          }
        }
      }
    }
  }
}


来源:https://stackoverflow.com/questions/51669448/search-for-documents-with-the-same-value-in-elasticsearch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!