AI Hints
Home›Elasticsearch Tutorial›Exact Values and Ranges
Elasticsearch · Lesson 28 of 120

term on Keywords

Exact values.

⏱ 2 min read▶ Video at 25:42🎓 Free
Watch this lesson in the Elasticsearch courseWatch on YouTube ↗Full Elasticsearch course

Not every search is full text. Show me the Jackets category is an exact question. For exact values, like categories, brands and IDs, use the term query.

  • Full text vs exact
  • Category = Jackets
  • term

term on a keyword

Each text field also has a keyword version, holding the exact original value. Term looks for exactly Jackets, in the category dot keyword field. Run it. Seven products are in the Jackets category.

Dev Tools Consoletext
GET products/_search?filter_path=hits.total.value
{
  "query": { "term": { "category.keyword": "Jackets" } }
}
Output
{"hits": {"total": {"value": 7}}}
Also uses _load
products

The classic mistake

Here's a mistake almost everyone makes. Term on the text field category, with a capital J, finds nothing. Run it. Zero hits. The text field stored lower case jackets, and term doesn't change your search text, so they never match.

Dev Tools Consoletext
GET products/_search?filter_path=hits.total.value
{
  "query": { "term": { "category": "Jackets" } }
}
Output
{"hits": {"total": {"value": 0}}}
Also uses _load
products

Summary

Use term for exact values on keyword fields. Don't use term on text fields. Use match for those. Next, matching a list of values.

Key takeaways
  • term
  • keyword fields
  • Not on text
Next up: terms and ids — any of these values