Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type: fixed
title: Fix pure negative (NOT) sub-expression queries in lucene QParser
authors:
- name: Abhishek Umarjikar
nick: abumarjikar
links:
- name: SOLR-18256
url: https://issues.apache.org/jira/browse/SOLR-18256
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
package org.apache.solr.search;

import java.util.List;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.Query;
import org.apache.solr.parser.QueryParser;

/** Solr's default query parser, a schema-driven superset of the classic lucene query parser. */
Expand All @@ -24,4 +27,9 @@ public class SolrQueryParser extends QueryParser {
public SolrQueryParser(QParser parser, String defaultField) {
super(defaultField, parser);
}

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

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.

}
28 changes: 28 additions & 0 deletions solr/core/src/test/org/apache/solr/search/TestSolrQueryParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1901,4 +1901,32 @@ public void testFieldExistsQueries() throws SyntaxError {
}
}
}

@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.

public void testNestedPureNegativeQuery() throws Exception {
// Standard sample data with completely unique field values to isolate matches
assertU(adoc("id", "9414", "v_t", "pureneg foo bar"));
assertU(adoc("id", "9415", "v_t", "pureneg foo baz"));
assertU(adoc("id", "9416", "v_t", "pureneg baz"));
assertU(commit());

// Top-level negative query must exclude 'bar' but successfully find our other docs
// Force sort by ID so the array index expectations always line up perfectly
assertJQ(
req("q", "v_t:pureneg AND -v_t:bar", "df", "v_t", "sort", "id asc"),
"/response/docs/[0]/id=='9415'",
"/response/docs/[1]/id=='9416'");

// Nested pure negative query inside a parenthesized group with AND
assertJQ(
req("q", "v_t:pureneg AND v_t:foo AND (-v_t:bar)", "df", "v_t"),
"/response/numFound==1",
"/response/docs/[0]/id=='9415'");

// Nested pure negative query using explicit NOT syntax
assertJQ(
req("q", "v_t:pureneg AND v_t:foo AND (NOT v_t:bar)", "df", "v_t"),
"/response/numFound==1",
"/response/docs/[0]/id=='9415'");
}
}
Loading