|
| 1 | + |
| 2 | +=== Filtering Aggregations |
| 3 | + |
| 4 | +A natural extension to aggregation scoping is filtering. Because the aggregation |
| 5 | +operates in the context of the query scope, any filter applied to the query |
| 6 | +will also apply to the aggregation. |
| 7 | + |
| 8 | +==== Filtered Query |
| 9 | +If we want to find all cars over $10,000 and also calculate the average price |
| 10 | +for those cars, we can simply use a `filtered` query: |
| 11 | + |
| 12 | +[source,js] |
| 13 | +-------------------------------------------------- |
| 14 | +GET /cars/transactions/_search?search_type=count |
| 15 | +{ |
| 16 | + "query" : { |
| 17 | + "filtered": { |
| 18 | + "range": { |
| 19 | + "price": { |
| 20 | + "gte": 10000 |
| 21 | + } |
| 22 | + } |
| 23 | + } |
| 24 | + }, |
| 25 | + "aggs" : { |
| 26 | + "single_avg_price": { |
| 27 | + "avg" : { "field" : "price" } |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | +-------------------------------------------------- |
| 32 | +// SENSE: 300_Aggregations/45_filtering.json |
| 33 | + |
| 34 | +Fundamentally, using a `filtered` query is no different from using a `match` |
| 35 | +query like we discussed in the last section. The query (which happens to include |
| 36 | +a filter) returns a certain subset of documents, and the aggregation operates |
| 37 | +on those documents. |
| 38 | + |
| 39 | +==== Filter bucket |
| 40 | + |
| 41 | +But what if you would like to filter just the aggregation results? Imagine we |
| 42 | +have are building the search page for our car dealership. We want to display |
| 43 | +search results according to what the user searches for. But we also want |
| 44 | +to enrich the page by including the average price of cars (matching the search) |
| 45 | +which were sold in the last month. |
| 46 | + |
| 47 | +We can't use simple scoping here, since there are two different criteria. The |
| 48 | +search results must match "ford", but the aggregation results must match "ford" |
| 49 | +AND "sold > now - 1M". |
| 50 | + |
| 51 | +To solve this problem, we can use a special bucket called `filter`. You specify |
| 52 | +a filter, and when documents match the filter's criteria, they are added to the |
| 53 | +bucket. |
| 54 | + |
| 55 | +Here is the resulting query: |
| 56 | + |
| 57 | +[source,js] |
| 58 | +-------------------------------------------------- |
| 59 | +GET /cars/transactions/_search?search_type=count |
| 60 | +{ |
| 61 | + "query":{ |
| 62 | + "match": { |
| 63 | + "make": "ford" |
| 64 | + } |
| 65 | + }, |
| 66 | + "aggs":{ |
| 67 | + "recent_sales": { |
| 68 | + "filter": { <1> |
| 69 | + "range": { |
| 70 | + "sold": { |
| 71 | + "from": "now-1M" |
| 72 | + } |
| 73 | + } |
| 74 | + }, |
| 75 | + "aggs": { |
| 76 | + "average_price":{ |
| 77 | + "avg": { |
| 78 | + "field": "price" <2> |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | +-------------------------------------------------- |
| 86 | +// SENSE: 300_Aggregations/45_filtering.json |
| 87 | +<1> Using the `filter` bucket to apply a filter in addition to the `query` scope |
| 88 | +<2> This `avg` metric will therefore only average docs which are both "ford" and sold in the last month |
| 89 | + |
| 90 | +Since the `filter` bucket operates like any other bucket, you are free to nest |
| 91 | +other buckets and metrics inside. All nested components will "inherit" the filter. |
| 92 | +This allows you to filter selective portions of the aggregation as required. |
| 93 | + |
| 94 | +==== Post Filter |
| 95 | + |
| 96 | +So far, we have a way to filter the both search results and aggregations (a |
| 97 | +`filtered` query), as well as filtering individual portions of the aggregation |
| 98 | +(`filter` bucket). |
| 99 | + |
| 100 | +You may be thinking to yourself "hmm...is there a way to filter _just_ the search |
| 101 | +results but not the aggregation?". The answer is to use a `post_filter`. |
| 102 | + |
| 103 | +This is a top-level search request element which accepts a filter. The filter is |
| 104 | +applied _after_ the query has executed (hence the "post" moniker...it runs |
| 105 | +_post query_ execution). Because it operates after the query has executed, |
| 106 | +it does not affect the query scope...and thus does not affect the aggregations |
| 107 | +either. |
| 108 | + |
| 109 | +We can use this behavior to apply additional filters to our search |
| 110 | +criteria that don't affect things like categorical facets in your UI. Let's |
| 111 | +design another search page for our car dealer. This page will allow the user |
| 112 | +to search for a car and filter by color. Color choices are populated via an |
| 113 | +aggregation. |
| 114 | + |
| 115 | +[source,js] |
| 116 | +-------------------------------------------------- |
| 117 | +GET /cars/transactions/_search?search_type=count |
| 118 | +{ |
| 119 | + "query": { |
| 120 | + "match": { |
| 121 | + "make": "ford" |
| 122 | + } |
| 123 | + }, |
| 124 | + "post_filter": { <1> |
| 125 | + "term" : { |
| 126 | + "color" : "green" |
| 127 | + } |
| 128 | + }, |
| 129 | + "aggs" : { |
| 130 | + "all_colors": { |
| 131 | + "terms" : { "field" : "color" } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | +-------------------------------------------------- |
| 136 | +// SENSE: 300_Aggregations/45_filtering.json |
| 137 | +<1> The `post_filter` element is a "top-level" element and filters just the search hits |
| 138 | + |
| 139 | +The `query` portion is finding all "ford" cars. We are then building a list of |
| 140 | +colors with a `terms` aggregation. Because aggregations operate in the query |
| 141 | +scope, the list of colors will correspond with the colors that Ford cars are |
| 142 | +painted. |
| 143 | + |
| 144 | +Finally, the `post_filter` will filter the search results to show only green |
| 145 | +"ford" cars. This happens _after_ the query is executed, so the aggregations |
| 146 | +are unaffected. |
| 147 | + |
| 148 | +This is often important for coherent UIs. Imagine a user clicks a category in |
| 149 | +your UI (e.g. "green"). The expectation is that the search results are filtered, |
| 150 | +but _not_ the UI options. If you applied a `filtered` query, the UI would |
| 151 | +instantly transform to show _only_ "green" as an option...not what the user wants! |
| 152 | + |
| 153 | +[WARNING] |
| 154 | +.Performance consideration |
| 155 | +==== |
| 156 | +_Only_ use a `post_filter` if you need to differentially filter search results |
| 157 | +and aggregations. Sometimes people will use `post_filter` for regular searches. |
| 158 | +
|
| 159 | +Don't do this! The nature of the `post_filter` means it runs _after_ the query, |
| 160 | +so any performance benefit of filtering (caches, etc) is lost completely. |
| 161 | +
|
| 162 | +The `post_filter` should only be used in combination with aggregations, and only |
| 163 | +when you need differential filtering. |
| 164 | +==== |
| 165 | + |
| 166 | +==== Recap |
| 167 | + |
| 168 | +Choosing the appropriate type of filtering -- search hits, aggregations or |
| 169 | +both -- often boils down to how you want your user interface to behave. Choose |
| 170 | +the appropriate filter (or combinations) depending on how you want to display |
| 171 | +results to your user. |
| 172 | + |
| 173 | + - `filtered` query: affects both search results and aggregations |
| 174 | + - `filter` bucket: affects just aggregations |
| 175 | + - `post_filter`: affects just search results |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | + |
| 181 | + |
0 commit comments