msfroh commented on code in PR #14728: URL: https://github.com/apache/lucene/pull/14728#discussion_r2112783861
########## lucene/join/src/test/org/apache/lucene/search/join/TestParentsChildrenBlockJoinQuery.java: ########## @@ -0,0 +1,186 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.lucene.search.join; + +import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.document.StringField; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.Term; +import org.apache.lucene.search.*; +import org.apache.lucene.store.Directory; +import org.apache.lucene.tests.index.RandomIndexWriter; +import org.apache.lucene.tests.util.LuceneTestCase; +import org.junit.Test; + +public class TestParentsChildrenBlockJoinQuery extends LuceneTestCase { + + private final TestCase testCase; + + public TestParentsChildrenBlockJoinQuery(TestCase testCase) { + this.testCase = testCase; + } + + @ParametersFactory + public static Collection<Object[]> testCases() { + return Arrays.asList( + new Object[] {new TestCase("EmptyIndex", 10, 0, new int[0], new TestDoc[0][0])}, + new Object[] { + new TestCase( + "OnlyParentDocs", + 10, + 0, + new int[0], + new TestDoc[][] { + { + new TestDoc("parent", true), + }, + { + new TestDoc("parent", true), + }, + { + new TestDoc("parent", true), + } + }) + }, + new Object[] { + new TestCase( + "FirstParentWithoutChild", + 10, + 2, + new int[] {1, 2}, + new TestDoc[][] { + { + new TestDoc("parent", true), + }, + { + new TestDoc("child", true), + }, + { + new TestDoc("child", true), + }, + { + new TestDoc("parent", true), + } Review Comment: Shouldn't this be: ``` new TestDoc[][] { { new TestDoc("parent", true) }, { new TestDoc("child", true), new TestDoc("child", true), new TestDoc("parent", true) } } ``` That is, shouldn't the children be part of the second parent's block? ########## lucene/join/src/test/org/apache/lucene/search/join/TestParentsChildrenBlockJoinQuery.java: ########## @@ -0,0 +1,186 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.lucene.search.join; + +import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.document.StringField; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.Term; +import org.apache.lucene.search.*; +import org.apache.lucene.store.Directory; +import org.apache.lucene.tests.index.RandomIndexWriter; +import org.apache.lucene.tests.util.LuceneTestCase; +import org.junit.Test; + +public class TestParentsChildrenBlockJoinQuery extends LuceneTestCase { + + private final TestCase testCase; + + public TestParentsChildrenBlockJoinQuery(TestCase testCase) { + this.testCase = testCase; + } + + @ParametersFactory + public static Collection<Object[]> testCases() { + return Arrays.asList( + new Object[] {new TestCase("EmptyIndex", 10, 0, new int[0], new TestDoc[0][0])}, + new Object[] { + new TestCase( + "OnlyParentDocs", + 10, + 0, + new int[0], + new TestDoc[][] { + { + new TestDoc("parent", true), + }, + { + new TestDoc("parent", true), + }, + { + new TestDoc("parent", true), + } + }) + }, + new Object[] { + new TestCase( + "FirstParentWithoutChild", + 10, + 2, + new int[] {1, 2}, + new TestDoc[][] { + { + new TestDoc("parent", true), + }, + { + new TestDoc("child", true), + }, + { + new TestDoc("child", true), + }, + { + new TestDoc("parent", true), + } + }) + }); + } + + @Test + public void test() throws Exception { + Directory dir = newDirectory(); + RandomIndexWriter writer = new RandomIndexWriter(random(), dir); + + // Add documents based on test case + final List<Document> docs = new ArrayList<>(); + for (TestDoc[] block : testCase.blocks) { + for (TestDoc doc : block) { + Document document = doc.toDocument(); + docs.add(document); + } + writer.addDocuments(docs); + docs.clear(); + } + writer.commit(); + + IndexReader reader = writer.getReader(); + writer.close(); + + IndexSearcher searcher = newSearcher(reader); + BitSetProducer parentFilter = + new QueryBitSetProducer(new TermQuery(new Term("type", "parent"))); + Query parentQuery = + new BooleanQuery.Builder() + .add(new TermQuery(new Term("value", "match")), BooleanClause.Occur.MUST) + .add(new TermQuery(new Term("type", "parent")), BooleanClause.Occur.MUST) + .build(); + ; + Query childQuery = + new BooleanQuery.Builder() + .add(new TermQuery(new Term("value", "match")), BooleanClause.Occur.MUST) + .add(new TermQuery(new Term("type", "child")), BooleanClause.Occur.MUST) + .build(); + + ParentsChildrenBlockJoinQuery query = + new ParentsChildrenBlockJoinQuery( + parentFilter, parentQuery, childQuery, testCase.childLimitPerParent); + + TopDocs results = searcher.search(query, 10); + assertEquals(testCase.expectedHits, results.totalHits.value()); + assertEquals(testCase.expectedDocIds.length, results.scoreDocs.length); + int i = 0; + for (ScoreDoc scoreDoc : results.scoreDocs) { + assertEquals(testCase.expectedDocIds[i++], scoreDoc.doc); + } + + reader.close(); + dir.close(); + } + + @Test + public void testInvalidChildLimit() { + BitSetProducer parentFilter = + new QueryBitSetProducer(new TermQuery(new Term("type", "parent"))); + Query parentQuery = new TermQuery(new Term("value", "parent")); + Query childQuery = new TermQuery(new Term("type", "child")); + + IllegalArgumentException e = + expectThrows( + IllegalArgumentException.class, + () -> new ParentsChildrenBlockJoinQuery(parentFilter, parentQuery, childQuery, 0)); + assertTrue(e.getMessage().contains("childLimitPerParent must be > 0")); + } + + private record TestCase( + String name, + int childLimitPerParent, + int expectedHits, + int[] expectedDocIds, + TestDoc[][] blocks) { + + @Override + public String toString() { + return name; + } + } + + private static class TestDoc { + String type; + boolean isMatch; + + TestDoc(String type, boolean isMatch) { + this.type = type; + this.isMatch = isMatch; + } Review Comment: This could also be a `record`, right? ########## lucene/join/src/test/org/apache/lucene/search/join/TestParentsChildrenBlockJoinQuery.java: ########## @@ -0,0 +1,186 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.lucene.search.join; + +import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.document.StringField; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.Term; +import org.apache.lucene.search.*; +import org.apache.lucene.store.Directory; +import org.apache.lucene.tests.index.RandomIndexWriter; +import org.apache.lucene.tests.util.LuceneTestCase; +import org.junit.Test; + +public class TestParentsChildrenBlockJoinQuery extends LuceneTestCase { + + private final TestCase testCase; + + public TestParentsChildrenBlockJoinQuery(TestCase testCase) { + this.testCase = testCase; + } + + @ParametersFactory + public static Collection<Object[]> testCases() { Review Comment: I think these three parametrized test cases can just be split out into separate test methods, `testEmptyIndex`, `testOnlyParentDocs`, and `testFirstParentWithoutChild`. Basically, you can get rid of the `TestCase` class, change your `test()` method to be a private method like: ``` private void test(TestDoc[][] blocks, int[] expectedHits) ``` Then you should be able to do a randomized test case, like: ``` int childLimit = 1 + random().nextInt(10); int numParents = atLeast(100); int d = 0; TestDoc[][] testDocs = new TestDoc[][numParents]; List<Integer> matches = new ArrayList<>(); for (int i = 0; i < numParents; i++) { boolean matchingParent = random().nextBoolean(); int numChildren = random().nextInt(20); testDocs[i] = new TestDoc[numChildren + 1]; int matchingChildren = 0; for (int j = 0; j < numChildren; j++) { boolean matchingChild = random.nextBoolean(); testDocs[i][j] = new TestDoc("child", matchingChild); if (matchingChild && matchingParent) { matchingChildren++; if (matchingChildren <= childLimit) { matches.add(d); } } d++; } testDocs[i][numChildren] = new TestDoc("parent", matchingParent); d++; } test(testDocs, matches.stream().mapToInt(Integer::intValue).toArray()); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org