diff --git a/solr/core/src/java/org/apache/solr/search/join/BlockJoinParentQParser.java b/solr/core/src/java/org/apache/solr/search/join/BlockJoinParentQParser.java index 1d73bbd78aa..b1b7e66a212 100644 --- a/solr/core/src/java/org/apache/solr/search/join/BlockJoinParentQParser.java +++ b/solr/core/src/java/org/apache/solr/search/join/BlockJoinParentQParser.java @@ -20,6 +20,8 @@ import java.io.UncheckedIOException; import java.util.Objects; import org.apache.lucene.index.LeafReaderContext; +import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.ConstantScoreScorer; import org.apache.lucene.search.ConstantScoreWeight; import org.apache.lucene.search.DocIdSetIterator; @@ -38,10 +40,14 @@ import org.apache.solr.request.SolrQueryRequest; import org.apache.solr.search.ExtendedQueryBase; import org.apache.solr.search.QParser; + + import org.apache.solr.search.SolrCache; import org.apache.solr.search.SyntaxError; import org.apache.solr.util.SolrDefaultScorerSupplier; +import static org.apache.solr.search.QueryUtils.makeQueryable; + public class BlockJoinParentQParser extends FiltersQParser { /** implementation detail subject to change */ public static final String CACHE_NAME = "perSegFilter"; @@ -68,10 +74,22 @@ protected Query parseParentFilter() throws SyntaxError { } @Override - protected Query wrapSubordinateClause(Query subordinate) throws SyntaxError { + protected Query wrapSubordinateClause(Query childrenQuery) throws SyntaxError { String scoreMode = localParams.get("score", ScoreMode.None.name()); Query parentQ = parseParentFilter(); - return createQuery(parentQ, subordinate, scoreMode); + + if (childrenQuery instanceof BooleanQuery bq) { + + if (bq.clauses().size() == 1) { + BooleanClause bqToFix = bq.clauses().get(0); + Query fixChildQuery = makeQueryable(bqToFix.query()); + BooleanQuery.Builder filterFixedChildrenQuery = new BooleanQuery.Builder(); + filterFixedChildrenQuery.add(fixChildQuery, BooleanClause.Occur.FILTER); + return createQuery(parentQ, filterFixedChildrenQuery.build(), scoreMode); + } + } + + return createQuery(parentQ, childrenQuery, scoreMode); } @Override diff --git a/solr/core/src/test/org/apache/solr/search/join/BJQParserTest.java b/solr/core/src/test/org/apache/solr/search/join/BlockJoinQueryParserTest.java similarity index 98% rename from solr/core/src/test/org/apache/solr/search/join/BJQParserTest.java rename to solr/core/src/test/org/apache/solr/search/join/BlockJoinQueryParserTest.java index bed4212bbca..545f83d5879 100644 --- a/solr/core/src/test/org/apache/solr/search/join/BJQParserTest.java +++ b/solr/core/src/test/org/apache/solr/search/join/BlockJoinQueryParserTest.java @@ -42,7 +42,7 @@ import org.junit.Test; import org.junit.rules.TestRule; -public class BJQParserTest extends SolrTestCaseJ4 { +public class BlockJoinQueryParserTest extends SolrTestCaseJ4 { private static final String[] klm = new String[] {"k", "l", "m"}; private static final List xyz = Arrays.asList("x", "y", "z"); @@ -163,7 +163,7 @@ public void testFull() { "//doc/arr[@name=\"parent_s\"]/str='c'", "//doc/arr[@name=\"parent_s\"]/str='d'", "//doc/arr[@name=\"parent_s\"]/str='e'", - "//doc/arr[@name=\"parent_s\"]/str='f'" + "//doc/arr[@name=\"parent_s\"]/str='f'", }; @Test @@ -639,6 +639,18 @@ public void testFiltersCache() throws SyntaxError, IOException { } } + @Test + public void testPureNegativeQueryInChildFilters() { + assertQ( + req( + "q", "{!parent tag=top filters=$childFq which=$pq}", + "pq", "parent_s:[* TO *]", + "childFq", "-child_s:l -child_s:k" + ), + "//*[@numFound='6']" + ); + } + @After public void cleanAfterTestFiltersCache() { assertU("should be noop", delI("12275")); diff --git a/solr/core/src/test/org/apache/solr/search/join/another/BJQFilterAccessibleTest.java b/solr/core/src/test/org/apache/solr/search/join/another/BlockJoinQueryFilterAccessibleTest.java similarity index 93% rename from solr/core/src/test/org/apache/solr/search/join/another/BJQFilterAccessibleTest.java rename to solr/core/src/test/org/apache/solr/search/join/another/BlockJoinQueryFilterAccessibleTest.java index d6c5dcf3bb0..84cf45c3e22 100644 --- a/solr/core/src/test/org/apache/solr/search/join/another/BJQFilterAccessibleTest.java +++ b/solr/core/src/test/org/apache/solr/search/join/another/BlockJoinQueryFilterAccessibleTest.java @@ -17,7 +17,7 @@ package org.apache.solr.search.join.another; -import static org.apache.solr.search.join.BJQParserTest.createIndex; +import static org.apache.solr.search.join.BlockJoinQueryParserTest.createIndex; import java.io.IOException; import org.apache.lucene.index.Term; @@ -34,7 +34,7 @@ import org.junit.ClassRule; import org.junit.rules.TestRule; -public class BJQFilterAccessibleTest extends SolrTestCaseJ4 { +public class BlockJoinQueryFilterAccessibleTest extends SolrTestCaseJ4 { @ClassRule public static final TestRule noReverseMerge = RandomNoReverseMergePolicyFactory.createRule();