gpt4 book ai didi

python - 如何从Elastic Search中获取唯一分数搜索结果

转载 作者:行者123 更新时间:2023-12-03 01:18:55 24 4
gpt4 key购买 nike

我正在尝试通过 flex 搜索显示唯一的“说明”行。我试图获取具有相同描述的许多重复行中的仅一行。我不想汇总,因为我也需要其他列中的其他信息。下面的代码是我想要实现的,但没有解决。

  res = esconnection.search(index='data', body={
# "query": {
# "match": {"description": query_input}
# },
# "size": 30

"query": {
"multi_match": {
"description": query_input
}
},
"aggs": {
"top-descriptions": {
"terms": {
"field": "description"
},
"aggs": {
"top_description_hits": {
"top_hits": {
"sort": [
{
"_score": {
"order": "desc"
}
}
],
"size": 1
}
}
}
}

}
})
return res["hits"]["hits"]

最佳答案

Field collapsing可用于将字段上的文档分组

Allows to collapse search results based on field values. The collapsing is done by selecting only the top sorted document per collapse key. For instance the query below retrieves the best tweet for each user and sorts them by number of likes.



数据
[
{
"_index" : "index4",
"_type" : "_doc",
"_id" : "P1lTjHEBF99yL6wF31iA",
"_score" : 1.0,
"_source" : {
"description" : "brown fox"
}
},
{
"_index" : "index4",
"_type" : "_doc",
"_id" : "QFlTjHEBF99yL6wF8liO",
"_score" : 1.0,
"_source" : {
"description" : "brown fox"
}
},
{
"_index" : "index4",
"_type" : "_doc",
"_id" : "QVlTjHEBF99yL6wF91gU",
"_score" : 1.0,
"_source" : {
"description" : "brown fox"
}
},
{
"_index" : "index4",
"_type" : "_doc",
"_id" : "QllUjHEBF99yL6wFFFh5",
"_score" : 1.0,
"_source" : {
"description" : "brown dog"
}
},
{
"_index" : "index4",
"_type" : "_doc",
"_id" : "Q1lUjHEBF99yL6wFGFhQ",
"_score" : 1.0,
"_source" : {
"description" : "brown dog"
}
}
]

我有三个描述为“棕狐”的文档和两个描述为“棕狗”的文档

查询:
{
"query": {
"match": {
"description": {
"query": "brown"
}
}
},
"collapse": {
"field": "description.keyword" --> notice keyword
}
}

结果:
"hits" : [
{
"_index" : "index4",
"_type" : "_doc",
"_id" : "P1lTjHEBF99yL6wF31iA",
"_score" : 0.087011375,
"_source" : {
"description" : "brown fox"
},
"fields" : {
"description.keyword" : [
"brown fox"
]
}
},
{
"_index" : "index4",
"_type" : "_doc",
"_id" : "QllUjHEBF99yL6wFFFh5",
"_score" : 0.087011375,
"_source" : {
"description" : "brown dog"
},
"fields" : {
"description.keyword" : [
"brown dog"
]
}
}
]

仅返回2个文档。
字段折叠提供了诸如“inner_hits”之类的功能:如果要查看组下的文档。使用排序,您可以决定要显示哪个文档。

关于python - 如何从Elastic Search中获取唯一分数搜索结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61271986/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com