OrchardCMS/OrchardCore · error · ArgumentException
Invalid prefix query
Error message
Invalid prefix query
What it means
PrefixQueryProvider.CreateQuery dispatches on the JSON value kind of the query node. A prefix query must be expressed as a JSON object; any other kind (string, number, array, etc.) reaches the switch's default case and throws ArgumentException. This guards against malformed query documents.
Solutions
- Wrap the query spec in a JSON object: {"field": {"type": "prefix", "prefix": "value"}}.
- Check the code path producing the query JSON and ensure it emits an object for prefix queries.
- Log/inspect the incoming query JSON to find which field node has the wrong shape.
Example fix
// before
{"Title": "Orch"}
// after
{"Title": {"type": "prefix", "prefix": "Orch"}} Defensive patterns
Strategy: type-guard
Validate before calling
if (node?.GetValueKind() == JsonValueKind.Object) { /* safe to call */ } Type guard
static bool IsPrefixQueryObject(JsonNode node) => node is JsonObject o && o.TryGetPropertyValue("type", out var t) && t.GetValueKind() == JsonValueKind.String && t.GetValue<string>() == "prefix"; Try / catch
try { var q = provider.CreateQuery(node); } catch (ArgumentException ex) when (ex.Message == "Invalid prefix query") { // log the raw node JSON for diagnosis } Prevention
- Wrap every query spec in an object with a "type" discriminator.
- Never flatten query specs to plain JSON values.
- Round-trip test generated query JSON through the provider in unit tests.
When it happens
Trigger: Passing a query node whose value kind is not JsonValueKind.Object, e.g. {"field": "abc"} or {"field": ["a"]} where a prefix-query object is expected.
Common situations: Queries built programmatically with the wrong shape; JSON where the type discriminator object was accidentally flattened to a plain value; copy-paste errors in recipe files.
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
- Invalid term in terms query
- Invalid terms query
- Invalid wildcard query
- Invalid fuzzy query
- Invalid query
AI-assisted analysis of OrchardCMS/OrchardCore@4306c0717f (2026-09-13).
Data as JSON: /api/errors/af624100ecd09183.
Report an issue: GitHub.
Appendix: source
Thrown at src/OrchardCore/OrchardCore.Lucene.Core/QueryProviders/PrefixQueryProvider.cs:50
prefixQuery = new PrefixQuery(new Term(first.Key, value.Value<string>()));
}
else if (obj.TryGetPropertyValue("prefix", out var prefix))
{
prefixQuery = new PrefixQuery(new Term(first.Key, prefix.Value<string>()));
}
else
{
throw new ArgumentException("Prefix query misses prefix value");
}
if (obj.TryGetPropertyValue("boost", out var boost))
{
prefixQuery.Boost = boost.Value<float>();
}
return prefixQuery;
default: throw new ArgumentException("Invalid prefix query");
}
}
}
View on GitHub (pinned to 4306c0717f)