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

terms and ids

Any of these values.

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

A shopper ticks two brands in the filter list, Summit and Alpina. You need products from either brand. Terms is term with a list of values.

  • Two brands ticked
  • Either one
  • terms

terms

Terms matches documents whose brand is any value in the list. Run it. Products from Summit or Alpina, and nothing else.

Dev Tools Consoletext
GET products/_search?filter_path=hits.total.value,hits.hits._source.brand
{
  "query": {
    "terms": { "brand.keyword": ["Summit", "Alpina"] }
  },
  "size": 4
}
Output
{
  "hits": {
    "total": {"value": 15},
    "hits": [
      {"_source": {"brand": "Alpina"}},
      {"_source": {"brand": "Summit"}},
      {"_source": {"brand": "Alpina"}},
      {"_source": {"brand": "Summit"}}
    ]
  }
}
Also uses _load
products

ids

The ids query matches documents by their ID, like a filter for a saved list. Run it. We get exactly products one, five and nine.

Dev Tools Consoletext
GET products/_search?filter_path=hits.hits._id,hits.hits._source.name
{
  "query": { "ids": { "values": ["1", "5", "9"] } }
}
Output
{
  "hits": {
    "hits": [
      {
        "_id": "1",
        "_source": {"name": "Basecamp 2-Person Tent"}
      },
      {"_id": "5", "_source": {"name": "Ultralight Solo Tent"}},
      {
        "_id": "9",
        "_source": {"name": "Arctic Down Sleeping Bag"}
      }
    ]
  }
}
Also uses _load
products

Summary

Terms matches any value in a list, and ids matches a list of document IDs. Next, ranges of numbers and dates.

Key takeaways
  • terms
  • Any of the list
  • ids
Next up: range — between two values