50

I have a multi-match query in ES, and wish to add a filter.

{
  "multi_match" : {
    "query" : "this is a test",
    "fields" : [ "subject^2", "message" ]
  }
}

What is the syntax for adding this filter?

I tried:

{
  "multi_match" => {
    "query" => "list",
    "fields" => [ "username" ]

  },
"filter" => {
        "term" => { "username" => "slimkicker"}
    }
}
Henley
  • 21,258
  • 32
  • 119
  • 207

4 Answers4

77

Depending on what you need you have to put the filter in the proper position. You have two options:

Use a top-level filter and apply the filter only to the search results but not to the facets

{
    "query" : {
        "multi_match" : {
            "query" : "this is a test",
            "fields" : [ "subject^2", "message" ]
        }
    },
    "filter" : {
        "term" : { "username": "slimkicker" }
    }
} 

Use a filtered query and apply the filter to both the search results and the facets

{
    "query" : {
        "filtered" : {
            "query" : {
                "multi_match" : {
                    "query" : "this is a test",
                    "fields" : [ "subject^2", "message" ]
                }
            },
            "filter" : {
                "term" : { "username": "slimkicker" }
            }
        }
    }
}
w00ngy
  • 1,646
  • 21
  • 25
javanna
  • 59,145
  • 14
  • 144
  • 125
  • 12
    Could you please explain little more on difference between two options? in which use cases should they be used? – Krishna Shetty Dec 17 '15 at 10:29
  • 1
    is the 2nd one about filters on the side like amazon.com eg.? – Emil May 20 '16 at 14:03
  • 4
    In ElasticSearch 7.6 this query does not work:`parsing_exception: Unknown key for a START_OBJECT in [filter].` I don't think you can put the filter outside the query. – Marc Apr 15 '20 at 09:04
  • Facets have been removed. See https://www.elastic.co/guide/en/elasticsearch/reference/6.8/search-facets-query-facet.html https://www.elastic.co/guide/en/elasticsearch/reference/6.8/search-facets-filter-facet.html – Max Starling May 02 '20 at 18:18
  • @w00ngy can you explain why use field’s score on `[ "subject^2", "message" ]` – Sayem Jul 05 '21 at 15:14
69

With Elasticsearch 5 the syntax has changed to the usage of bool query, e.g.

{
  "from" : 0,
  "size" : 10,
  "sort" : "publishDate",
  "query": {
    "bool": {  
      "must" : {
        "multi_match" : {
          "query":      "wedding",
          "type":       "most_fields",
          "fields":     [ "title", "text" ]
        }
      },
      "filter": {
        "term": {
          "locale": "english"
        }
      }
    }
  }
}

Documentation can be found here.

Fritz Duchardt
  • 11,026
  • 4
  • 41
  • 60
9

As per the new Documentation of Elasticsearch, the format is little bit changed, now you have to use bool and must and can apply filter separately from query like follows,

{
    'index' : 'users',
        'type' : 'users',
        'body' : {
          "query" : {
            "bool" : {
              "must" : {
                'multi_match' : {
                    'fields' : {'source^1', 'first_name^5', 'last_name^4', 'email^3', 'postcode^2', 'telephone', 'address', 'alternate_address'
                    },
                    'query' : 'Shahrukh Anwar',
                },
              },
              "filter" : {
                "term" : {
                  'assigned_to' : 125
                }
              }
            }
          }
        }
}
Shahrukh Anwar
  • 2,544
  • 1
  • 24
  • 24
0

Try this:

[
        "index" => 'shop_1', //index_name,
        "type" => 'shop', //type_name,
        "from" => 0, //offset
        "size" => 30, //limit
        "body" => [
            "query" => [
                "bool" => [
                    "must" =>   [
                                    "match" => [
                                        "category" => 'men' // results must contain 'men' in category field
                                    ]
                                ]
                ]
            ],
            "_source" => [ // fields to retrieve
                            "id",
                            "product_id",
                            "item_title",
                            "item_slug",
                            "item_sku"
                        ],
            "sort" => [
                [
                    "_score" => [
                        "order" => "desc" // order by score desc
                    ]
                ]
            ]

      ],
];
Selim Reza
  • 683
  • 1
  • 11
  • 31