gpt4 book ai didi

elasticsearch - 应该查询里面必须elasticsearch

转载 作者:行者123 更新时间:2023-12-03 02:35:33 25 4
gpt4 key购买 nike

我是Elasticsearch查询DSL的新手,我的ES索引中包含以下文档

{
"doc_id":1,
"city_id": 1,
"name": "test1",
"vvip": false
},
{
"doc_id":2,
"city_id": 1,
"name": "test2",
"vvip": false
},
{
"doc_id":3,
"city_id": 1,
"name": "test3"
}
{
"doc_id":4,
"city_id": 1,
"name": "test4"
}
{
"doc_id":5,
"city_id": 1,
"name": "test5",
"vvip": true
}
{
"doc_id":6,
"city_id": 1,
"name": "test6",
"vvip": true
}

对于doc_id 3和doc_id 4,vvip字段不存在

我想以这样的方式形成查询:如果我查询vvip:true或vvip:false,则应始终返回这些文档

查询city_id = 1和vvip = false的含义
响应将包含doc_id 1,2,3,4
对于city_id = 1和vvip = true
响应将包含doc_id 3、4、5、6

我在下面的查询中尝试过,但没有帮助
{
"query": {
"bool": {
"must": [
{
"bool": {
"must": [
{
"term": {
"city_id": "1"
}
},
{
"bool": {
"must_not": {
"exists": {
"field": "vvip"
}
}
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"city_id": "1"
}
},
{
"match": {
"vvip": false
}
}
]
}
}
]
}
}
}

最佳答案

您可以像这样简化查询,该查询应该可以按预期工作:

{
"query": {
"bool": {
"must": [
{
"match": {
"city_id": "1"
}
},
{
"bool": {
"should": [
{
"match": {
"vvip": true
}
},
{
"bool": {
"must_not": {
"exists": {
"field": "vvip"
}
}
}
}
]
}
}
]
}
}
}

该查询意味着您匹配 city_id=1 ( vvip=true vvip不存在)。

关于elasticsearch - 应该查询里面必须elasticsearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58975150/

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