How to remove duplicate search result in elasticsearch?

夙愿已清 提交于 2019-12-18 03:37:42

问题


First Create some example data (e1,e2,e3 are types and test is the index name):

PUT test/e1/1
{
  "id":1
  "subject": "subject 1"
}
PUT test/e2/1
{
  "id":1
  "subject": "subject 2"
}
PUT test/e3/2
{
  "id":2
  "subject": "subject 3"
}

Now my question is: how can I get just these two data? remove duplicate data with the same id in the curl -XGET _search result.

test/e1/1
{
  "id":1
  "subject": "subject 1"
}
test/e3/2
{
  "id":2
  "subject": "subject 3"
}

回答1:


First you will need to search across multiple index.
Then, on the result remove the duplicate ID.

POST  http://myElastic.com/test/e1,e2,e3/_search
{
  "aggs":{
    "dedup" : {
      "terms":{
        "field": "id"
       },
       "aggs":{
         "dedup_docs":{
           "top_hits":{
             "size":1
           }
         }
       }    
    }
  }
}

This might help you:

  • search multi-index type
  • Remove duplicate documents from a search in Elasticsearch
  • Filter elasticsearch results to contain only unique documents based on one field value


来源:https://stackoverflow.com/questions/29886477/how-to-remove-duplicate-search-result-in-elasticsearch

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