【发布时间】:2018-01-13 00:18:34
【问题描述】:
我正在尝试使用 bool 为 not 构建弹性搜索查询,因为不推荐使用 not。例如。匹配所有不属于foo@bar.com的文档。
我使用 elasticsearch-dsl 尝试了这些(在这篇文章之后https://www.elastic.co/blog/lost-in-translation-boolean-operations-and-filters-in-the-bool-query):
{'query': {
'bool': {
'must_not': [{
'match': {
'author': 'foo@bar.com'
}
}]
}
}}
尽管存在 'author': 'foo@bar.com' 以外的文档,但不会返回任何结果。
我也试过
{'query': {
'bool': {
'must_not': [{
'term': {
'author': 'foo@bar.com'
}
}]
}
}}
这会返回混合文档,其中一些具有'author': 'foo@bar.com',而另一些则没有。
【问题讨论】:
-
您应该尝试在 must 的同一级别添加 must_not。比如:bool > must,must_not。另一方面,应表示的基本级别是“或”。有了这个或者,可能会出现一些不需要的文档。
-
您可以尝试为您的搜索创建一个真正的句子。例如,我想获取不是用户自己的文档并且它们是私有的文档。那么,您应该查询什么?完成我们的句子。
标签: elasticsearch elasticsearch-dsl elasticsearch-py