gpt4 book ai didi

elasticsearch - Elasticsearch :如何 'OR'两个不同的嵌套查询?

转载 作者:行者123 更新时间:2023-12-02 23:40:59 25 4
gpt4 key购买 nike

我有一个User映射,例如:

name: { 
type: "string"
},
followed_groups: {
type: "nested",
properties: {
created_at: {
type: "string"
},
group_id: {
type: "string"
}
}
},
id: {
type: "integer"
},
member_of_groups: {
type: "nested",
properties: {
created_at: {
type: "string"
},
group_id: {
type: "string"
}
}
}

我想吸引正在关注某个组或该组成员的用户。

每个查询都可以使用嵌套查询,例如:
"query":{
"nested":{
"query":{
"terms":{
"followed_groups.group_id":[
"22"
]
}
},
"path":"followed_groups"
}
}

但是我找不到一种结合 OR条件的方法。可以吗?

最佳答案

在我看来,您并不在乎使用查询提供的评分,有关使用过滤器的解决方案,请参见下文。

您可以简单地使用您在should指令中的查询数组中给出的query指令:

{ "query" : {
"bool" : {
"should" : [
{
"query" : {
"nested" : {
"query" : {
"terms" : {
"followed_groups.group_id" : [
"22"
]
}
},
"path" : "followed_groups"
}
}
},
{…}
]
}
}}

可以在最高级别使用过滤器,但这在同时使用构面时效果也不佳,因此我将在此处使用 filtered查询:
{
"query": {
"filtered" : {
"query" : { "match_all" : {} },
"filter" :{ "or" : [
{
"nested":{
"query":{
"terms":{
"followed_groups.group_id":[
"22"
]
}
},
"path":"followed_groups"
}
},
{…}
]
}
}
}
}

我还没有测试过,但这至少应该为您的查询提供正确的结构。

关于elasticsearch - Elasticsearch :如何 'OR'两个不同的嵌套查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18653170/

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