gpt4 book ai didi

elasticsearch - bool 过滤器以及应和必须组合

转载 作者:行者123 更新时间:2023-12-02 22:51:35 32 4
gpt4 key购买 nike

我对 bool(boolean) 查询中应该和必须使用的用法有些困惑。当您在SHOULD和MUST子句中有多个过滤器时,它们可以放在同一级别还是应该嵌套?

以下是我的数据和我测试的两个查询的简化版本,第一个失败,第二个工作。在实际操作中,我必须且应该有许多过滤器。

我开始相信,如果要合并多个SHOULD和MUST过滤器,则必须始终将外部过滤器组合在一起。这是正确的假设吗?如果我想使用MUST_NOT,在这种情况下应将其放在哪里?

我的资料:

_index,_type,_id,_score,_source.id,_source.type,_source.valueType,_source.sentence,_source.location
"test","var","0","1","0","study","text","Lorem text is jumbled","spain"
"test","var","1","1","1","study","text","bla bla bla","spain"
"test","var","2","1","2","schema","decimal","ipsum","germany"
"test","var","3","1","3","study","integer","lorem","france"

这是失败的查询:
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must": {
"terms": {
"location": [
"germany"
]
}
},
"should": {
"terms": {
"valueType": [
"integer"
]
}
}
}
}
}
}
}

这是我的工作查询,返回ID 2和3:
{
"query": {
"bool": {
"should": [
{
"terms": {
"location": [
"germany"
]
}
},
{
"bool": {
"must": [
{
"terms": {
"valueType": [
"integer"
]
}
}
]
}
}
]
}
}
}

非常感谢。

最佳答案

首先需要了解过滤器的含义。
复合过滤器:must子句是必需的(和)should子句是可选的(或)
因此,在第一步中,您正在检查term中的must(和)。因此,该术语必须在结果集中。并且cond 2应该(也可以不)在结果集中。

{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must": {
....... Cond 1
},
"should": {
....... Cond 2
}
}
}
}
}
}
在您的工作场景中,您正在查询工作,因为应该检查条件1或条件2。
{
"query": {
"bool": {
"should": [ // OR
{
...... Cond 1
},
{
...... Cond 2
}
]
}
}
}

关于elasticsearch - bool 过滤器以及应和必须组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26715173/

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