Skip to content

Commit cd55995

Browse files
committed
[DOCS] Rewrite terms query (#42889)
1 parent 27b7f10 commit cd55995

File tree

2 files changed

+212
-81
lines changed

2 files changed

+212
-81
lines changed

docs/reference/index-modules.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ specific index module:
183183
This setting is only applicable when highlighting is requested on a text that was indexed without offsets or term vectors.
184184
By default this settings is unset in 6.x, defaults to `-1`.
185185

186+
[[index-max-terms-count]]
186187
`index.max_terms_count`::
187188

188189
The maximum number of terms that can be used in Terms Query.
Lines changed: 211 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,255 @@
11
[[query-dsl-terms-query]]
22
=== Terms Query
33

4-
Filters documents that have fields that match any of the provided terms
5-
(*not analyzed*). For example:
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`.
614

715
[source,js]
8-
--------------------------------------------------
16+
----
917
GET /_search
1018
{
11-
"query": {
12-
"terms" : { "user" : ["kimchy", "elasticsearch"]}
19+
"query" : {
20+
"terms" : {
21+
"user" : ["kimchy", "elasticsearch"],
22+
"boost" : 1.0
23+
}
1324
}
1425
}
15-
--------------------------------------------------
26+
----
1627
// CONSOLE
1728

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.
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
2174

22-
[float]
2375
[[query-dsl-terms-lookup]]
24-
===== Terms lookup mechanism
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.
2580

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.
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.
3284

33-
The terms lookup mechanism supports the following options:
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.
3489

35-
[horizontal]
90+
To perform a terms lookup, use the following parameters.
91+
92+
[[query-dsl-terms-lookup-params]]
93+
====== Terms lookup parameters
3694
`index`::
37-
The index to fetch the term values from.
95+
Name of the index from which to fetch field values.
3896

3997
`type`::
4098
The type to fetch the term values from.
4199

42100
`id`::
43-
The id of the document to fetch the term values from.
101+
<<mapping-id-field,ID>> of the document from which to fetch field values.
44102

45103
`path`::
46-
The field specified as path to fetch the actual values for the
47-
`terms` filter.
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+
--
48112

49113
`routing`::
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.
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+
--
77126

78127
[source,js]
79-
--------------------------------------------------
80-
PUT /users/_doc/2
128+
----
129+
PUT my_index
81130
{
82-
"followers" : ["1", "3"]
131+
"mappings" : {
132+
"properties" : {
133+
"color" : { "type" : "keyword" }
134+
}
135+
}
83136
}
137+
----
138+
// CONSOLE
139+
--
84140

85-
PUT /tweets/_doc/1
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
86149
{
87-
"user" : "1"
150+
"color": ["blue", "green"]
88151
}
152+
----
153+
// CONSOLE
154+
// TEST[continued]
155+
--
89156

90-
GET /tweets/_search
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
91165
{
92-
"query" : {
93-
"terms" : {
94-
"user" : {
95-
"index" : "users",
96-
"type" : "_doc",
97-
"id" : "2",
98-
"path" : "followers"
99-
}
100-
}
101-
}
166+
"color": "blue"
102167
}
103-
--------------------------------------------------
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+
181+
[source,js]
182+
----
183+
POST my_index/_refresh
184+
----
104185
// CONSOLE
186+
// TEST[continued]
105187
106-
The structure of the external terms document can also include an array of
107-
inner objects, for example:
188+
////
108189

109190
[source,js]
110-
--------------------------------------------------
111-
PUT /users/_doc/2
191+
----
192+
GET my_index/_search?pretty
112193
{
113-
"followers" : [
114-
{
115-
"id" : "1"
116-
},
117-
{
118-
"id" : "2"
119-
}
120-
]
194+
"query": {
195+
"terms": {
196+
"color" : {
197+
"index" : "my_index",
198+
"id" : "2",
199+
"path" : "color"
200+
}
201+
}
202+
}
121203
}
122-
--------------------------------------------------
204+
----
123205
// CONSOLE
206+
// TEST[continued]
207+
208+
Because document 2 and document 1 both contain `blue` as a value in the `color`
209+
field, {es} returns both documents.
124210

125-
In which case, the lookup path will be `followers.id`.
211+
[source,js]
212+
----
213+
{
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+
}
252+
}
253+
----
254+
// TESTRESPONSE[s/"took" : 17/"took" : $body.took/]
255+
--

0 commit comments

Comments
 (0)