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:

  1. GET shakespeare/_search
  2. {
  3. "query": {
  4. "term": {
  5. "line_id": {
  6. "value": "61809"
  7. }
  8. }
  9. }
  10. }

copy

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:

  1. GET shakespeare/_search
  2. {
  3. "query": {
  4. "term": {
  5. "speaker": {
  6. "value": "HAMLET",
  7. "case_insensitive": true
  8. }
  9. }
  10. }
  11. }

copy

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

  1. "hits": {
  2. "total": {
  3. "value": 1582,
  4. "relation": "eq"
  5. },
  6. "max_score": 2,
  7. "hits": [
  8. {
  9. "_index": "shakespeare",
  10. "_id": "32700",
  11. "_score": 2,
  12. "_source": {
  13. "type": "line",
  14. "line_id": 32701,
  15. "play_name": "Hamlet",
  16. "speech_number": 9,
  17. "line_number": "1.2.66",
  18. "speaker": "HAMLET",
  19. "text_entry": "[Aside] A little more than kin, and less than kind."
  20. }
  21. },
  22. ...
  23. }

Parameters

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

  1. GET _search
  2. {
  3. "query": {
  4. "term": {
  5. "<field>": {
  6. "value": "sample",
  7. ...
  8. }
  9. }
  10. }
  11. }

copy

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

ParameterData typeDescription
valueStringThe 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.
boostFloating-pointBoosts the query by the given multiplier. Useful for searches that contain more than one query. Values in the [0, 1) range decrease relevance, and values greater than 1 increase relevance. Default is 1.
case_insensitiveBooleanIf 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).