elasticsearch Elasticsearch布尔查询,用于检查属性是否完全丢失,或者属性是否存在并包含特定字符串
我试图编写一个elasticsearch布尔查询,检查对象中是否存在字段,然后尝试匹配该字段中可能存在或不存在的字符串。我希望查询返回满足以下条件之一的所有对象(帖子): 1.字段postETA.byline不存在 2.如果确实存在Posteta.byline,则它包含字符串“John Doe” 以下是我尝试的最新查询,但不起作用:elasticsearch Elasticsearch布尔查询,用于检查属性是否完全丢失,或者属性是否存在并包含特定字符串,elasticsearch,elasticsearch,我试图编写一个elasticsearch布尔查询,检查对象中是否存在字段,然后尝试匹配该字段中可能存在或不存在的字符串。我希望查询返回满足以下条件之一的所有对象(帖子): 1.字段postETA.byline不存在 2.如果确实存在Posteta.byline,则它包含字符串“John Doe” 以下是我尝试的最新查询,但不起作用: { "query": { "bool": { "filter": [ { "match": { "status": "publish"
{ "query":
{ "bool":
{ "filter":
[ { "match": { "status": "publish" } },
{ "match": { "authors": "104" } }
],
"should": [
{ "must_not": { "exists": { "field": "postMeta.byline" } } },
{ "filter": {
"wildcard": {
"postMeta.byline": "*John Doe*"
}
}
}
]
}
}
}
在bool查询中,如果您已经有了must或filter,则should将无效。以下为文件所载— 子句(查询)应出现在匹配文档中。如果bool查询位于查询上下文中,并且有一个must或filter子句,则文档将匹配bool查询,即使所有should查询都不匹配。在这种情况下,这些条款仅用于影响分数 您可以尝试的是嵌套bool查询
{ "query":
{ "bool":
{ "filter":
[ { "match": { "status": "publish" } },
{ "match": { "authors": "104" } } ,
{ "bool" :
{ "should" : [
{ "bool": { "must_not": { "exists": { "field": "postMeta.byline" } } } },
{ "wildcard": { "postMeta.byline": "*John Doe*" } }
]
}
}
]
}
}
}
第一个bool在filter中有三个子句。因此,它将匹配所有三个。嵌套布尔应该有。因此,它将至少匹配两个选项中的一个
我还没有测试这个查询 在bool查询中,如果您已经有了must或filter,则should将无效。以下为文件所载— 子句(查询)应出现在匹配文档中。如果bool查询位于查询上下文中,并且有一个must或filter子句,则文档将匹配bool查询,即使所有should查询都不匹配。在这种情况下,这些条款仅用于影响分数 您可以尝试的是嵌套bool查询
{ "query":
{ "bool":
{ "filter":
[ { "match": { "status": "publish" } },
{ "match": { "authors": "104" } } ,
{ "bool" :
{ "should" : [
{ "bool": { "must_not": { "exists": { "field": "postMeta.byline" } } } },
{ "wildcard": { "postMeta.byline": "*John Doe*" } }
]
}
}
]
}
}
}
第一个bool在filter中有三个子句。因此,它将匹配所有三个。嵌套布尔应该有。因此,它将至少匹配两个选项中的一个
我还没有测试这个查询 谢谢@nitzien,感谢您的回复!你的提问几乎奏效了;我所做的唯一改变是将“不得”条款包装在另一个“bool”条款中,如下所示:
{ "query":
{ "bool":
{ "filter":
[ { "match": { "status": "publish" } },
{ "match": { "authors": "104" } } ,
{ "bool" :
{ "should" : [
{ "bool": { "must_not": { "exists": { "field": "postMeta.byline" } } } },
{ "wildcard": { "postMeta.byline": "*John Doe*" } }
]
}
}
]
}
}
}
谢谢@nitzien,谢谢你的回复!你的提问几乎奏效了;我所做的唯一改变是将“不得”条款包装在另一个“bool”条款中,如下所示:
{ "query":
{ "bool":
{ "filter":
[ { "match": { "status": "publish" } },
{ "match": { "authors": "104" } } ,
{ "bool" :
{ "should" : [
{ "bool": { "must_not": { "exists": { "field": "postMeta.byline" } } } },
{ "wildcard": { "postMeta.byline": "*John Doe*" } }
]
}
}
]
}
}
}
谢谢我没有测试它。我也会把你的零钱加到我的答案上。谢谢。我没有测试它。我也会把你的零钱加到我的答案上。