2013-08-28 19:24:34 -04:00
|
|
|
[[query-dsl-term-query]]
|
2019-07-18 10:18:11 -04:00
|
|
|
=== Term query
|
|
|
|
++++
|
|
|
|
<titleabbrev>Term</titleabbrev>
|
|
|
|
++++
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-05-06 10:36:07 -04:00
|
|
|
Returns documents that contain an *exact* term in a provided field.
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-05-06 10:36:07 -04:00
|
|
|
You can use the `term` query to find documents based on a precise value such as
|
|
|
|
a price, a product ID, or a username.
|
|
|
|
|
|
|
|
[WARNING]
|
|
|
|
====
|
|
|
|
Avoid using the `term` query for <<text, `text`>> fields.
|
|
|
|
|
|
|
|
By default, {es} changes the values of `text` fields as part of <<analysis,
|
|
|
|
analysis>>. This can make finding exact matches for `text` field values
|
|
|
|
difficult.
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-05-06 10:36:07 -04:00
|
|
|
To search `text` field values, use the <<query-dsl-match-query,`match`>> query
|
|
|
|
instead.
|
|
|
|
====
|
|
|
|
|
|
|
|
[[term-query-ex-request]]
|
|
|
|
==== Example request
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,js]
|
2019-05-06 10:36:07 -04:00
|
|
|
----
|
|
|
|
GET /_search
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
2019-05-06 10:36:07 -04:00
|
|
|
"query": {
|
|
|
|
"term": {
|
|
|
|
"user": {
|
|
|
|
"value": "Kimchy",
|
|
|
|
"boost": 1.0
|
2015-05-08 02:31:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-06 10:36:07 -04:00
|
|
|
----
|
2016-05-09 09:42:23 -04:00
|
|
|
// CONSOLE
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-05-06 10:36:07 -04:00
|
|
|
[[term-top-level-params]]
|
|
|
|
==== Top-level parameters for `term`
|
|
|
|
`<field>`::
|
2019-07-31 14:18:22 -04:00
|
|
|
(Required, object) Field you wish to search.
|
2019-05-06 10:36:07 -04:00
|
|
|
|
|
|
|
[[term-field-params]]
|
|
|
|
==== Parameters for `<field>`
|
|
|
|
`value`::
|
2019-07-31 14:18:22 -04:00
|
|
|
(Required, string) Term you wish to find in the provided `<field>`. To return a
|
|
|
|
document, the term must exactly match the field value, including whitespace and
|
|
|
|
capitalization.
|
2019-05-06 10:36:07 -04:00
|
|
|
|
|
|
|
`boost`::
|
2019-07-31 14:18:22 -04:00
|
|
|
(Optional, float) Floating point number used to decrease or increase the
|
2019-08-02 14:15:12 -04:00
|
|
|
<<relevance-scores,relevance scores>> of a query. Defaults to `1.0`.
|
2019-05-06 10:36:07 -04:00
|
|
|
+
|
|
|
|
You can use the `boost` parameter to adjust relevance scores for searches
|
|
|
|
containing two or more queries.
|
|
|
|
+
|
|
|
|
Boost values are relative to the default value of `1.0`. A boost value between
|
|
|
|
`0` and `1.0` decreases the relevance score. A value greater than `1.0`
|
|
|
|
increases the relevance score.
|
|
|
|
|
|
|
|
[[term-query-notes]]
|
|
|
|
==== Notes
|
|
|
|
|
|
|
|
[[avoid-term-query-text-fields]]
|
|
|
|
===== Avoid using the `term` query for `text` fields
|
|
|
|
By default, {es} changes the values of `text` fields during analysis. For
|
|
|
|
example, the default <<analysis-standard-analyzer, standard analyzer>> changes
|
|
|
|
`text` field values as follows:
|
|
|
|
|
|
|
|
* Removes most punctuation
|
|
|
|
* Divides the remaining content into individual words, called
|
|
|
|
<<analysis-tokenizers, tokens>>
|
|
|
|
* Lowercases the tokens
|
|
|
|
|
|
|
|
To better search `text` fields, the `match` query also analyzes your provided
|
|
|
|
search term before performing a search. This means the `match` query can search
|
|
|
|
`text` fields for analyzed tokens rather than an exact term.
|
|
|
|
|
|
|
|
The `term` query does *not* analyze the search term. The `term` query only
|
|
|
|
searches for the *exact* term you provide. This means the `term` query may
|
|
|
|
return poor or no results when searching `text` fields.
|
|
|
|
|
|
|
|
To see the difference in search results, try the following example.
|
|
|
|
|
|
|
|
. Create an index with a `text` field called `full_text`.
|
|
|
|
+
|
|
|
|
--
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,js]
|
2019-05-06 10:36:07 -04:00
|
|
|
----
|
2019-01-22 09:13:52 -05:00
|
|
|
PUT my_index
|
2015-05-08 02:31:15 -04:00
|
|
|
{
|
2019-05-06 10:36:07 -04:00
|
|
|
"mappings" : {
|
|
|
|
"properties" : {
|
|
|
|
"full_text" : { "type" : "text" }
|
|
|
|
}
|
2015-05-08 02:31:15 -04:00
|
|
|
}
|
|
|
|
}
|
2019-05-06 10:36:07 -04:00
|
|
|
----
|
|
|
|
// CONSOLE
|
|
|
|
|
|
|
|
--
|
2015-05-08 02:31:15 -04:00
|
|
|
|
2019-05-06 10:36:07 -04:00
|
|
|
. Index a document with a value of `Quick Brown Foxes!` in the `full_text`
|
|
|
|
field.
|
|
|
|
+
|
|
|
|
--
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
----
|
2017-12-14 11:47:53 -05:00
|
|
|
PUT my_index/_doc/1
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
2019-05-06 10:36:07 -04:00
|
|
|
"full_text": "Quick Brown Foxes!"
|
2015-05-08 02:31:15 -04:00
|
|
|
}
|
2019-05-06 10:36:07 -04:00
|
|
|
----
|
2016-05-09 09:42:23 -04:00
|
|
|
// CONSOLE
|
2019-05-06 10:36:07 -04:00
|
|
|
// TEST[continued]
|
|
|
|
|
|
|
|
Because `full_text` is a `text` field, {es} changes `Quick Brown Foxes!` to
|
|
|
|
`[quick, brown, fox]` during analysis.
|
2015-05-08 02:31:15 -04:00
|
|
|
|
2019-05-06 10:36:07 -04:00
|
|
|
--
|
2015-05-08 02:31:15 -04:00
|
|
|
|
2019-05-06 10:36:07 -04:00
|
|
|
. Use the `term` query to search for `Quick Brown Foxes!` in the `full_text`
|
|
|
|
field. Include the `pretty` parameter so the response is more readable.
|
|
|
|
+
|
|
|
|
--
|
2015-05-08 02:31:15 -04:00
|
|
|
|
|
|
|
[source,js]
|
2019-05-06 10:36:07 -04:00
|
|
|
----
|
|
|
|
GET my_index/_search?pretty
|
2015-05-08 02:31:15 -04:00
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"term": {
|
2019-05-06 10:36:07 -04:00
|
|
|
"full_text": "Quick Brown Foxes!"
|
2015-05-08 02:31:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-06 10:36:07 -04:00
|
|
|
----
|
|
|
|
// CONSOLE
|
|
|
|
// TEST[continued]
|
2015-05-08 02:31:15 -04:00
|
|
|
|
2019-05-06 10:36:07 -04:00
|
|
|
Because the `full_text` field no longer contains the *exact* term `Quick Brown
|
|
|
|
Foxes!`, the `term` query search returns no results.
|
2015-05-08 02:31:15 -04:00
|
|
|
|
2019-05-06 10:36:07 -04:00
|
|
|
--
|
|
|
|
|
|
|
|
. Use the `match` query to search for `Quick Brown Foxes!` in the `full_text`
|
|
|
|
field.
|
|
|
|
+
|
|
|
|
--
|
|
|
|
|
|
|
|
////
|
2015-05-08 02:31:15 -04:00
|
|
|
|
2019-05-06 10:36:07 -04:00
|
|
|
[source,js]
|
|
|
|
----
|
|
|
|
POST my_index/_refresh
|
|
|
|
----
|
|
|
|
// CONSOLE
|
|
|
|
// TEST[continued]
|
|
|
|
|
|
|
|
////
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
----
|
|
|
|
GET my_index/_search?pretty
|
2015-05-08 02:31:15 -04:00
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"match": {
|
2019-05-06 10:36:07 -04:00
|
|
|
"full_text": "Quick Brown Foxes!"
|
2015-05-08 02:31:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-06 10:36:07 -04:00
|
|
|
----
|
2016-05-09 09:42:23 -04:00
|
|
|
// CONSOLE
|
2016-04-29 10:42:03 -04:00
|
|
|
// TEST[continued]
|
2015-05-08 02:31:15 -04:00
|
|
|
|
2019-05-06 10:36:07 -04:00
|
|
|
Unlike the `term` query, the `match` query analyzes your provided search term,
|
|
|
|
`Quick Brown Foxes!`, before performing a search. The `match` query then returns
|
|
|
|
any documents containing the `quick`, `brown`, or `fox` tokens in the
|
|
|
|
`full_text` field.
|
|
|
|
|
|
|
|
Here's the response for the `match` query search containing the indexed document
|
|
|
|
in the results.
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
----
|
|
|
|
{
|
|
|
|
"took" : 1,
|
|
|
|
"timed_out" : false,
|
|
|
|
"_shards" : {
|
|
|
|
"total" : 1,
|
|
|
|
"successful" : 1,
|
|
|
|
"skipped" : 0,
|
|
|
|
"failed" : 0
|
|
|
|
},
|
|
|
|
"hits" : {
|
|
|
|
"total" : {
|
|
|
|
"value" : 1,
|
|
|
|
"relation" : "eq"
|
|
|
|
},
|
|
|
|
"max_score" : 0.8630463,
|
|
|
|
"hits" : [
|
|
|
|
{
|
|
|
|
"_index" : "my_index",
|
|
|
|
"_type" : "_doc",
|
|
|
|
"_id" : "1",
|
|
|
|
"_score" : 0.8630463,
|
|
|
|
"_source" : {
|
|
|
|
"full_text" : "Quick Brown Foxes!"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
// TESTRESPONSE[s/"took" : 1/"took" : $body.took/]
|
|
|
|
--
|