{"record":{"id":"2de2fdc67957ebc9","repo":"OrchardCMS/OrchardCore","slug":"invalid-term-query","errorCode":null,"errorMessage":"Invalid term query","messagePattern":"Invalid term query","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.Lucene.Core/QueryProviders/Filters/TermFilterProvider.cs","lineNumber":43,"sourceCode":"        // A term query has only one member, which can either be a string or an object\n        TermQuery termQuery;\n\n        switch (first.Value.GetValueKind())\n        {\n            case JsonValueKind.String:\n                termQuery = new TermQuery(new Term(first.Key, first.Value.ToString()));\n                break;\n            case JsonValueKind.Object:\n                var obj = first.Value.AsObject();\n                var value = obj[\"value\"].Value<string>();\n                termQuery = new TermQuery(new Term(first.Key, value));\n\n                if (obj.TryGetPropertyValue(\"boost\", out var boost))\n                {\n                    termQuery.Boost = boost.Value<float>();\n                }\n                break;\n            default: throw new ArgumentException(\"Invalid term query\");\n        }\n\n        booleanQuery.Add(termQuery, Occur.MUST);\n        var queryFilter = new QueryWrapperFilter(termQuery);\n\n        return new FilteredQuery(booleanQuery, queryFilter);\n    }\n}\n","sourceCodeStart":25,"sourceCodeEnd":52,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.Lucene.Core/QueryProviders/Filters/TermFilterProvider.cs#L25-L52","documentation":"TermFilterProvider.CreateFilteredQuery builds a Lucene TermQuery from a term filter. The term value must be in a supported shape (an object with \"value\"/optional \"boost\", or the accepted simple form); if the value's JSON kind reaches the switch default, the provider throws ArgumentException(\"Invalid term query\") because it cannot derive a term from the supplied JSON.","triggerScenarios":"Calling CreateFilteredQuery with {\"term\": {\"Status\": [\"a\", \"b\"]}} (array), {\"term\": {\"Status\": {\"boost\": 2}}} (object without \"value\"), or {\"term\": {\"Status\": null}} — value kinds that fall through to the default branch.","commonSituations":"Using Elasticsearch-style term arrays for multi-value matching (not supported here; use a different filter); an object carrying only boost with the value removed; null values from dynamic query builders for empty filters; copy-paste between term and match filter syntaxes.","solutions":["Provide a scalar value or an object with a \"value\" property: {\"term\": {\"Status\": {\"value\": \"Published\"}}}.","For multiple values, issue multiple term filters or use the match filter instead of an array under term.","Never omit \"value\" when using the object form; put options like boost alongside a present \"value\".","Coerce null/undefined filter values to a concrete string (or drop the filter) before calling CreateFilteredQuery."],"exampleFix":"// before\n{\"term\": {\"Status\": {\"boost\": 2}}}\n// after\n{\"term\": {\"Status\": {\"value\": \"Published\", \"boost\": 2}}}","handlingStrategy":"validation","validationCode":"if (filter.TryGetPropertyValue(field, out var v))\n{\n    var ok = v.ValueKind is JsonValueKind.String or JsonValueKind.Number or JsonValueKind.Object;\n    if (ok && v.ValueKind == JsonValueKind.Object)\n        ok = v.AsObject().TryGetPropertyValue(\"value\", out _);\n    if (!ok)\n        throw new InvalidOperationException($\"term filter for '{field}' needs a scalar value or object with 'value'.\");\n}","typeGuard":"static bool IsValidTermValue(JsonElement v) =>\n    v.ValueKind is JsonValueKind.String or JsonValueKind.Number ||\n    (v.ValueKind == JsonValueKind.Object && v.AsObject().TryGetPropertyValue(\"value\", out _));","tryCatchPattern":"try\n{\n    var query = provider.CreateFilteredQuery(context);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Invalid term\"))\n{\n    _logger.LogError(ex, \"Unsupported term filter value: {Json}\", filterJson);\n    throw new QueryValidationException(\"term filters require a scalar or object with 'value'.\", ex);\n}","preventionTips":["Never pass arrays or null as a term filter value; use match filters for multi-term matching.","Keep \"value\" present when using the object form with boost.","Drop filters with unset values instead of serializing null."],"tags":["lucene","query-filter","invalid-argument","term-query"],"backgroundTag":"invalid-argument-format","analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}