我目前正在寻找构建(针对我的查询的)Elasticsearch过滤器以过滤我的查询,但是由于过滤器也没有过滤,因此失败了。
ES群集版本为6.8.3。
这是我当前的过滤器代码(PHP版本):
'filter' => [
['term' =>
[
'displayMode' => 'hidden'
]],
[ 'bool' => [
'must_not' => [
[ 'bool' => [
'must' => [
'term' => ['displayMode' => 'untreated']
]
]],
[ 'bool' => [
'must' => [
'term' => [ 'lifetime' => 'temporary' ]
]
]]
]]
],
[ 'bool' => [
'must' => [
[ 'bool' => [
'must_not' => [
'term' => ['activity' => 'inactive']
]
]],
[ 'bool' => [
'must_not' => [
'term' => [ 'lifetime' => 'everlasting' ]
]
]],
[ 'bool' => [
'should' => [
[ 'range' => [
'toDate' => [
'lt' => (new \DateTime())->format('Y-m-d')
]
]],
[ 'bool' => [
'must_not' => [
'exists' => [
'field' => 'toDate'
]
]
]]
]
]]
]
]]
]
因此,要显示项目,您必须遵守所有这些条件:
item.displayAll != 'hidden'
和
(item.displayMode != 'untreated' AND item.lifetime != 'temporary')
并且
(item.activity != 'inactive' AND item.lifetime != 'everlasting' AND item.toDate < NOW())
不幸的是,我的“多”项条件不是多个,因此每个子过滤器似乎都是一个接一个地执行。因此,如果我的商品是“未经处理的”,但其“寿命”不是“临时”,则该商品正在过滤,但不应过滤。
编辑:
当我尝试简化过滤器时(只是检查小过滤器是否正常工作),它也无法正常工作...
// ...
'filter' => [
['bool' => [
'must_not' => [
['term' => [ 'displayMode' => 'untreated' ]],
['term' => [ 'lifetime' => 'temporary' ]]
]
]],
// ...
要么
'filter' => [
['bool' => [
'must' => [
['bool' => [
'must_not' => [
['term' => [ 'displayMode' => 'untreated' ]],
]
]],
['bool' => [
'must_not' => [
['term' => [ 'lifetime' => 'temporary' ]]
]
]]
]
]],
(上面的两个示例不起作用,因为它过滤了所有结果,即使是有效的结果)
要么
'filter' => [
['bool' => [
'should' => [
['bool' => [
'must_not' => [
['term' => [ 'displayMode' => 'untreated' ]],
]
]],
['bool' => [
'must_not' => [
['term' => [ 'lifetime' => 'temporary' ]]
]
]]
]
]],
(例如上面的例子,它并没有过滤我想要的东西)。
要么
'filter' => [
['bool' => [
'must_not' => [
['term' => [ 'displayMode' => 'untreated' ]],
['term' => [ 'lifetime' => 'temporary' ]]
]
]]
同上,它正在过滤所有结果。
谁能帮我吗?
最佳答案
因此,根据您的 bool(boolean) 逻辑,您可以将其简单地转换为bool
查询:
AND
=> filter
OR
=> should
!=
=> must_not
它将产生以下结果:
{
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{
"bool": {
"must_not": {
"term": {
"displayMode": "hidden"
}
}
}
},
{
"bool": {
"must_not": [
{
"term": {
"displayMode": "untreated"
}
},
{
"term": {
"lifetime": "temporary"
}
}
]
}
},
{
"bool": {
"must_not": [
{
"term": {
"activity": "inactive"
}
},
{
"term": {
"lifetime": "everlasting"
}
},
{
"bool": {
"should": [
{
"range": {
"toDate": {
"lt": "now"
}
}
},
{
"bool": {
"must_not": {
"exists": {
"field": "toDate"
}
}
}
}
]
}
}
]
}
}
]
}
}
}
关于php - 如何在Elasticsearch过滤器中组合多项查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60277802/