{"record":{"id":"838b93f3151b0e40","repo":"theonedev/onedev","slug":"regex-query-is-too-general-regex","errorCode":null,"errorMessage":"Regex query is too general: ${regex}","messagePattern":"Regex query is too general: (.+?)","errorType":"validation","errorClass":"TooGeneralQueryException","httpStatus":406,"severity":"error","filePath":"server-core/src/main/java/io/onedev/server/search/code/query/regex/RegexLiterals.java","lineNumber":70,"sourceCode":"\t * @throws TooGeneralQueryException\n\t */\n\tpublic Query asNGramQuery(String fieldName, int gramSize) throws TooGeneralQueryException {\n\t\tBooleanQuery.Builder orQueryBuilder = new BooleanQuery.Builder();\n\t\tfor (List<LeafLiterals> row: rows) {\n\t\t\tBooleanQuery.Builder andQueryBuilder = new BooleanQuery.Builder();\n\t\t\tfor (LeafLiterals literals: row) {\n\t\t\t\tif (literals.getLiteral() != null && literals.getLiteral().length()>=NGRAM_SIZE)\n\t\t\t\t\tandQueryBuilder.add(new NGramLuceneQuery(fieldName, literals.getLiteral(), gramSize), Occur.MUST);\n\t\t\t}\n\t\t\tBooleanQuery andQuery = andQueryBuilder.build();\n\t\t\tif (andQuery.clauses().size() != 0)\n\t\t\t\torQueryBuilder.add(andQuery, Occur.SHOULD);\n\t\t}\n\t\tBooleanQuery orQuery = orQueryBuilder.build();\n\t\tif (orQuery.clauses().size() != 0)\n\t\t\treturn orQuery;\n\t\telse\n\t\t\tthrow new TooGeneralQueryException(\"Regex query is too general: \" + regex);\n\t}\n\n\t@Override\n\tpublic String toString() {\n\t\tStringBuilder orBuilder = new StringBuilder();\n\t\tfor (List<LeafLiterals> row: rows) {\n\t\t\tStringBuilder andBuilder = new StringBuilder(); \n\t\t\tfor (LeafLiterals literals: row) {\n\t\t\t\tif (!Strings.isNullOrEmpty(literals.getLiteral())) {\n\t\t\t\t\tif (andBuilder.length() != 0)\n\t\t\t\t\t\tandBuilder.append(\"&\");\n\t\t\t\t\tandBuilder.append(literals.getLiteral());\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (orBuilder.length() != 0)\n\t\t\t\torBuilder.append(\"|\");\n\t\t\torBuilder.append(andBuilder);\n\t\t}","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/theonedev/onedev/blob/d44925c47c37992c828ea673a5f9620539bc3ff2/server-core/src/main/java/io/onedev/server/search/code/query/regex/RegexLiterals.java#L52-L88","documentation":"RegexLiterals.asNGramQuery converts a regex into Lucene n-gram queries built from literal substrings extracted from the pattern. If the regex contains no extractable literal content (pure alternations of wildcards/anchors, producing zero OR-clauses), TooGeneralQueryException is thrown because nothing can be indexed efficiently.","triggerScenarios":"Calling asNGramQuery on a RegexLiterals built from a pattern with no literal characters, e.g. '.*', '[a-z]*', '(a|b)*' — the derived BooleanQuery has no clauses so the exception is thrown.","commonSituations":"User submits a purely wildcard regex like '.*' into regex-based code search; a programmatic TextQuery with regex=true built from an unanchored dot-star pattern.","solutions":["Include literal characters in the regex (e.g. 'foo.*bar' instead of '.*'), since literals are extracted into n-gram terms.","Anchor the pattern with concrete text; drop redundant '.*' prefixes/suffixes — they are implicit.","Catch TooGeneralQueryException at the search entry and ask the user for a more specific pattern."],"exampleFix":"// before\nnew RegexLiterals(\".*\").asNGramQuery(BLOB_TEXT.name(), NGRAM_SIZE);\n// after\nnew RegexLiterals(\"foo.*bar\").asNGramQuery(BLOB_TEXT.name(), NGRAM_SIZE);","handlingStrategy":"validation","validationCode":"if (regex != null && regex.replaceAll(\"[\\\\W_]|\\\\\\\\[dDwWsS]\", \"\").isEmpty())\n    throw new IllegalArgumentException(\"Regex has no literal content for n-gram indexing\");","typeGuard":"boolean hasRegexLiterals(String pattern) {\n    return pattern != null && pattern.replaceAll(\"[^A-Za-z0-9]\", \"\").length() > 0;\n}","tryCatchPattern":"try {\n    q = new RegexLiterals(pattern).asNGramQuery(field, ngramSize);\n} catch (TooGeneralQueryException e) {\n    return Result.error(\"Regex too general: \" + pattern);\n}","preventionTips":["Require concrete literal substrings in regex code search patterns","Strip redundant leading/trailing '.*' from user patterns","Catch TooGeneralQueryException at the regex search entry point"],"tags":["regex","lucene","code-search"],"backgroundTag":"invalid-regex-pattern","analyzedSha":"d44925c47c37992c828ea673a5f9620539bc3ff2","analyzedAt":"2026-09-06T07:18:27.995Z","contentChangedAt":"2026-09-06T07:18:27.995Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}