Wildcard and prefix fuzzy search: wildcard

Updated at:

In scenarios such as SKU code lookup, file path search, error code matching, or search box autocomplete, business logic often needs to run *wildcard*, regex, fuzzy correction, or prefix queries. By choosing dedicated field types when creating the collection, the system builds substring or prefix indexes in advance so these fuzzy and prefix queries run at high speed. For example, use wildcard, or text with index_prefixes enabled, or search_as_you_type.

1. Why dedicated index fields are needed

In a regular database, running LIKE '%iph%' triggers a full table scan. Similarly, on plain keyword or text fields, directly running wildcard, regexp, fuzzy or unindexed prefix queries, the engine must scan huge dictionaries because there is no dedicated index structure, which easily causes query timeouts and slows down the shared cluster.

Agentic ES enables expensive-query protection by default (search.allow_expensive_queries = false), which blocks slow queries that have no dedicated index support. By choosing the right dedicated field type for your query pattern when creating the collection, these queries run at high speed:

image

Query syntax

Recommended field type for the collection (high-speed)

Commonly misused fields (no dedicated index; blocked by protection, returning 400)

wildcard (the */?), regexp (regex), fuzzy (fuzzy correction);

match / multi_match with fuzziness;

query_string or the URL parameter q with *, ~, /regex/

wildcard (builds a dedicated index for substring and regex matching at any position)

plain keyword, text, flattened

prefix (prefix query), match_bool_prefix

Option A: add text field with "index_prefixes": {} (by default accelerates 1–5 character prefixes; for longer prefixes, set e.g. "index_prefixes": {"min_chars": 1, "max_chars": 10}, max_chars up to 19)

Option B: use search_as_you_type (prefixes up to 20 characters) or wildcard field type (no prefix length limit)

keyword, flattened;

Without index_prefixes enabled, a plain text (or when the prefix length exceeds max_chars / search_as_you_type's 20-character limit)

2. Complete hands-on example: create the collection, write data, and run scenario-based queries

The following example uses a standalone product catalog collection sku-catalog, configuring two dedicated fields for their respective purposes (you can copy the following requests directly into the console Kibana Dev Tools and run them in order):

  • sku_code (wildcard): lets operations staff troubleshoot product codes by *wildcard* at any position, or by regex;

  • product_name (text + index_prefixes): lets the frontend search box provide real-time autocomplete as the user types a partial word.

// 1. Create the collection and configure dedicated field types (index_prefixes accelerates only 1–5 characters by default; here it is set to a maximum of 10 characters)
PUT /_slice_collection/sku-catalog
{
  "default_slice": "main",
  "mappings": {
    "properties": {
      "sku_code": { "type": "wildcard" },
      "product_name": {
        "type": "text",
        "index_prefixes": { "min_chars": 1, "max_chars": 10 }
      }
    }
  }
}

// 2. Write two sample product records
POST /sku-catalog/_bulk
{"index":{"_id":"1"}}
{"sku_code":"APPLE-IPHONE-16-PRO","product_name":"iPhone 16 Pro case"}
{"index":{"_id":"2"}}
{"sku_code":"APPLE-MACBOOK-M4","product_name":"MacBook Pro 14 laptop"}

// 3. After waiting a few seconds for background auto-refresh, scenario 1: troubleshoot codes by wildcard at any position (matches document 1)
//    Note: the wildcard field is case-sensitive by default; to ignore case, explicitly set "case_insensitive": true
POST /sku-catalog/_search?allow_partial_search_results=false
{
  "query": {
    "wildcard": {
      "sku_code": { "value": "*iph*16*", "case_insensitive": true }
    }
  }
}

// 4. Scenario 2: run regex matching by code pattern (matches document 1)
POST /sku-catalog/_search?allow_partial_search_results=false
{
  "query": {
    "regexp": { "sku_code": "APPLE-IPHONE-[0-9]+-PRO" }
  }
}

// 5. Scenario 3: search box autocomplete (the user has just typed the partial word "macboo", , real-time autocomplete matches document 2)
POST /sku-catalog/_search?allow_partial_search_results=false
{
  "query": {
    "match_bool_prefix": { "product_name": "macboo" }
  }
}

// 6. Clean up the sample collection (optional)
DELETE /_slice_collection/sku-catalog

Expected results:

  • Step 3 (code wildcard): Matches document 1 (APPLE-IPHONE-16-PRO). wildcard field is case-sensitive by default; to match an uppercase code with a lowercase condition, enable "case_insensitive": true.

  • Step 4 (code regex): Exactly matches APPLE-IPHONE-<digits>-PRO, hitting document 1 and returning HTTP 200.

  • Step 5 (search box autocomplete): After the user types the 6-character partial word "macboo", the prefix index matches document 2 ("MacBook Pro 14 laptop"); here it is configured with max_chars: 10, prefixes longer than 5 characters can also use the prefix index for high-speed execution (if you keep the default index_prefixes: {}, prefixes longer than 5 characters trigger 400).

    For related references, see the Elasticsearch documentation: Keyword / Wildcard field, Wildcard query, Prefix query, index_prefixes and search_as_you_type.