121

I have a query which I need to filter out results.

This is my query

{
    "query": {
        "filtered": {
            "query": {
                "multi_match": {
                    "default_operator": "AND",
                    "fields": [
                        "author",
                        "title",
                        "publisher",
                        "year"
                    ],
                    "query": "George Orwell"
                }
            },
            "filter": {
                "terms": {
                    "year": [
                        1980,
                        1981
                    ]
                }
            }
        }
    }
}

I get an error saying no [query] registered for [filtered]. I clearly have a query for the filtered field. I am following the format given in the filtered query documentation on the elasticsearch page. https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html

devxeq
  • 1,329
  • 2
  • 8
  • 15

1 Answers1

201

The filtered query has been deprecated and removed in ES 5.0. You should now use the bool/must/filter query instead.

{
    "query": {
        "bool": {
            "must": {
                "multi_match": {
                    "operator": "and",
                    "fields": [
                        "author",
                        "title",
                        "publisher",
                        "year"
                    ],
                    "query": "George Orwell"
                }
            },
            "filter": {
                "terms": {
                    "year": [
                        1980,
                        1981
                    ]
                }
            }
        }
    }
}

Here are the differences between the two queries:

3,4c3,4
<         "bool": {
<             "must": {
---
>         "filtered": {
>             "query": {
6c6
<                     "operator": "and",
---
>                     "default_operator": "AND",

PS: the reference page you're looking at is located in the "deleted pages" of the appendix, so it's not part of the main documentation anymore.

hughdbrown
  • 47,733
  • 20
  • 85
  • 108
Val
  • 207,596
  • 13
  • 358
  • 360
  • 4
    When using 5.5 it seems default_operator has been removed from the multi_match query as well val so it maybe worth updating this answer as it led me down the wrong road assuming the above was going to work i amended the query to match my fields to then realise it didn't work! It was a simple fix for me though bu just removing the default operator but maybe worth editing for future readers, The error is: [multi_match] query does not support [default_operator] – Birdy Aug 03 '17 at 02:15
  • 3
    @Birdy actually the parameter is named `operator` and not `default_operator`, I've amended my answer, sorry about that. – Val Aug 03 '17 at 04:31
  • Thanks, this worked for me when upgrading an ES5.1 instance to 7.1 – jhob101 Feb 07 '20 at 17:13