gpt4 book ai didi

elasticsearch - 更加重视领域的存在

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

我正在尝试学习和编写Elasticsearch查询。我意识到,存在一个“存在”字段,该字段返回指定字段存在或不存在的文档。为了了解这一点,我编写了一个简单的查询,并且我想学习更多并使用查询结构。

我有一个查询,可以简单地检查至少一个指定字段是否存在。但是,我想更加重视一个 Realm 。这是我的查询:

"query": {
"bool": {
"minimum_should_match" : 1,
"should": [
{
"exists": {
"field": "geo"
}
},
{
"exists": {
"field": "location"
}
}
]
"size": 100
}

我想首先获取所有具有地理位置字段的文档(例如,包含位置字段的文档有30个),其余70个文档(尺寸-存在地理字段的文档)将包含位置字段的文档(其他应包含)。因此,在我的案例中,位置字段权重的存在小于地理位置的存在。

我为此尝试了增强,但是当我这样做时,它对我的​​情况没有作用;
"query": {
"bool": {
"minimum_should_match" : 1,
"should": [
{
"exists": {
"field": "geo",
"boost": 5
}
},
{
"exists": {
"field": "location"
}
}
]
"size": 100
}

当我将minimum_should_match更改为2时,它仅返回存在地理字段的文档。

最佳答案

您应该尝试此查询

{
"query": {
"function_score": {
"functions": [
{
"filter": {
"exists": {
"field": "geo"
}
},
"weight": 2
},
{
"filter": {
"exists": {
"field": "location"
}
},
"weight": 1
}
]
}
},
"from": 0,
"_source": [
"geo", "location"
],
"size": 100
}

这给出了以下结果;
 {
"_index": "mentions",
"_type": "post",
"_id": "1",
"_score": 2,
"_source": {
"geo": {
"lon": XXX,
"lat": XXX
},
"location": "California, USA"
}
},

{
"_index": "mentions",
"_type": "post",
"_id": "2",
"_score": 1,
"_source": {
"location": "Berlin, Germany"
}
}

第一个函数的得分为2,因为它具有地理字段,但是第二个得分没有。

关于elasticsearch - 更加重视领域的存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53611210/

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