简体   繁体   English

如何在 Elasticsearch 过滤器中组合多词查询

[英]How to combine multi term query in Elasticsearch filter

I'm currently looking for building an Elasticsearch filter (for my query) to filter my query but it's failing because the filter don't filter as well.我目前正在寻找构建一个 Elasticsearch 过滤器(用于我的查询)来过滤我的查询,但它失败了,因为过滤器也没有过滤。

The ES cluster version is 6.8.3. ES集群版本为6.8.3。 Here is my current filter code (PHP version):这是我当前的过滤器代码(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'
                                                    ]
                                                ]
                                            ]]
                                        ]
                                    ]]
                                ]
                            ]]
      ]

So, to display an item, you must respect all these conditions:因此,要显示项目,您必须遵守所有这些条件:

item.displayAll != 'hidden'

AND

(item.displayMode != 'untreated' AND item.lifetime != 'temporary')

AND ALSO并且

(item.activity != 'inactive' AND item.lifetime != 'everlasting' AND item.toDate < NOW())

Unfortunately, my "multi" term conditions are not multi, so each of the sub filters seems to be executed one by one.不幸的是,我的“多”条款条件不是多,所以每个子过滤器似乎都被一个一个地执行。 So if my item are "untreated" but its "lifetime" is other than "temporary", the item is filtering but it shouldn't be.因此,如果我的项目“未经处理”但其“生命周期”不是“临时”,则该项目正在过滤,但不应过滤。

EDIT: When I try to simplify my filter (just to check if the little one is working) it's not working also...编辑:当我尝试简化我的过滤器时(只是为了检查小过滤器是否在工作)它也不起作用......

// ...
'filter' => [
                                ['bool' => [
                                    'must_not' => [
                                        ['term' => [ 'displayMode' => 'untreated' ]],
                                        ['term' => [ 'lifetime' => 'temporary' ]]
                                    ]
                                ]],
   // ...

OR或者

'filter' => [
                                ['bool' => [
                                    'must' => [
                                        ['bool' => [
                                            'must_not' => [
                                                ['term' => [ 'displayMode' => 'untreated' ]],
                                            ]
                                        ]],
                                        ['bool' => [
                                            'must_not' => [
                                                ['term' => [ 'lifetime' => 'temporary' ]]
                                            ]
                                        ]]
                                    ]
                                ]],

(the two examples above are not working because it's filtering all results, even the valid ones) (上面的两个例子不起作用,因为它过滤了所有结果,甚至是有效的结果)

OR或者

'filter' => [
                                ['bool' => [
                                    'should' => [
                                        ['bool' => [
                                            'must_not' => [
                                                ['term' => [ 'displayMode' => 'untreated' ]],
                                            ]
                                        ]],
                                        ['bool' => [
                                            'must_not' => [
                                                ['term' => [ 'lifetime' => 'temporary' ]]
                                            ]
                                        ]]
                                    ]
                                ]],

(Exemple above, it's not filtering anything like I wanted to). (上面的例子,它没有像我想要的那样过滤任何东西)。

OR或者

'filter' => [
                                ['bool' => [
                                    'must_not' => [
                                        ['term' => [ 'displayMode' => 'untreated' ]],
                                        ['term' => [ 'lifetime' => 'temporary' ]]
                                    ]
                                ]]

Idem, it's filtering all the results..同上,它正在过滤所有结果..

Can anyone can help me please?任何人都可以帮助我吗?

So according to your boolean logic, you can simply transform that into bool queries:因此,根据您的布尔逻辑,您可以简单地将其转换为bool查询:

  • AND => filter AND => filter
  • OR => should OR => should
  • != => must_not != => must_not

It would yield this:它会产生这样的结果:

{
  "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"
                          }
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}

So, after a huge waste of time, I finally realized that my index was not containing the "lifetime" field.所以,在浪费了大量时间之后,我终于意识到我的索引不包含“lifetime”字段。 All the filters conditions was failed for that.为此,所有过滤条件都失败了。 My first query was the good one (or one way to do what I wanted to do).我的第一个查询是好的(或者做我想做的事情的一种方式)。 Have a good day.祝你有美好的一天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM