summaryrefslogtreecommitdiff
path: root/docs/reference/search/request/named-queries-and-filters.asciidoc
blob: c28baec7158e545a27829c1d6d05564dc0ba33b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
[[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_filters` 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 as available from version `0.90.4` and the support on filters is available
also in versions before `0.90.4`.