Table of contents :

Fuzzy Search for Misspelled Product Names

100-days-of-elasticsearch-woocommerce-025

Table of contents :

Misspelled product names are a common reason shoppers abandon WooCommerce searches. A customer may search for wireles headphones, stainles steel bottle, or runing shoes and still expect the correct products to appear. Elasticsearch can handle these mistakes with fuzzy matching, provided the query is configured carefully and integrated with WooCommerce product rules.

How Elasticsearch fuzzy matching works

Fuzzy matching uses edit distance to find terms that are within a small number of character changes from the search term. The supported changes include inserting, removing, replacing, or transposing characters.

For example, a search for headphons can match headphones. Elasticsearch exposes this behavior through the fuzziness option on queries such as match and multi_match.

For product search, AUTO is usually a good starting point because Elasticsearch selects an edit distance based on the length of each term. Explicit values such as 1 or 2 can also be used, but allowing too many edits increases false positives and query cost.

A practical fuzzy product query

The following query searches product names and descriptions while giving product names the strongest relevance. The exact field names depend on the index mapping used by the agency or search extension.

{"query":{"bool":{"must":[{"multi_match":{"query":"wireles headphones","fields":["name^4","short_description^2","description"],"type":"best_fields","fuzziness":"AUTO","prefix_length":1,"max_expansions":50}}],"filter":[{"term":{"post_type":"product"}},{"term":{"catalog_visibility":"visible"}}]}}}

The prefix_length setting keeps the first character exact before fuzzy expansion begins. This reduces work and prevents very broad matches. max_expansions limits the number of terms generated for each fuzzy token, which helps keep latency predictable on large catalogs.

Use fuzziness as a fallback, not the only strategy

Fuzzy matching should normally be one part of a search pipeline. A useful approach is to run an exact or lightly analyzed query first, then use fuzzy matching when the first query produces few or no results. This preserves precise ranking for correctly spelled searches while still recovering misspelled searches.

One implementation can use a should clause to combine exact and fuzzy relevance:

{"query":{"bool":{"should":[{"match":{"name^5":{"query":"stainles steel bottle","operator":"and"}}},{"match":{"name^3":{"query":"stainles steel bottle","fuzziness":"AUTO","prefix_length":1}}}],"minimum_should_match":1}}}

Exact matches receive the larger boost, while the fuzzy clause provides a recovery path. For multi-word searches, test both operator: and and the default or behavior. Requiring every token can improve precision, but it may hide useful products when a shopper misspells one word or includes an uncommon term.

Keep WooCommerce filters separate from text relevance

Fuzzy text matching should not override WooCommerce business rules. Product status, catalog visibility, language, stock availability, customer-specific pricing, sales channels, and permitted categories should be applied as filter clauses wherever possible. Filters do not contribute to relevance scoring and are generally more efficient than putting the same conditions inside the full-text query.

For example, an agency might add stock filtering like this:

{"filter":[{"term":{"post_status":"publish"}},{"term":{"catalog_visibility":"visible"}},{"term":{"stock_status":"instock"}}]}

Do not assume that a product indexed in Elasticsearch is automatically eligible for display. The indexing process and the search request should both respect WooCommerce visibility and authorization rules.

Handle SKUs and brand names differently

Fuzzy matching is useful for natural-language names, but SKUs often require different treatment. A typo in a short SKU can produce unrelated matches, while an exact SKU lookup should normally be highly authoritative. Use a keyword field for exact SKU matching and optionally add a lower-boost fuzzy or analyzed SKU field for longer identifiers.

{"query":{"bool":{"should":[{"term":{"sku.keyword":{"value":"ABC-1042","boost":10}}},{"match":{"name":{"query":"ABC-1042","fuzziness":1}}}],"minimum_should_match":1}}}

Brands, model numbers, and technical terms may also need curated aliases. A synonym filter can handle known alternatives such as tv and television, while fuzzy matching handles accidental spelling errors. Synonyms and fuzzy expansion solve different problems and should be tested independently.

Integrating the query with a WooCommerce search endpoint

When a WordPress or WooCommerce endpoint receives the search term, validate and normalize the input before sending it to Elasticsearch. Preserve the original term for relevance, but apply sensible limits to its length and the number of requested results.

<?php $term = isset( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : ''; $body = array( 'query' => array( 'bool' => array( 'must' => array( array( 'multi_match' => array( 'query' => $term, 'fields' => array( 'name^4', 'short_description^2', 'description' ), 'fuzziness' => 'AUTO', 'prefix_length' => 1, 'max_expansions' => 50 ) ) ), 'filter' => array( array( 'term' => array( 'post_type' => 'product' ) ), array( 'term' => array( 'post_status' => 'publish' ) ) ) ) ) );

Use the Elasticsearch client or the search extension’s supported API rather than constructing raw SQL from the request. If the site uses multilingual products, include the correct language or index filter and make sure the analyzer matches the language of the indexed content.

Prevent fuzzy search from becoming expensive

  • Use fuzzy matching only on fields that need it, usually the product name and selected descriptive fields.
  • Set a small prefix_length and a controlled max_expansions.
  • Avoid high fuzziness values for short terms. A one-character or two-character search should usually use exact or prefix matching instead.
  • Limit the result window and use search-after or pagination strategies for deeper result sets.
  • Apply catalog, stock, language, and authorization constraints as filters.
  • Monitor query latency, total hits, and the percentage of searches that use fuzzy fallback.

For autocomplete, use a completion or edge-ngram design instead of running unrestricted fuzzy queries on every keystroke. Fuzzy matching is better suited to submitted searches, typo recovery, and a deliberate “did you mean” flow.

Test relevance with real WooCommerce queries

Build a test set from the store’s search analytics and include common errors, short terms, product variants, SKUs, brand names, and searches that should return no results. Check whether the intended product appears near the top, whether unrelated products outrank it, and whether filters remain correct.

Useful metrics include zero-result rate, click-through rate from search results, add-to-cart rate, conversion rate, and p95 search latency. Review these metrics by query type rather than treating all searches as one group. A fuzzy configuration that improves misspelled product names may be harmful for exact model-number searches.

Trending posts
You might also like