AI Hints
Home›Elasticsearch Tutorial›Project: Trailhead Search
Elasticsearch · Lesson 116 of 120

Project: Search and Facets

The main search.

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

Now the main search request. A shopper types warm, under three hundred dollars, and has clicked the Summit brand. We need the hits, highlights, and brand facets.

  • Words: warm
  • Under $300
  • Clicked: Summit

The full search request

Multi match searches name and description, with name boosted. The price limit is a filter. The clicked brand goes in post filter, so brand facets stay complete. Highlight shows why each hit matched. Run it. The top Summit product, with a snippet, and counts for every brand.

Dev Tools Consoletext
GET trailhead/_search?filter_path=hits.hits.highlight,aggregations.brands.buckets
{
  "size": 1,
  "query": { "bool": {
    "must":   { "multi_match": { "query": "warm", "fields": ["name^3", "description"] } },
    "filter": { "range": { "price": { "lte": 300 } } } } },
  "post_filter": { "term": { "brand": "Summit" } },
  "aggs": { "brands": { "terms": { "field": "brand", "size": 4 } } },
  "highlight": { "fields": { "description": {} } }
}
Output
{
  "hits": {
    "hits": [
      {
        "highlight": {
          "description": [
            "A bright, <em>warm</em> light that runs for hours on one charge."
          ]
        }
      }
    ]
  },
  "aggregations": {
    "brands": {
      "buckets": [
        {"key": "Summit", "doc_count": 5},
        {"key": "Ridgeline", "doc_count": 3},
        {"key": "NorthPeak", "doc_count": 2},
        {"key": "TrailCo", "doc_count": 2}
      ]
    }
  }
}
Also uses _load
products
Also uses _setup.http
PUT trailhead?filter_path=acknowledged
{ "settings": { "analysis": {
    "filter": { "edge": { "type": "edge_ngram", "min_gram": 2, "max_gram": 10 } },
    "analyzer": { "auto": { "tokenizer": "standard", "filter": ["lowercase", "edge"] } } } },
  "mappings": { "properties": {
    "name": { "type": "text", "analyzer": "english", "fields": {
      "auto": { "type": "text", "analyzer": "auto", "search_analyzer": "standard" },
      "keyword": { "type": "keyword" } } },
    "description": { "type": "text", "analyzer": "english" },
    "brand":    { "type": "keyword" }, "category": { "type": "keyword" },
    "price":    { "type": "double" },  "rating":   { "type": "double" },
    "in_stock": { "type": "boolean" } } } }

POST _reindex?refresh=true&filter_path=created
{ "source": { "index": "products" }, "dest": { "index": "trailhead" } }

What we used

Multi match and boosts from the relevance lessons. Bool filters from the query lessons. Post filter and terms from the aggregation lessons. And highlighting. That's the core of a real store's search.

keyvalue
multi_matchboosted fields
filterprice range
post_filterclicked facet
highlightsnippets

Summary

One request gives hits, highlights and facets, using filters and post filter correctly. Next, autocomplete and ranking.

Key takeaways
  • Search
  • Facets
  • Highlights
Next up: Project: Autocomplete and Ranking — the finishing touches