Link Search Menu Expand Document Documentation Menu

Term query

Use the term query to search for an exact term in a field. For example, the following query searches for a line with an exact line number:

GET shakespeare/_search
{
  "query": {
    "term": {
      "line_id": {
        "value": "61809"
      }
    }
  }
}

When a document is indexed, the text fields are analyzed. Analysis includes tokenizing and lowercasing the text and removing punctuation. Unlike match queries, which analyze the query text, term queries only match the exact term and thus may not return relevant results. Avoid using term queries on text fields. For more information, see Term-level and full-text queries compared.

You can specify that the query should be case insensitive in the case_insensitive parameter:

GET shakespeare/_search
{
  "query": {
    "term": {
      "speaker": {
        "value": "HAMLET",
        "case_insensitive": true
      }
    }
  }
}

The response contains the matching documents despite any differences in case:

"hits": {
  "total": {
    "value": 1582,
    "relation": "eq"
  },
  "max_score": 2,
  "hits": [
    {
      "_index": "shakespeare",
      "_id": "32700",
      "_score": 2,
      "_source": {
        "type": "line",
        "line_id": 32701,
        "play_name": "Hamlet",
        "speech_number": 9,
        "line_number": "1.2.66",
        "speaker": "HAMLET",
        "text_entry": "[Aside]  A little more than kin, and less than kind."
      }
    },
  ...
}

Parameters

The query accepts the name of the field (<field>) as a top-level parameter:

GET _search
{
  "query": {
    "term": {
      "<field>": {
        "value": "sample",
        ...
      }
    }
  }
}

The <field> accepts the following parameters. All parameters except value are optional.

Parameter Data type Description
value String The term to search for in the field specified in <field>. A document is returned in the results only if its field value exactly matches the term, with the correct spacing and capitalization.
boost Floating-point A floating-point value that specifies the weight of this field toward the relevance score. Values above 1.0 increase the field’s relevance. Values between 0.0 and 1.0 decrease the field’s relevance. Default is 1.0.
case_insensitive Boolean If true, allows case-insensitive matching of the value with the indexed field values. Default is false (case sensitivity is determined by the field’s mapping).
350 characters left

Have a question? .

Want to contribute? or .