Table of contents :

Wildcard Queries: Powerful but Expensive

100-days-of-elasticsearch-woocommerce-028

Table of contents :

Wildcard queries are useful when a WooCommerce search requirement includes incomplete terms, variable SKU formats, or unknown characters. They are also one of the easiest ways to make Elasticsearch queries expensive, especially when the pattern begins with * or ?.

What a wildcard query does

A wildcard query matches terms using pattern characters:

  • * matches zero or more characters.
  • ? matches exactly one character.

For example, this query can find product SKUs containing shoe anywhere in the value:

{
  "query": {
    "wildcard": {
      "sku.keyword": {
        "value": "*shoe*",
        "case_insensitive": true
      }
    }
  }
}

Wildcard queries operate on terms in the index. They do not analyze the input like a match query. For WooCommerce product data, this usually means querying a keyword field such as sku.keyword, slug, or a normalized exact-value field.

Why leading wildcards are expensive

Elasticsearch can resolve a pattern such as shoe* by navigating the term dictionary from a known prefix. A pattern such as *shoe or *shoe* has no known starting point, so Elasticsearch may need to inspect a large portion of the term dictionary and construct a much larger set of matching terms.

The cost increases when the field contains many unique product values, when the query runs across multiple shards, or when the request is executed for every keystroke in a live WooCommerce search box. A query that appears harmless on a development catalog can create significant CPU and latency pressure on a large production store.

Choose the narrowest pattern

Prefer a trailing wildcard when the business requirement allows it:

{
  "query": {
    "wildcard": {
      "sku.keyword": {
        "value": "ABC-*"
      }
    }
  }
}

This is generally less expensive than matching the same value with *ABC-*. If the user is searching for a complete SKU, use a term query instead of a wildcard query:

{
  "query": {
    "term": {
      "sku.keyword": "ABC-12345"
    }
  }
}

For an exact SKU lookup, a term query is both clearer and more efficient. Agencies should avoid automatically wrapping every search value in asterisks simply to make search appear more forgiving.

Use wildcard fields deliberately

Elasticsearch also provides the wildcard field type, which is designed for efficiently querying arbitrary substring and regular-expression patterns. It can be appropriate for high-cardinality strings such as log-like identifiers, imported reference codes, or normalized SKU values that genuinely require infix searching.

It is not a universal replacement for keyword. Product names, descriptions, and category text usually need full-text fields with analyzers, while exact filters such as stock status, brand IDs, and product attributes should remain keyword or numeric fields.

Control expensive queries

Elasticsearch can reject expensive wildcard and regular-expression queries when the cluster setting search.allow_expensive_queries is disabled. This is a useful safety control, but it should not be treated as a performance strategy. A rejected query still represents an application design problem that should be handled before it reaches the search cluster.

At the request level, avoid broad patterns and restrict the query to the fields that actually need wildcard behavior. Do not send wildcard queries across every product field using a large bool or query_string expression.

Better options for WooCommerce search

Prefix search

If customers normally enter the beginning of a product name or SKU, use an edge-ngram analyzer at index time and query the resulting field with a match or prefix query. This moves much of the work from query time to indexing time and avoids leading wildcards.

Substring search

If users must find a value from the middle of a SKU, create a dedicated n-gram field or use an Elasticsearch wildcard field. Keep this field separate from the field used for sorting, aggregations, exact filters, and display.

Full-text product search

For product titles and descriptions, use an analyzer suited to the store’s language and search behavior. A match query, possibly combined with phrase matching and boosted fields, usually provides better relevance than a pattern such as *red shoes*.

Protect live autocomplete requests

Autocomplete is a common source of accidental wildcard overload. Apply a minimum input length, debounce requests in the browser, and avoid running a leading wildcard until the user has entered enough characters. Limit the response with size and request only the fields needed to render suggestions.

{
  "size": 8,
  "_source": ["id", "name", "sku"],
  "query": {
    "wildcard": {
      "sku.keyword": {
        "value": "*1234*",
        "case_insensitive": true
      }
    }
  }
}

For a WooCommerce agency, this behavior should be configurable per store. A catalog with a few thousand products may tolerate a narrow wildcard request, while a multi-million-product catalog should use a dedicated autocomplete or n-gram design.

Review wildcard queries in production

  • Inspect slow logs and search latency by endpoint, store, and query type.
  • Test leading, trailing, and infix patterns against a production-sized index.
  • Measure shard CPU, heap usage, and query fan-out during autocomplete traffic.
  • Confirm that wildcard queries target keyword or wildcard-compatible fields rather than analyzed text fields.
  • Set application limits for minimum query length, maximum result count, and request frequency.

Wildcard queries are valuable for specific WooCommerce requirements, particularly irregular identifiers and partial SKU searches. They become a liability when used as a default substitute for field mapping, analyzers, prefix indexes, or exact-value queries.

Trending posts
You might also like