{"record":{"id":"780160c7381b78b0","repo":"OrchardCMS/OrchardCore","slug":"invalid-terms-query-termsqueryprovider","errorCode":null,"errorMessage":"Invalid terms query","messagePattern":"Invalid terms query","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.Lucene.Core/QueryProviders/TermsQueryProvider.cs","lineNumber":40,"sourceCode":"        switch (first.Value.GetValueKind())\n        {\n            case JsonValueKind.Array:\n\n                foreach (var item in first.Value.AsArray())\n                {\n                    if (item.GetValueKind() != JsonValueKind.String)\n                    {\n                        throw new ArgumentException($\"Invalid term in terms query\");\n                    }\n\n                    boolQuery.Add(new TermQuery(new Term(field, item.Value<string>())), Occur.SHOULD);\n                }\n\n                break;\n            case JsonValueKind.Object:\n                throw new ArgumentException(\"The terms lookup query is not supported\");\n\n            default: throw new ArgumentException(\"Invalid terms query\");\n        }\n\n        return boolQuery;\n    }\n}\n","sourceCodeStart":22,"sourceCodeEnd":46,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.Lucene.Core/QueryProviders/TermsQueryProvider.cs#L22-L46","documentation":"TermsQueryProvider only accepts a JSON array of values for the terms field. Any JSON kind other than Array or Object (string, number, bool, null) falls into the default case and throws ArgumentException.","triggerScenarios":"Calling CreateQuery with a terms query whose field value is a scalar string/number/boolean/null, e.g. {\"terms\": {\"myField\": \"singleValue\"}}.","commonSituations":"Hand-writing Lucene query JSON and supplying a single term as a plain string instead of an array; programmatic query builders appending one value without wrapping it in a list.","solutions":["Wrap the value in a JSON array: {\"terms\": {\"myField\": [\"singleValue\"]}}.","Validate the query JSON shape before passing it to the provider.","If you only need one term, use a Term-based query (e.g. term query) instead of terms."],"exampleFix":"// before\n{\"terms\": {\"category\": \"news\"}}\n// after\n{\"terms\": {\"category\": [\"news\"]}}","handlingStrategy":"validation","validationCode":"if (terms.ValueKind != JsonValueKind.Array)\n    throw new InvalidOperationException(\"Terms query value must be a JSON array of term values.\");","typeGuard":"static bool IsTermsArray(JsonElement el) => el.ValueKind == JsonValueKind.Array && el.GetArrayLength() > 0;","tryCatchPattern":"catch (ArgumentException ex) when (ex.Message == \"Invalid terms query\")\n{\n    // wrap single scalar into an array and retry, or report invalid query\n}","preventionTips":["Always emit terms values as JSON arrays, even for a single term.","Validate query JSON against the provider's expected schema before dispatch.","Use typed query builder objects rather than raw JSON strings."],"tags":["lucene","query","json"],"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-16T04:17:20.429Z"}