将match_all与过滤器配合使用

时间:2019-05-08 14:33:28

标签: php elasticsearch search

我有一个页面,允许用户查询数据集并应用过滤器。他们还可以应用过滤器而无需查询字符串。为此,我尝试将match_all与过滤器配合使用,但出现以下错误

  

“ {” error“:{” root_cause“:[{” type“:” parsing_exception“,” reason“:” [match_all]   查询格式错误,应为[END_OBJECT],但已找到   [FIELD_NAME]“,” line“:1,” col“:26}],” type“:” parsing_exception“,” reason“:” [match_all]   查询格式错误,应为[END_OBJECT],但已找到   [FIELD_NAME]“,”行“:1,” col“:26},”状态“:400}”,

这是我正在构建并将其发送到弹性客户端的搜索参数的示例。

[
  "type" => "events"
  "index" => "events"
  "body" => [
    "query" => [
      "match_all" => {}
      "bool" => [
        "filter" => [
          "range" => [
            "start_date.date" => [
              "gte" => "01/05/2019"
              "lte" => "05/2019"
              "format" => "dd/MM/yyyy||MM/yyyy"
            ]
          ]
        ]
      ]
    ]
    "from" => 0
    "size" => 30
  ]
]

我似乎无法弄清楚如何同时使用它们。有指针吗?谢谢。

1 个答案:

答案 0 :(得分:2)

您将需要将查询包装在bool查询中,如下所示:

"query": {
    "bool" : {
        "must" : {
        "match_all": {}
        },
        "filter": {
        "range" : { /* your filter here*/ }
        }
    }
}

只需在您的bool周围包裹must和一个match_all查询,它应该可以工作。

我不知道确切的PHP语法,但是应该是这样的:

[
  "type" => "events"
  "index" => "events"
  "body" => [
    "query" => [
      "bool" => [
        "must" => [ "match_all" => {}]
        "filter" => [
          "range" => [
            "start_date.date" => [
              "gte" => "01/05/2019"
              "lte" => "05/2019"
              "format" => "dd/MM/yyyy||MM/yyyy"
            ]
          ]
        ]
      ]
    ]
    "from" => 0
    "size" => 30
  ]
]

有关参考,请参见文档Elasticsearch Reference [7.0] » Query DSL » Compound queries » Bool Query,其中包含与您的示例类似的示例,其中match_all与过滤器结合使用。

相关问题