AI Hints
Home›Elasticsearch Tutorial›More Queries, Sorting and Paging
Elasticsearch · Lesson 31 of 120

exists, prefix and wildcard

Handy extras.

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

A few more queries come in handy. Find documents that have a field at all. Find words that start with some letters. Or match a simple pattern.

  • Has the field?
  • Starts with
  • Pattern

exists

Some new products don't have a rating yet. Exists matches documents where a field has a value. We add a product with no rating, and count products that do have one. Run it. Forty nine out of fifty have a rating.

Dev Tools Consoletext
GET products/_count?filter_path=count
{
  "query": { "exists": { "field": "rating" } }
}
Output
{"count": 49}
Also uses _load
products
Also uses _setup.http
PUT products/_doc/50
{ "name": "Mystery Gift Card", "price": 25 }

prefix and wildcard

Prefix matches keyword values that start with some letters, here N, o, r, t, h. Wildcard uses a star for any letters, so star Peak matches NorthPeak. Run it. Both find the NorthPeak brand.

Dev Tools Consoletext
GET products/_count?filter_path=count
{
  "query": { "prefix": { "brand.keyword": "North" } }
}

GET products/_count?filter_path=count
{
  "query": { "wildcard": { "brand.keyword": "*Peak" } }
}
Output
# GET products/_count 200 OK
{"count": 6}

# GET products/_count 200 OK
{"count": 6}
Also uses _load
products

Summary

Exists finds documents that have a field, prefix matches the start of a value, and wildcard matches a pattern. Wildcards that start with a star can be slow on big indexes. Next, sorting.

Key takeaways
  • exists
  • prefix
  • wildcard
Next up: Sorting — not just by score