gpt4 book ai didi

elasticsearch - 具有多个必须和应该的嵌套 bool 查询

转载 作者:行者123 更新时间:2023-12-02 23:53:46 26 4
gpt4 key购买 nike

看下面我的用例

两个嵌套字段

base1.point1
base1.point2
base1.point3
base1.point4


base2.point1
base2.point2
base2.point3
base2.point4

搜索条件
(
(base1.point1 AND base1.point2) OR (base1.point2 AND base1.point3)
)
AND
(
(base2.point1 AND base2.point2) OR (base2.point2 AND base2.point3)
)

请帮助在单个查询中进行上述编写。

最佳答案

请注意,我假设当您说嵌套时,您实际上是在谈论Nested Datatype而不是简单的Object Datatype

在查看您的问题时,您只关心嵌套字段是否存在,无论它们的值如何。对于此类用例,您将需要使用Exists Query

以下是我的Nested Query的样子:

POST <you_index_name>/_search
{
"query":{
"bool":{
"must":[
{
"bool":{
"should":[
{
"bool":{
"must":[
{
"nested":{
"path":"base1",
"query":{
"exists":{
"field":"base1.point1"
}
}
}
},
{
"nested":{
"path":"base1",
"query":{
"exists":{
"field":"base1.point2"
}
}
}
}
]
}
},
{
"bool":{
"must":[
{
"nested":{
"path":"base1",
"query":{
"exists":{
"field":"base1.point2"
}
}
}
},
{
"nested":{
"path":"base1",
"query":{
"exists":{
"field":"base1.point3"
}
}
}
}
]
}
}
]
}
},
{
"bool":{
"should":[
{
"bool":{
"must":[
{
"nested":{
"path":"base2",
"query":{
"exists":{
"field":"base2.point1"
}
}
}
},
{
"nested":{
"path":"base1",
"query":{
"exists":{
"field":"base2.point2"
}
}
}
}
]
}
},
{
"bool":{
"must":[
{
"nested":{
"path":"base2",
"query":{
"exists":{
"field":"base2.point2"
}
}
}
},
{
"nested":{
"path":"base2",
"query":{
"exists":{
"field":"base2.point3"
}
}
}
}
]
}
}
]
}
}
]
}
}
}

希望这可以帮助!

关于elasticsearch - 具有多个必须和应该的嵌套 bool 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55332738/

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