Skip to content

SOLR-18256: Fix lucene QParser to support nested pure negative queries#4552

Open
abumarjikar wants to merge 1 commit into
apache:mainfrom
abumarjikar:SOLR_18256_autofix_negative_lucene_clauses
Open

SOLR-18256: Fix lucene QParser to support nested pure negative queries#4552
abumarjikar wants to merge 1 commit into
apache:mainfrom
abumarjikar:SOLR_18256_autofix_negative_lucene_clauses

Conversation

@abumarjikar

Copy link
Copy Markdown
Contributor

https://issues.apache.org/jira/browse/SOLR-18256

Description

This pull request addresses a long-standing limitation where nested pure negative (NOT) queries wrapped in parentheses or sub-expressions inside the lucene QParser evaluate incorrectly (resulting in 0 matching documents). By overriding getBooleanQuery in SolrQueryParser, we now safely apply QueryUtils.makeQueryable() on every boolean group built during the parsing cycle. This correctly transforms and executes both top-level and nested pure negative sub-expressions as expected.

Solution

Modified SolrQueryParser to override getBooleanQuery(List<BooleanClause> clauses). Instead of relying purely on top-level query transformations to fix pure negative clauses, the parser now intercepts every boolean group built during syntax processing. If a generated BooleanQuery contains only prohibited (negative) clauses, QueryUtils.makeQueryable() automatically wraps or balances it against a MatchAllDocsQuery, restoring expected Lucene/Solr negation behavior seamlessly across nested parenthesized groups.

Tests

  • Integrated a dedicated behavioral test case testNestedPureNegativeQuery inside solr/core/src/test/org/apache/solr/search/TestSolrQueryParser.java.
  • Verified top-level and nested pure negative behavior under standard parenthesized groups using explicit AND (-field:val) and AND (NOT field:val) syntax variations against an isolated set of unique test documents.
  • Ensured reliable query assertions by introducing an explicit sort parameter (id asc) to circumvent score weighting conflicts during JSON response validation.

Checklist

Please review the following and check all that apply:

  • I have reviewed the guidelines for How to Contribute and my code conforms to the standards described there to the best of my ability.
  • I have created a Jira issue and added the issue ID to my pull request title.
  • I have given Solr maintainers access to contribute to my PR branch. (optional but recommended, not available for branches on forks living under an organisation)
  • I have developed this patch against the main branch.
  • I have run ./gradlew check.
  • I have added tests for my changes.
  • I have added documentation for the Reference Guide
  • I have added a changelog entry for my change

@dsmiley dsmiley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should enable broad improvements across tests where a silly *:* is present next to a negative clause. Maybe not that many tests to update but still. Perhaps even the ref guide has a reference. Please search for the pattern. I suggest waiting until after your #4512 to avoid merge conflicts.

Comment on lines +31 to +34
@Override
protected Query getBooleanQuery(List<BooleanClause> clauses) throws SyntaxError {
return QueryUtils.makeQueryable(super.getBooleanQuery(clauses));
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow... boy this was simple.

}
}

@Test

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not seeing here a pure negative query... you've put a MUST clause on each one.

@dsmiley dsmiley requested a review from hossman June 26, 2026 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants