Php 如何在Elasticsearch筛选器中组合多术语查询
我目前正在寻找为我的查询构建一个Elasticsearch过滤器来过滤我的查询,但它失败了,因为过滤器也没有过滤 ES群集版本为6.8.3。 以下是我当前的筛选代码PHP版本:Php 如何在Elasticsearch筛选器中组合多术语查询,php,
elasticsearch,conditional-statements,term,multi-term,Php,
elasticsearch,Conditional Statements,Term,Multi Term,我目前正在寻找为我的查询构建一个Elasticsearch过滤器来过滤我的查询,但它失败了,因为过滤器也没有过滤 ES群集版本为6.8.3。 以下是我当前的筛选代码PHP版本: 'filter' => [ ['term' => [ 'displayMode' => 'hidden' ]],
'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.activity != 'inactive' AND item.lifetime != 'everlasting' AND item.toDate < NOW())
或
上面的两个示例不起作用,因为它正在过滤所有结果,甚至是有效的结果
或
在上面的例子中,它并没有像我想的那样过滤任何东西
或
我的意思是,它正在过滤所有的结果
有人能帮我吗?因此,根据您的布尔逻辑,您可以简单地将其转换为布尔查询: AND=>过滤器 或者=>应该 != => 一定不要 它将产生以下结果:
{
"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"
}
}
}
}
]
}
}
]
}
}
]
}
}
}
因此,在浪费了大量时间之后,我终于意识到我的索引没有包含life字段。所有的过滤器条件都失败了。我的第一个问题是做我想做的事情的好方法。
祝您愉快。您能用简单的AND/OR/NOT布尔逻辑写出您的条件吗?@Val我在问题中为您添加了bolean逻辑,谢谢-看一看,看看他们是否以任何方式激励了你,尽管这似乎是从2016年开始的。非常感谢。现在如果我想用和替换OR?我只是想用“必须”替换“应该”?是的,那会有用,但最好你可以用“过滤器”替换“应该”。所有的查询对我都不起作用。当我试图简化时…``````。。。过滤器'=>[['bool'=>['不得'=>[['term'=>['displayMode'=>'未处理']],['term'=>['寿命'=>'临时']]],/…``它正在过滤所有的项目,不管项目属性是什么..你为什么现在转换或到?你能给出你想要在不改变任何内容的情况下实际执行的确切布尔逻辑吗?好的,午饭后我再看一看。很高兴你找到了它。
// ...
'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' ]]
]
]]
{
"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"
}
}
}
}
]
}
}
]
}
}
]
}
}
}