OrchardCMS/OrchardCore · error · ArgumentException

Invalid fuzzy query

Error message

Invalid fuzzy query

What it means

This is the fall-through case of FuzzyQueryProvider.CreateQuery: the field's JSON value is neither a scalar string nor the supported object form (e.g. an array, number, or null), so the provider cannot interpret it as a fuzzy query and throws.

Solutions

  1. Provide the term as a JSON string: {"title": "werd"}.
  2. Or use the object form: {"title": {"value": "werd", "fuzziness": 2}}.
  3. Guard against null/undefined values reaching the query builder from templates.

Example fix

// before
{"title": ["werd"]}
// after
{"title": {"value": "werd", "fuzziness": 2}}
Defensive patterns

Strategy: validation

Validate before calling

var k = query.Value.ValueKind;
if (k is not (JsonValueKind.String or JsonValueKind.Object))
    throw new ArgumentException("fuzzy query value must be a string or an object with 'value'");

Type guard

static bool IsFuzzyShape(JsonElement el) =>
    el.ValueKind == JsonValueKind.String ||
    (el.ValueKind == JsonValueKind.Object && el.TryGetProperty("value", out var v) && v.ValueKind == JsonValueKind.String);

Try / catch

try { var query = provider.CreateQuery(context, queryJson); }
catch (ArgumentException ex) { return BadRequest($"Invalid fuzzy query: {ex.Message}"); }

Prevention

When it happens

Trigger: Calling CreateQuery with a filter like {"title": ["a", "b"]} or {"title": null} — any value shape other than string or object with "value".

Common situations: Copy-pasted terms-query JSON into a fuzzy query; null values from unbound template variables; arrays of fuzzy terms which the provider does not support.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of OrchardCMS/OrchardCore@4306c0717f (2026-09-13). Data as JSON: /api/errors/6f5ae9d4382bebc9. Report an issue: GitHub.

Appendix: source

Thrown at src/OrchardCore/OrchardCore.Lucene.Core/QueryProviders/FuzzyQueryProvider.cs:50

                obj.TryGetPropertyValue("fuzziness", out var fuzziness);
                obj.TryGetPropertyValue("prefix_length", out var prefixLength);
                obj.TryGetPropertyValue("max_expansions", out var maxExpansions);

                var fuzzyQuery = new FuzzyQuery(
                    new Term(first.Key, value.Value<string>()),
                    fuzziness?.Value<int>() ?? LevenshteinAutomata.MAXIMUM_SUPPORTED_DISTANCE,
                    prefixLength?.Value<int>() ?? 0,
                    maxExpansions?.Value<int>() ?? 50,
                    true);

                if (obj.TryGetPropertyValue("boost", out var boost))
                {
                    fuzzyQuery.Boost = boost.Value<float>();
                }

                return fuzzyQuery;
            default: throw new ArgumentException("Invalid fuzzy query");
        }
    }
}

View on GitHub (pinned to 4306c0717f)