|
1 | 1 | [[query-dsl-terms-query]]
|
2 | 2 | === Terms Query
|
3 | 3 |
|
4 |
| -Returns documents that contain one or more *exact* terms in a provided field. |
5 |
| - |
6 |
| -The `terms` query is the same as the <<query-dsl-term-query, `term` query>>, |
7 |
| -except you can search for multiple values. |
8 |
| - |
9 |
| -[[terms-query-ex-request]] |
10 |
| -==== Example request |
11 |
| - |
12 |
| -The following search returns documents where the `user` field contains `kimchy` |
13 |
| -or `elasticsearch`. |
| 4 | +Filters documents that have fields that match any of the provided terms |
| 5 | +(*not analyzed*). For example: |
14 | 6 |
|
15 | 7 | [source,js]
|
16 |
| ----- |
| 8 | +-------------------------------------------------- |
17 | 9 | GET /_search
|
18 | 10 | {
|
19 |
| - "query" : { |
20 |
| - "terms" : { |
21 |
| - "user" : ["kimchy", "elasticsearch"], |
22 |
| - "boost" : 1.0 |
23 |
| - } |
| 11 | + "query": { |
| 12 | + "terms" : { "user" : ["kimchy", "elasticsearch"]} |
24 | 13 | }
|
25 | 14 | }
|
26 |
| ----- |
| 15 | +-------------------------------------------------- |
27 | 16 | // CONSOLE
|
28 | 17 |
|
29 |
| -[[terms-top-level-params]] |
30 |
| -==== Top-level parameters for `terms` |
31 |
| -`<field>`:: |
32 |
| -+ |
33 |
| --- |
34 |
| -Field you wish to search. |
35 |
| - |
36 |
| -The value of this parameter is an array of terms you wish to find in the |
37 |
| -provided field. To return a document, one or more terms must exactly match a |
38 |
| -field value, including whitespace and capitalization. |
39 |
| - |
40 |
| -By default, {es} limits the `terms` query to a maximum of 65,536 |
41 |
| -terms. You can change this limit using the <<index-max-terms-count, |
42 |
| -`index.max_terms_count`>> setting. |
43 |
| - |
44 |
| -[NOTE] |
45 |
| -To use the field values of an existing document as search terms, use the |
46 |
| -<<query-dsl-terms-lookup, terms lookup>> parameters. |
47 |
| --- |
48 |
| - |
49 |
| -`boost`:: |
50 |
| -+ |
51 |
| --- |
52 |
| -Floating point number used to decrease or increase the |
53 |
| -<<query-filter-context, relevance scores>> of a query. Default is `1.0`. |
54 |
| -Optional. |
55 |
| - |
56 |
| -You can use the `boost` parameter to adjust relevance scores for searches |
57 |
| -containing two or more queries. |
58 |
| - |
59 |
| -Boost values are relative to the default value of `1.0`. A boost value between |
60 |
| -`0` and `1.0` decreases the relevance score. A value greater than `1.0` |
61 |
| -increases the relevance score. |
62 |
| --- |
63 |
| - |
64 |
| -[[terms-query-notes]] |
65 |
| -==== Notes |
66 |
| - |
67 |
| -[[query-dsl-terms-query-highlighting]] |
68 |
| -===== Highlighting `terms` queries |
69 |
| -<<search-request-highlighting,Highlighting>> is best-effort only. {es} may not |
70 |
| -return highlight results for `terms` queries depending on: |
71 |
| - |
72 |
| -* Highlighter type |
73 |
| -* Number of terms in the query |
| 18 | +NOTE: Highlighting `terms` queries is best-effort only, so terms of a `terms` |
| 19 | +query might not be highlighted depending on the highlighter implementation that |
| 20 | +is selected and on the number of terms in the `terms` query. |
74 | 21 |
|
| 22 | +[float] |
75 | 23 | [[query-dsl-terms-lookup]]
|
76 |
| -===== Terms lookup |
77 |
| -Terms lookup fetches the field values of an existing document. {es} then uses |
78 |
| -those values as search terms. This can be helpful when searching for a large set |
79 |
| -of terms. |
| 24 | +===== Terms lookup mechanism |
80 | 25 |
|
81 |
| -Because terms lookup fetches values from a document, the <<mapping-source-field, |
82 |
| -`_source`>> mapping field must be enabled to use terms lookup. The `_source` |
83 |
| -field is enabled by default. |
| 26 | +When it's needed to specify a `terms` filter with a lot of terms it can |
| 27 | +be beneficial to fetch those term values from a document in an index. A |
| 28 | +concrete example would be to filter tweets tweeted by your followers. |
| 29 | +Potentially the amount of user ids specified in the terms filter can be |
| 30 | +a lot. In this scenario it makes sense to use the terms filter's terms |
| 31 | +lookup mechanism. |
84 | 32 |
|
85 |
| -[NOTE] |
86 |
| -By default, {es} limits the `terms` query to a maximum of 65,536 |
87 |
| -terms. This includes terms fetched using terms lookup. You can change |
88 |
| -this limit using the <<index-max-terms-count, `index.max_terms_count`>> setting. |
| 33 | +The terms lookup mechanism supports the following options: |
89 | 34 |
|
90 |
| -To perform a terms lookup, use the following parameters. |
91 |
| - |
92 |
| -[[query-dsl-terms-lookup-params]] |
93 |
| -====== Terms lookup parameters |
| 35 | +[horizontal] |
94 | 36 | `index`::
|
95 |
| -Name of the index from which to fetch field values. |
| 37 | + The index to fetch the term values from. |
96 | 38 |
|
97 | 39 | `type`::
|
98 | 40 | The type to fetch the term values from.
|
99 | 41 |
|
100 | 42 | `id`::
|
101 |
| -<<mapping-id-field,ID>> of the document from which to fetch field values. |
| 43 | + The id of the document to fetch the term values from. |
102 | 44 |
|
103 | 45 | `path`::
|
104 |
| -+ |
105 |
| --- |
106 |
| -Name of the field from which to fetch field values. {es} uses |
107 |
| -these values as search terms for the query. |
108 |
| - |
109 |
| -If the field values include an array of nested inner objects, you can access |
110 |
| -those objects using dot notation syntax. |
111 |
| --- |
| 46 | + The field specified as path to fetch the actual values for the |
| 47 | + `terms` filter. |
112 | 48 |
|
113 | 49 | `routing`::
|
114 |
| -Custom <<mapping-routing-field, routing value>> of the document from which to |
115 |
| -fetch term values. If a custom routing value was provided when the document was |
116 |
| -indexed, this parameter is required. |
117 |
| - |
118 |
| -[[query-dsl-terms-lookup-example]] |
119 |
| -====== Terms lookup example |
120 |
| - |
121 |
| -To see how terms lookup works, try the following example. |
122 |
| - |
123 |
| -. Create an index with a `keyword` field named `color`. |
124 |
| -+ |
125 |
| --- |
| 50 | + A custom routing value to be used when retrieving the |
| 51 | + external terms doc. |
| 52 | + |
| 53 | +The values for the `terms` filter will be fetched from a field in a |
| 54 | +document with the specified id in the specified type and index. |
| 55 | +Internally a get request is executed to fetch the values from the |
| 56 | +specified path. At the moment for this feature to work the `_source` |
| 57 | +needs to be stored. |
| 58 | + |
| 59 | +Also, consider using an index with a single shard and fully replicated |
| 60 | +across all nodes if the "reference" terms data is not large. The lookup |
| 61 | +terms filter will prefer to execute the get request on a local node if |
| 62 | +possible, reducing the need for networking. |
| 63 | + |
| 64 | +[WARNING] |
| 65 | +Executing a Terms Query request with a lot of terms can be quite slow, |
| 66 | +as each additional term demands extra processing and memory. |
| 67 | +To safeguard against this, the maximum number of terms that can be used |
| 68 | +in a Terms Query both directly or through lookup has been limited to `65536`. |
| 69 | +This default maximum can be changed for a particular index with the index setting |
| 70 | + `index.max_terms_count`. |
| 71 | + |
| 72 | +[float] |
| 73 | +===== Terms lookup twitter example |
| 74 | +At first we index the information for user with id 2, specifically, its |
| 75 | +followers, then index a tweet from user with id 1. Finally we search on |
| 76 | +all the tweets that match the followers of user 2. |
126 | 77 |
|
127 | 78 | [source,js]
|
128 |
| ----- |
129 |
| -PUT my_index |
| 79 | +-------------------------------------------------- |
| 80 | +PUT /users/_doc/2 |
130 | 81 | {
|
131 |
| - "mappings" : { |
132 |
| - "properties" : { |
133 |
| - "color" : { "type" : "keyword" } |
134 |
| - } |
135 |
| - } |
| 82 | + "followers" : ["1", "3"] |
136 | 83 | }
|
137 |
| ----- |
138 |
| -// CONSOLE |
139 |
| --- |
140 | 84 |
|
141 |
| -. Index a document with an ID of 1 and values of `["blue", "green"]` in the |
142 |
| -`color` field. |
143 |
| -+ |
144 |
| --- |
145 |
| - |
146 |
| -[source,js] |
147 |
| ----- |
148 |
| -PUT my_index/_doc/1 |
| 85 | +PUT /tweets/_doc/1 |
149 | 86 | {
|
150 |
| - "color": ["blue", "green"] |
| 87 | + "user" : "1" |
151 | 88 | }
|
152 |
| ----- |
153 |
| -// CONSOLE |
154 |
| -// TEST[continued] |
155 |
| --- |
156 |
| - |
157 |
| -. Index another document with an ID of 2 and value of `blue` in the `color` |
158 |
| -field. |
159 |
| -+ |
160 |
| --- |
161 |
| - |
162 |
| -[source,js] |
163 |
| ----- |
164 |
| -PUT my_index/_doc/2 |
165 |
| -{ |
166 |
| - "color": "blue" |
167 |
| -} |
168 |
| ----- |
169 |
| -// CONSOLE |
170 |
| -// TEST[continued] |
171 |
| --- |
172 |
| - |
173 |
| -. Use the `terms` query with terms lookup parameters to find documents |
174 |
| -containing one or more of the same terms as document 2. Include the `pretty` |
175 |
| -parameter so the response is more readable. |
176 |
| -+ |
177 |
| --- |
178 |
| - |
179 |
| -//// |
180 | 89 |
|
181 |
| -[source,js] |
182 |
| ----- |
183 |
| -POST my_index/_refresh |
184 |
| ----- |
185 |
| -// CONSOLE |
186 |
| -// TEST[continued] |
187 |
| -
|
188 |
| -//// |
189 |
| - |
190 |
| -[source,js] |
191 |
| ----- |
192 |
| -GET my_index/_search?pretty |
| 90 | +GET /tweets/_search |
193 | 91 | {
|
194 |
| - "query": { |
195 |
| - "terms": { |
196 |
| - "color" : { |
197 |
| - "index" : "my_index", |
198 |
| - "id" : "2", |
199 |
| - "path" : "color" |
| 92 | + "query" : { |
| 93 | + "terms" : { |
| 94 | + "user" : { |
| 95 | + "index" : "users", |
| 96 | + "type" : "_doc", |
| 97 | + "id" : "2", |
| 98 | + "path" : "followers" |
| 99 | + } |
200 | 100 | }
|
201 | 101 | }
|
202 |
| - } |
203 | 102 | }
|
204 |
| ----- |
| 103 | +-------------------------------------------------- |
205 | 104 | // CONSOLE
|
206 |
| -// TEST[continued] |
207 | 105 |
|
208 |
| -Because document 2 and document 1 both contain `blue` as a value in the `color` |
209 |
| -field, {es} returns both documents. |
| 106 | +The structure of the external terms document can also include an array of |
| 107 | +inner objects, for example: |
210 | 108 |
|
211 | 109 | [source,js]
|
212 |
| ----- |
| 110 | +-------------------------------------------------- |
| 111 | +PUT /users/_doc/2 |
213 | 112 | {
|
214 |
| - "took" : 17, |
215 |
| - "timed_out" : false, |
216 |
| - "_shards" : { |
217 |
| - "total" : 1, |
218 |
| - "successful" : 1, |
219 |
| - "skipped" : 0, |
220 |
| - "failed" : 0 |
221 |
| - }, |
222 |
| - "hits" : { |
223 |
| - "total" : { |
224 |
| - "value" : 2, |
225 |
| - "relation" : "eq" |
226 |
| - }, |
227 |
| - "max_score" : 1.0, |
228 |
| - "hits" : [ |
229 |
| - { |
230 |
| - "_index" : "my_index", |
231 |
| - "_type" : "_doc", |
232 |
| - "_id" : "1", |
233 |
| - "_score" : 1.0, |
234 |
| - "_source" : { |
235 |
| - "color" : [ |
236 |
| - "blue", |
237 |
| - "green" |
238 |
| - ] |
239 |
| - } |
240 |
| - }, |
241 |
| - { |
242 |
| - "_index" : "my_index", |
243 |
| - "_type" : "_doc", |
244 |
| - "_id" : "2", |
245 |
| - "_score" : 1.0, |
246 |
| - "_source" : { |
247 |
| - "color" : "blue" |
248 |
| - } |
249 |
| - } |
250 |
| - ] |
251 |
| - } |
| 113 | + "followers" : [ |
| 114 | + { |
| 115 | + "id" : "1" |
| 116 | + }, |
| 117 | + { |
| 118 | + "id" : "2" |
| 119 | + } |
| 120 | + ] |
252 | 121 | }
|
253 |
| ----- |
254 |
| -// TESTRESPONSE[s/"took" : 17/"took" : $body.took/] |
255 |
| --- |
| 122 | +-------------------------------------------------- |
| 123 | +// CONSOLE |
| 124 | + |
| 125 | +In which case, the lookup path will be `followers.id`. |
0 commit comments