[[search-request-named-queries-and-filters]]
=== Named Queries and Filters

Each filter and query can accept a `_name` in its top level definition.

[source,js]
--------------------------------------------------
{
    "filtered" : {
        "query" : {
            "bool" : {
                "should" : [
                    {"match" : { "name.first" : {"query" : "shay", "_name" : "first"} }},
                    {"match" : { "name.last" : {"query" : "banon", "_name" : "last"} }}
                ]
            }
        },
        "filter" : {
            "terms" : {
                "name.last" : ["banon", "kimchy"],
                "_name" : "test"
            }
        }
    }
}
--------------------------------------------------

The search response will include for each hit the `matched_queries` it matched on. The tagging of queries and filters
only make sense for compound queries and filters (such as `bool` query and filter, `or` and `and` filter, `filtered` query etc.).

Note, the query filter had to be enhanced in order to support this. In
order to set a name, the `fquery` filter should be used, which wraps a
query (just so there will be a place to set a name for it), for example:

[source,js]
--------------------------------------------------
{
    "filtered" : {
        "query" : {
            "term" : { "name.first" : "shay" }
        },
        "filter" : {
            "fquery" : {
                "query" : {
                    "term" : { "name.last" : "banon" }
                },
                "_name" : "test"
            }
        }
    }
}
--------------------------------------------------

==== Named queries

The support for the `_name` option on queries is available from version `0.90.4` and the support on filters is available
also in versions before `0.90.4`.
