Table of contents :

Prefix Search for Product Discovery

100-days-of-elasticsearch-woocommerce-027

Table of contents :

Prefix search matches products whose searchable terms begin with the customer’s input. In a WooCommerce catalog, this is useful for product names, SKUs, brand names, and category labels when shoppers type the beginning of a word such as wire for “Wireless Charging Stand” or nike air for a product family.

Prefix search is different from substring search. A prefix query can match wireless when the input is wire, but it should not match a value where “wire” appears only in the middle of a term. That distinction makes prefix search faster and more predictable for autocomplete and product discovery.

Choose the right Elasticsearch field

For product discovery, the most flexible approach is usually a text field with an analyzer designed for prefixes. A standard analyzed field is not enough by itself because the term “wireless” is indexed as a complete token, while a query for “wire” is a different term.

Elasticsearch’s search_as_you_type field type is a practical option for product titles and other human-readable fields. It creates supporting subfields that help match partial input without requiring a large custom n-gram index.

{ "properties": { "name": { "type": "search_as_you_type" }, "sku": { "type": "keyword" }, "brand": { "type": "search_as_you_type" } } }

Use a keyword field for exact SKU and identifier matching. If shoppers commonly enter the beginning of an SKU, add a separate prefix-oriented field or use a purpose-built autocomplete mapping rather than applying a text analyzer blindly to the canonical SKU value.

Using a prefix query

A prefix query is appropriate when the field contains a single indexed term, such as a normalized SKU or a keyword-based product code.

{ "query": { "prefix": { "sku.prefix": { "value": "wc-24" } } } }

This query asks Elasticsearch to find terms beginning with wc-24. It does not analyze the input, so the application should normalize values consistently. For example, decide whether SKUs are lowercased, whether spaces are removed, and whether punctuation is retained before indexing and querying.

Using match_phrase_prefix for product names

For a multi-word product title, match_phrase_prefix is often more useful than a raw prefix query. It analyzes the input and treats the final term as a prefix while preserving the order of earlier terms.

{ "query": { "match_phrase_prefix": { "name": { "query": "wireless charg", "max_expansions": 25 } } } }

With this query, “wireless” must match as an analyzed term and the final term can match values beginning with “charg”, such as “charging”. The max_expansions setting limits how many indexed terms Elasticsearch considers for the final prefix. Lower values can protect latency, while higher values may improve recall in catalogs with many similar terms.

Using match_bool_prefix with search_as_you_type

When the field is mapped as search_as_you_type, multi_match with bool_prefix can search the generated subfields efficiently.

{ "query": { "multi_match": { "query": "wireless charg", "type": "bool_prefix", "fields": [ "name", "name._2gram", "name._3gram" ] } } }

This pattern works well for autocomplete because the final token is treated as an incomplete term while the preceding tokens contribute normal matching signals. In a WooCommerce integration, build the field list from the configured mapping rather than hard-coding subfields if different stores use different field names or shingle settings.

Combining prefix discovery with exact product signals

Prefix matching should normally be one part of a product query, not the only ranking signal. Combine it with exact SKU matches, product visibility filters, stock rules, language constraints, and optional boosts for title or brand matches.

{ "query": { "bool": { "filter": [ { "term": { "status": "publish" } }, { "term": { "catalog_visibility": "visible" } } ], "should": [ { "term": { "sku": { "value": "wc-2401", "boost": 8 } } }, { "multi_match": { "query": "wireless charg", "type": "bool_prefix", "fields": [ "name^4", "name._2gram^2", "name._3gram", "brand^2" ] } } ], "minimum_should_match": 1 } } }

Keep filters in the filter context because they do not need relevance scoring and can be cached more effectively. Use the should clauses for relevance. This separation is especially important for WooCommerce catalogs where stock, language, channel, and customer-specific visibility rules can substantially reduce the eligible product set.

Handling very short prefixes

One- or two-character prefixes can generate many matches and create noisy autocomplete results. Set a minimum input length in the storefront, commonly two or three characters, and avoid sending a request for every keystroke without debouncing.

A practical request flow is:

  1. Normalize the input using the same case and whitespace rules used by the index.
  2. Wait briefly after the last keystroke, such as 100 to 250 milliseconds, before querying.
  3. Require a minimum length unless the shopper is searching a constrained field such as a known SKU prefix.
  4. Limit the result count for autocomplete, often to 5–10 products.
  5. Cancel stale requests so a slower response for an earlier keystroke cannot overwrite newer results.

Do not use a large max_expansions value as a substitute for input controls. Prefix expansion cost depends on the number of matching terms, and broad prefixes can become expensive on large catalogs.

Prefix matching and WooCommerce variations

Decide whether the search document represents a parent product, a variation, or both. If variations are indexed separately, a prefix match on a variation SKU may return several visually identical parent products. Collapse or group results by the parent product ID when the storefront displays parent products.

For variation attributes, index searchable display values such as “blue” or “large” in dedicated fields. Avoid concatenating every variation value into the product title, because that can create confusing autocomplete matches and inflate the index. Instead, query product name, brand, SKU, and selected attribute fields with deliberate boosts.

When to use edge n-grams instead

An edge_ngram analyzer can be useful when you need exact control over indexed prefixes, especially for custom autocomplete behavior or fields that do not fit search_as_you_type. The trade-off is a larger index and more analysis configuration.

{ "settings": { "analysis": { "analyzer": { "product_prefix_index": { "tokenizer": "standard", "filter": [ "lowercase", "product_prefix_filter" ] } }, "filter": { "product_prefix_filter": { "type": "edge_ngram", "min_gram": 2, "max_gram": 15 } } } }, "mappings": { "properties": { "name_prefix": { "type": "text", "analyzer": "product_prefix_index", "search_analyzer": "standard" } } } }

Use a conservative max_gram based on real catalog data. Increasing it increases index size. Also remember that changing analyzers requires creating a new index and reindexing products; analyzers are not retroactively applied to existing terms.

Testing relevance and operational behavior

Test prefixes against real WooCommerce data, including product names with punctuation, accented characters, multiple languages, numbers, hyphens, and duplicate variation names. Check both recall and ordering: a query such as usb c should find relevant USB-C products without allowing a broad “USB” result set to dominate every response.

Measure query latency at realistic catalog sizes and with production-like filters. Review slow logs and inspect the generated query when results are unexpectedly broad. Also test empty input, stop words, repeated spaces, malformed SKU values, and prefixes that match thousands of terms.

For agencies, keep mapping definitions, normalization rules, minimum prefix lengths, boosts, and result limits in version-controlled configuration. This makes search behavior repeatable across client stores and prevents a theme or plugin update from silently changing the contract between WooCommerce and Elasticsearch.

Trending posts
You might also like