4
votes

I have a page which allows users to query datasets and apply filters. They can also apply filters without querying with a string. To do so I'm attempting to use match_all with filters but getting the following error

"{"error":{"root_cause":[{"type":"parsing_exception","reason":"[match_all] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":26}],"type":"parsing_exception","reason":"[match_all] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":26},"status":400}",

This is an example of the search parameters that I'm building and sending to the elastic client.

[
  "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
  ]
]

I can't seem to figure out how to use both of them. Any pointers? Thank you.

1

1 Answers

5
votes

You will need to wrap your query in a bool query like this:

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

Just wrap the bool and a must query around your match_all and it should work.

I don't know the exact PHP syntax but it should be something like this:

[
  "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
  ]
]

For reference see the docs Elasticsearch Reference [7.0] » Query DSL » Compound queries » Bool Query, it contains an example like yours with match_all combined with filters.