索引中有两个字段。让我们假设为 field1 和 field2。
搜索案例
- 两者都可能出现
- 字段 1 或字段 2 可能存在
- field1 和 field2 可能会出现
这里我必须创建一个像这样的查询
- 如果 field1 存在,则对其进行搜索并忽略 field2
- 如果 field1 不存在,则对 field2 应用搜索
- 如果两者都存在,则仅在字段 1 上应用搜索,忽略字段 2
有人可以帮忙查询吗
最佳答案
我认为您要寻找的是 1 个 should
,其中包含 3 个 must
:
GET conditionals/_search
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"exists": {
"field": "field1"
}
},
{
"match": {
"field1": "abc"
}
}
]
}
},
{
"bool": {
"must": [
{
"bool": {
"must_not": [
{
"exists": {
"field": "field1"
}
}
]
}
},
{
"match": {
"field2": "xyz"
}
}
]
}
},
{
"bool": {
"must": [
{
"bool": {
"must": [
{
"exists": {
"field": "field1"
}
},
{
"exists": {
"field": "field2"
}
}
]
}
},
{
"match": {
"field2": "xyz"
}
}
]
}
}
],
"minimum_should_match": 1
}
}
}
关于java - 如果存在则在 field1 上应用搜索并忽略 field2 否则在 field2 上搜索 Elasticsearch ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62179490/