Configurable Fuzzy Search Parameters in Amazon OpenSearch Service Tutorials

The AWS OpenSearch Service tutorial repository exposes three configurable fuzzy search parameters—fuzziness, prefix_length, and fuzzy_transpositions—which control edit distance, exact-match character requirements, and transposition handling within the multi_match query builder.

The aws-samples/sample-for-amazon-opensearch-service-tutorials-101 repository demonstrates how to implement tolerant text matching using configurable fuzzy search parameters in Amazon OpenSearch Service. Understanding these settings allows developers to fine-tune search relevance by adjusting how strictly the engine matches terms with typographical variations.

How Fuzzy Search Works in the Sample Implementation

When the Lambda function receives a request with "search_type": "fuzzy", it enriches a standard multi_match query with fuzzy-specific parameters. In artifacts/search_lambda/opensearch_search.py (lines 194-200), the code updates the query dictionary to enable approximate string matching while preserving the best_fields search strategy.

Configurable Fuzzy Search Parameters Explained

The sample code implements three primary parameters that govern fuzzy matching behavior:

fuzziness

Controls the maximum edit distance allowed between the search term and indexed terms. The sample defaults to "AUTO", which automatically selects an edit distance of 0 for terms 1-2 characters, 1 for terms 3-5 characters, and 2 for longer terms. You can replace this with fixed integers (1 or 2) to enforce consistent tolerance regardless of term length.

prefix_length

Specifies the number of initial characters that must match exactly before fuzziness is applied. The tutorial sets this to 2, meaning the first two characters must match precisely while subsequent characters may vary. Increasing this value reduces false positives by requiring stronger initial alignment; decreasing it to 0 or 1 broadens result sets.

fuzzy_transpositions

Determines whether swapping two adjacent characters (e.g., "ab" → "ba") counts as a single edit. The sample enables this (True), treating transpositions as one change rather than two separate substitutions. Setting this to False makes the matching more strict, requiring exact character order except for substitutions, insertions, or deletions.

Customizing Parameters in opensearch_search.py

To modify these values, edit the conditional block in artifacts/search_lambda/opensearch_search.py where search_type == "fuzzy":

if search_type == "fuzzy":
    text_search["multi_match"].update({
        "fuzziness": "AUTO",
        "prefix_length": 2,
        "fuzzy_transpositions": True
    })

Example: Increasing Search Tolerance

For broader matching that catches more typos but may reduce precision:

if search_type == "fuzzy":
    text_search["multi_match"].update({
        "fuzziness": "2",
        "prefix_length": 1,
        "fuzzy_transpositions": True
    })

Example: Strict Fuzzy Matching

For tighter control requiring exact initial characters and no transposition support:

if search_type == "fuzzy":
    text_search["multi_match"].update({
        "fuzziness": "1",
        "prefix_length": 3,
        "fuzzy_transpositions": False
    })

Request and Query Structure

When invoking the fuzzy search, the API payload remains simple while the Lambda constructs the complex OpenSearch query:

{
  "search_type": "fuzzy",
  "search_value": "sneaker",
  "fields": [
    { "name": "title", "type": "text" },
    { "name": "description", "type": "text" }
  ]
}

This generates the following multi_match clause with the configurable fuzzy search parameters embedded:

{
  "multi_match": {
    "query": "sneaker",
    "fields": ["title^3", "description^2"],
    "type": "best_fields",
    "fuzziness": "AUTO",
    "prefix_length": 2,
    "fuzzy_transpositions": true
  }
}

Summary

  • The aws-samples/sample-for-amazon-opensearch-service-tutorials-101 repository implements fuzzy search via multi_match query enrichment in artifacts/search_lambda/opensearch_search.py (lines 194-200).
  • Three configurable fuzzy search parameters control matching behavior: fuzziness (edit distance), prefix_length (exact-match prefix requirement), and fuzzy_transpositions (adjacent character swap handling).
  • Default values are "AUTO", 2, and True respectively, balancing tolerance and precision for typical e-commerce use cases.
  • Modifying the source code allows customization of these parameters to suit specific domain requirements without changing the API contract.

Frequently Asked Questions

What is the default fuzziness setting in the AWS OpenSearch tutorial?

The sample code uses "AUTO" for the fuzziness parameter, which instructs OpenSearch to automatically determine the edit distance based on term length: 0 for 1-2 character terms, 1 for 3-5 character terms, and 2 for longer terms. This provides adaptive tolerance without manual tuning.

How does prefix_length affect fuzzy search results?

The prefix_length parameter requires the first N characters to match exactly before applying fuzzy logic. With the default value of 2, terms must share their first two characters exactly; subsequent characters may differ within the specified edit distance. Increasing this value to 3 or higher reduces false positives by requiring stronger initial alignment.

Can I configure fuzzy search parameters without modifying the source code?

No, the current implementation hardcodes these values in opensearch_search.py. To expose them as runtime configuration options, you would need to refactor the Lambda handler to read these parameters from the incoming request JSON or environment variables instead of using static values.

The implementation uses the multi_match query with type: "best_fields", which searches across multiple fields and returns documents matching any field, using the highest relevance score from any single field. The fuzzy parameters are added to this standard query structure when search_type is set to "fuzzy".

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →