过滤弹性搜索结果仅包含基于一个字段值的唯一文档 [英] Filter elasticsearch results to contain only unique documents based on one field value

查看:116
本文介绍了过滤弹性搜索结果仅包含基于一个字段值的唯一文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的所有文档都有一个 uid 字段,其中的ID将文档链接到用户。有多个文档具有相同的 uid



我想对所有只返回/ code>。



选择相关文档的查询是一个简单的

解决方案

你需要一个 top_hits 聚合。 p>

根据您的具体情况:

  {
查询:{
multi_match:{
...
}
},
aggs:{
top-uids
条款:{
field:uid
},
aggs:{
top_uids_hits:{
top_hits :{
sort:[
{
_score:{
order:de sc
}
}
],
size:1
}
}
}
}
}
}

上面的查询执行你的 multi_match 根据 uid 查询并聚合结果。对于每个uid bucket,它只返回一个结果,但是桶中的所有文档都是根据 _score 按照后代顺序排序的。


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 perform a search over all the documents returning only the highest scoring document per unique uid.

The query selecting the relevant documents is a simple multi_match query.

解决方案

You need a top_hits aggregation.

And for your specific case:

{
  "query": {
    "multi_match": {
      ...
    }
  },
  "aggs": {
    "top-uids": {
      "terms": {
        "field": "uid"
      },
      "aggs": {
        "top_uids_hits": {
          "top_hits": {
            "sort": [
              {
                "_score": {
                  "order": "desc"
                }
              }
            ],
            "size": 1
          }
        }
      }
    }
  }
}

The query above does perform your multi_match query and aggregates the results based on uid. For each uid bucket it returns only one result, but after all the documents in the bucket were sorted based on _score in descendant order.

这篇关于过滤弹性搜索结果仅包含基于一个字段值的唯一文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆