Skip to content

Commit ed44215

Browse files
committed
[DOCS] Re-add term vs. match query example
1 parent d10254a commit ed44215

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed

docs/reference/query-dsl/term-query.asciidoc

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ GET /_search
3737
----
3838
// CONSOLE
3939

40+
[[term-top-level-params]]
4041
==== Top-level parameters for `term`
4142
`<field>`::
4243
Field you wish to search.
4344

45+
[[term-field-params]]
4446
==== Parameters for `<field>`
4547
`value`::
4648
Term you wish to find in the provided `<field>`. To return a document, the term
@@ -58,3 +60,151 @@ Boost values are relative to the default value of `1.0`. A boost value between
5860
`0` and `1.0` decreases the relevance score. A value greater than `1.0`
5961
increases the relevance score.
6062

63+
[[term-query-notes]]
64+
==== Notes
65+
66+
[[avoid-term-query-text-fields]]
67+
===== Avoid using the `term` query for `text` fields
68+
By default, {es} changes the values of `text` fields during analysis. For
69+
example, the default <<analysis-standard-analyzer, standard analyzer>> changes
70+
`text` field values as follows:
71+
72+
* Removes most punctuation
73+
* Divides the remaining content into individual words, called
74+
<<analysis-tokenizers, tokens>>
75+
* Lowercases the tokens
76+
77+
To better search `text` fields, the `match` query also analyzes your provided
78+
search term before performing a search. This means the `match` query can search
79+
`text` fields for analyzed tokens rather than an exact term.
80+
81+
The `term` query does *not* analyze the search term. The `term` query only
82+
searches for the *exact* term you provide. This means the `term` query may
83+
return poor or no results when searching `text` fields.
84+
85+
To see the difference in search results, try the following example.
86+
87+
. Create an index with a `text` field called `full_text`.
88+
+
89+
--
90+
91+
[source,js]
92+
----
93+
PUT my_index
94+
{
95+
"mappings" : {
96+
"properties" : {
97+
"full_text" : { "type" : "text" }
98+
}
99+
}
100+
}
101+
----
102+
// CONSOLE
103+
104+
--
105+
106+
. Index a document with a value of `Quick Brown Foxes!` in the `full_text`
107+
field.
108+
+
109+
--
110+
111+
[source,js]
112+
----
113+
PUT my_index/_doc/1
114+
{
115+
"full_text": "Quick Brown Foxes!"
116+
}
117+
----
118+
// CONSOLE
119+
// TEST[continued]
120+
121+
Because `full_text` is a `text` field, {es} changes `Quick Brown Foxes!` to
122+
`[quick, brown, fox]` during analysis.
123+
124+
--
125+
126+
. Use the `term` query to search for `Quick Brown Foxes!` in the `full_text`
127+
field. Include the `pretty` parameter so the response is more readable.
128+
+
129+
--
130+
131+
[source,js]
132+
----
133+
GET my_index/_search?pretty
134+
{
135+
"query": {
136+
"term": {
137+
"full_text": "Quick Brown Foxes!"
138+
}
139+
}
140+
}
141+
----
142+
// CONSOLE
143+
// TEST[continued]
144+
145+
Because the `full_text` field no longer contains the *exact* term `Quick Brown
146+
Foxes!`, the `term` query search returns no results.
147+
148+
--
149+
150+
. Use the `match` query to search for `Quick Brown Foxes!` in the `full_text`
151+
field.
152+
+
153+
--
154+
155+
[source,js]
156+
----
157+
GET my_index/_search?pretty
158+
{
159+
"query": {
160+
"match": {
161+
"full_text": "Quick Brown Foxes!"
162+
}
163+
}
164+
}
165+
----
166+
// CONSOLE
167+
// TEST[continued]
168+
169+
Unlike the `term` query, the `match` query analyzes your provided search term,
170+
`Quick Brown Foxes!`, before performing a search. The `match` query then returns
171+
any documents containing the `quick`, `brown`, or `fox` tokens in the
172+
`full_text` field.
173+
174+
Here's the response for the `match` query search containing the indexed document
175+
in the results.
176+
177+
[source,js]
178+
----
179+
{
180+
"took" : 1,
181+
"timed_out" : false,
182+
"_shards" : {
183+
"total" : 1,
184+
"successful" : 1,
185+
"skipped" : 0,
186+
"failed" : 0
187+
},
188+
"hits" : {
189+
"total" : {
190+
"value" : 1,
191+
"relation" : "eq"
192+
},
193+
"max_score" : 0.8630463,
194+
"hits" : [
195+
{
196+
"_index" : "my_index",
197+
"_type" : "_doc",
198+
"_id" : "1",
199+
"_score" : 0.8630463,
200+
"_source" : {
201+
"full_text" : "Quick Brown Foxes!"
202+
}
203+
}
204+
]
205+
}
206+
}
207+
----
208+
// TESTRESPONSE[s/1/$body.took/]
209+
210+
--

0 commit comments

Comments
 (0)