Term query

Returns documents that contain an exact term in a provided field.

You can use the term query to find documents based on a precise value such as a price, a product ID, or a username.

Avoid using the term query for text fields.

By default, Elasticsearch changes the values of text fields as part of analysis. This can make finding exact matches for text field values difficult.

To search text field values, use the match query instead.

Example request

  1. GET /_search
  2. {
  3. "query": {
  4. "term": {
  5. "user.id": {
  6. "value": "kimchy",
  7. "boost": 1.0
  8. }
  9. }
  10. }
  11. }

Top-level parameters for term

<field>

(Required, object) Field you wish to search.

Parameters for <field>

value

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

boost

(Optional, float) Floating point number used to decrease or increase the relevance scores of a query. Defaults to 1.0.

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.

Notes

Avoid using the term query for text fields

By default, Elasticsearch changes the values of text fields during analysis. For example, the default standard analyzer changes text field values as follows:

  • Removes most punctuation
  • Divides the remaining content into individual words, called 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.

  1. Create an index with a text field called full_text.

    1. PUT my-index-000001
    2. {
    3. "mappings": {
    4. "properties": {
    5. "full_text": { "type": "text" }
    6. }
    7. }
    8. }
  2. Index a document with a value of Quick Brown Foxes! in the full_text field.

    1. PUT my-index-000001/_doc/1
    2. {
    3. "full_text": "Quick Brown Foxes!"
    4. }

    Because full_text is a text field, Elasticsearch changes Quick Brown Foxes! to [quick, brown, fox] during analysis.

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

    1. GET my-index-000001/_search?pretty
    2. {
    3. "query": {
    4. "term": {
    5. "full_text": "Quick Brown Foxes!"
    6. }
    7. }
    8. }

    Because the full_text field no longer contains the exact term Quick Brown Foxes!, the term query search returns no results.

  4. Use the match query to search for Quick Brown Foxes! in the full_text field.

    1. GET my-index-000001/_search?pretty
    2. {
    3. "query": {
    4. "match": {
    5. "full_text": "Quick Brown Foxes!"
    6. }
    7. }
    8. }

    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.

    1. {
    2. "took" : 1,
    3. "timed_out" : false,
    4. "_shards" : {
    5. "total" : 1,
    6. "successful" : 1,
    7. "skipped" : 0,
    8. "failed" : 0
    9. },
    10. "hits" : {
    11. "total" : {
    12. "value" : 1,
    13. "relation" : "eq"
    14. },
    15. "max_score" : 0.8630463,
    16. "hits" : [
    17. {
    18. "_index" : "my-index-000001",
    19. "_type" : "_doc",
    20. "_id" : "1",
    21. "_score" : 0.8630463,
    22. "_source" : {
    23. "full_text" : "Quick Brown Foxes!"
    24. }
    25. }
    26. ]
    27. }
    28. }