0
0
Elasticsearchquery~5 mins

Term query in Elasticsearch

Choose your learning style9 modes available
Introduction

A term query helps you find exact matches in your data. It looks for a specific word or number exactly as you type it.

You want to find all documents where a field exactly matches a word, like 'apple'.
You need to search for a specific ID number in your records.
You want to filter results by a keyword without any changes or analysis.
You want to find documents with a specific tag or category exactly.
You want to search for exact boolean values like true or false.
Syntax
Elasticsearch
{
  "term": {
    "field_name": {
      "value": "value"
    }
  }
}

The field_name is the exact field you want to search in.

The value is the exact term you want to find.

Examples
Finds documents where the 'status' field is exactly 'active'.
Elasticsearch
{
  "term": {
    "status": {
      "value": "active"
    }
  }
}
Finds documents where the 'user_id' field is exactly 12345.
Elasticsearch
{
  "term": {
    "user_id": {
      "value": 12345
    }
  }
}
Finds documents where the 'is_verified' field is exactly true.
Elasticsearch
{
  "term": {
    "is_verified": {
      "value": true
    }
  }
}
Sample Program

This query searches for documents where the 'category' field exactly matches the word 'books'.

Elasticsearch
{
  "query": {
    "term": {
      "category": {
        "value": "books"
      }
    }
  }
}
OutputSuccess
Important Notes

Term query does not analyze the text, so it matches the exact term.

Use term query for exact matches, not for full-text search.

Field must be not analyzed or keyword type for term query to work as expected.

Summary

Term query finds exact matches in your data.

It is useful for searching keywords, IDs, or boolean values exactly.

It does not change or analyze the search term.