Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 43 ยท Projects

Project: Product Search with Facets

Design product search for a small shop: mapping, analyzer, bool query with filters, ranking, and facet counts, plus a runnable mini engine in Node.js.

Product search is the classic Elasticsearch job. A shopper types "running shoes", sees the best matches first, narrows the list with filters (brand, category, price, in stock), and sees counts next to each filter option. In this project you design all of it for a small shop: the mapping, the analysis, the query and the aggregations.

Elasticsearch cannot run on this site. The requests below are real and read-only. After each design step you get a runnable Node.js mini engine that does the same thing on eight products, so you can watch how analysis, scoring, filters and facets fit together. The mini engine is a teaching model, not Elasticsearch.

What we are building

One search page, four parts

Text box Full-text match on the product name.
Filters Brand, category, price, stock.
Facet counts How many results each option has.
Ranking Best text matches first.

Text goes in the scoring part of the query, choices go in filters.

1 / 2

Our products have these fields: name, brand, category, price, rating, in_stock.

Step 1: the mapping

Decide for each field how it will be used (mappings and field types):

HTTP
PUT /shop-products
{
  "mappings": {
    "dynamic": "strict",
    "properties": {
      "name":     { "type": "text", "analyzer": "english",
                    "fields": { "raw": { "type": "keyword", "ignore_above": 256 } } },
      "brand":    { "type": "keyword" },
      "category": { "type": "keyword" },
      "price":    { "type": "scaled_float", "scaling_factor": 100 },
      "rating":   { "type": "half_float" },
      "in_stock": { "type": "boolean" }
    }
  }
}
  • name is text so it is analyzed for full-text search. The english analyzer lowercases, removes stop words and stems, so "shoes" and "shoe" match each other. The raw multi-field keeps the exact string for sorting.
  • brand and category are keyword: exact values used for filters and for terms aggregations (facets). They are never analyzed.
  • price is a scaled_float (stored as an integer count of cents), which is exact enough and compact. in_stock is a boolean.
  • "dynamic": "strict" rejects documents with unknown fields, so a typo in a feed cannot silently create a new field.

Index documents in bulk (using Elasticsearch from code) and search through an alias, so you can rebuild the index later without downtime.

Step 2: the search request

Here is the request your application builds when a shopper types "running shoes" and ticks "in stock" with a maximum price of 100:

HTTP
GET /shop-products/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "name": "running shoes" } }
      ],
      "filter": [
        { "term":  { "in_stock": true } },
        { "range": { "price": { "lte": 100 } } }
      ]
    }
  },
  "aggs": {
    "by_category": { "terms": { "field": "category", "size": 10 } },
    "by_brand":    { "terms": { "field": "brand", "size": 10 } },
    "price_ranges": {
      "range": {
        "field": "price",
        "ranges": [ { "to": 50 }, { "from": 50, "to": 100 }, { "from": 100 } ]
      }
    }
  },
  "size": 12
}

Why it looks like this:

  • must holds the full-text clause: it decides which products match by words and gives each a BM25 score.
  • filter holds the shopper's choices: yes/no conditions with no scoring, and cacheable (query performance).
  • match uses OR between words by default, so "running shoes" also finds a "Running Socks Pack". That is often fine for recall, because the shoes score higher (both words). If you want stricter results, use "operator": "and" or minimum_should_match (full-text queries).
  • The aggs section returns facet counts for the current result set. Aggregations run on documents that match the query and its filters.

Step 3: the mini engine

The program below follows the same steps with plain JavaScript: an analyzer (lowercase, split, simple plural stemmer), an inverted index, BM25 scoring, filters, and terms buckets.

Node.js A product search engine in 80 lines

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Read the three results:

  1. "running shoes" matches four products, because the engine uses OR. The two shoes score highest (both words), and the sock pack, which has only "running", comes next. The facets count the four matches by category and brand.
  2. "trail" with in-stock and max price filters drops the jacket, because it costs more than 100 and the filters remove it. The filters changed the facet counts, but not the scores: scores come only from the text.
  3. No text, only a brand filter has no scoring clause, so every hit gets the same constant score, like a match_all plus a filter. Ties are then broken by ID here; in Elasticsearch you would add a sort on rating or popularity.

Step 4: making facets behave like a real shop

A subtle problem appears with multi-select facets. If the shopper ticks the brand "Aerolite", a plain terms aggregation on brand will show only Aerolite with a count, and the other brands disappear, so shoppers cannot switch easily. The standard fix uses post_filter: the brand choice is applied after aggregations are computed, so the brand facet still counts all brands, while the hit list is filtered:

HTTP
GET /shop-products/_search
{
  "query": { "match": { "name": "running" } },
  "post_filter": { "term": { "brand": "Aerolite" } },
  "aggs": {
    "brands": { "terms": { "field": "brand" } }
  }
}

Other facets (such as category) should then be computed inside a filter aggregation that applies the brand choice, so each facet reflects the other selections. This pattern is covered in detail in faceted search and filters.

Step 5: ranking beyond text

Pure BM25 treats a 4.8-star bestseller like an unrated item. Common improvements:

  • Boost the name over other fields in multi_match ("fields": ["name^3", "description"]).
  • Boost by rating or popularity with a function_score query or rank_feature field (bool and compound queries). Keep the boost mild so relevance still matters.
  • Push out-of-stock items down instead of hiding them: a should clause on in_stock: true adds a little score to available products.
  • Spelling mistakes: add "fuzziness": "AUTO" to the match query.
  • Autocomplete: see the next project.

Production checklist

  • Search through an alias, and build new indexes for mapping changes.
  • Reindex from your source of truth (the shop database) with a bulk job; do not treat Elasticsearch as the only copy.
  • Whitelist sortable and filterable fields on your server; clamp size (security and operations).
  • Return only needed fields (_source filtering) and keep facet size small.
  • Test relevance with a fixed list of real queries and expected top results, and re-run it after each change.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In the product search request, why are the in-stock and price conditions placed in filter, not must?

    Filter context skips scoring and can be cached. Only the text match should decide relevance.
  2. 2. Which field type fits brand and category when you want facet counts?

    Terms aggregations count exact values, so brand and category are keyword fields. A text field would be split into analyzed tokens.
  3. 3. A shopper selects the brand Aerolite, and the brand facet now shows only Aerolite. How do you keep all brands visible in the facet?

    post_filter narrows the hits after aggregations are calculated, so the brand facet still counts every brand.
  4. 4. Why does the search for running shoes in the mini engine also return a Running Socks Pack?

    With OR semantics any matching word counts. The socks score lower because only one of the two words matches.

Summary

  • Map text for searching (text), exact values for filters and facets (keyword), and money as scaled_float.
  • Put the shopper's words in a scoring must clause and their choices in filter clauses.
  • terms aggregations produce facet counts over the current matches; post_filter keeps other options visible.
  • Improve ranking with boosts, rating signals and fuzziness, and test with a fixed list of real queries.
  • Search through an alias and keep your database as the source of truth.

Next: Project: Search-as-You-Type Autocomplete.