Filter elasticsearch results to contain only unique documents based on one field value

前端 未结 2 616
旧时难觅i
旧时难觅i 2020-12-05 15:27

All my documents have a uid field with an ID that links the document to a user. There are multiple documents with the same uid.

I want to p

2条回答
  •  有刺的猬
    2020-12-05 15:47

    In ElasticSearch 5.3 they added support for field collapsing. You should be able to do something like:

    GET /_search
    {
      "query": {
        "multi_match" : {
          "query":    "this is a test", 
          "fields": [ "subject", "message", "uid" ] 
        }
      },
      "collapse" : {
        "field" : "uid" 
      },
      "size": 20,
      "from": 100
    }
    

    The benefit of using field collapsing instead of a top hits aggregation is that you can use pagination with field collapsing.

提交回复
热议问题