php - 如何在Elasticsearch过滤器中组合多项查询

标签 php elasticsearch conditional-statements term multi-term

我目前正在寻找构建(针对我的查询的)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/

    相关文章:

    python:如何输入字符串并将其用作查询匹配elasticsearch

    python - 用于生成嵌套字段的 Elasticsearch Groovy 脚本语法

    postgresql - 在 postgresql 中优化 AND 条件

    php - AJAX + PHP 数据搜索

    php - 为什么我用PHP插入数据库后不能使用数据

    php - 更新 PHP 的简单方法

    elasticsearch - 在elasticsearch中按字段的单词数排序

    Javascript:有理由做 "if (!a || (a && a.a))"吗?

    r - 根据序列中的位置分配新值

    php - 分页 - 始终返回数据库中的所有记录