gpt4 book ai didi

elasticsearch - 带有 must 和 should 运算符的嵌套搜索 bool 查询

转载 作者:行者123 更新时间:2023-11-29 02:56:15 25 4
gpt4 key购买 nike

我正在尝试嵌套聚合并对其应用过滤器。

请看下面的例子:

汽车

  • 品牌
  • 姓名
  • 特点

特点

  • 姓名
  • 值(value)

这是一些测试数据:

car_A:
{
brand :"VW",
name: "Golf",
features: [
{name: "color", value: "black"},
{name: "power", value: "150"}
]
}

car_B:
{
brand :"VW",
name: "Golf",
features: [
{name: "color", value: "blue"},
{name: "power", value: "150"}
]
}

car_C:
{
brand :"VW",
name: "Golf",
features: [
{name: "color", value: "white"},
{name: "power", value: "150"}
]
}

car_D:
{
brand :"BMW",
name: "X3",
features: [
{name: "color", value: "white"},
{name: "power", value: "180"}
]
}

car_E:
{
brand :"BMW",
name: "X5",
features: [
{name: "color", value: "blue"},
{name: "power", value: "250"}
]
}

car_F:
{
brand :"BMW",
name: "X3",
features: [
{name: "color", value: "blue"},
{name: "power", value: "150"}
]
}

这是查询:

"query": {
"nested": {
"path": "features",
"query": {
"bool": {
"should": [
{
"match": {
"features.color": "blue"
}
},
{
"match": {
"features.color": "white"
}
}
],
"must": [
{"match": {
"features.power": 150
}}
]
}
}
}
}

查询结果为A,B,C,F

预期的结果应该是 B,C,F (color=blue OR color=white) AND power=150

最佳答案

试试这个查询:

"query": {
"nested": {
"path": "features",
"query": {
"bool": {
"should": [
{
"match": {
"features.color": "blue"
}
},
{
"match": {
"features.color": "white"
}
}
],
"must": [
{"match": {
"features.power": 150
}}
]
}
}
}
}

意思是,您在查询中使用的字段应以路径的名称为前缀:features.colorfeatures.power.

编辑:

试试这个查询(我错过了这里的本质 - 你需要两个 must,一个是 boolshould):

{
"query": {
"nested": {
"path": "features",
"query": {
"bool": {
"must": [
{
"match": {
"features.power": 150
}
},
{
"bool": {
"should": [
{
"match": {
"features.color": "blue"
}
},
{
"match": {
"features.color": "white"
}
}
]
}
}
]
}
}
}
}
}

关于elasticsearch - 带有 must 和 should 运算符的嵌套搜索 bool 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30935978/

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