ercsonusharma commented on code in PR #4645: URL: https://github.com/apache/solr/pull/4645#discussion_r3630791309
########## solr/core/src/test/org/apache/solr/schema/numericrange/MultiBinaryRangeDocValuesQueryTest.java: ########## @@ -0,0 +1,217 @@ +/* + * 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.solr.schema.numericrange; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Collection; +import org.apache.lucene.document.BinaryDocValuesField; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.IntRange; +import org.apache.lucene.document.IntRangeDocValuesField; +import org.apache.lucene.document.RangeFieldQuery.QueryType; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.LeafReaderContext; +import org.apache.lucene.search.CollectorManager; +import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.Query; +import org.apache.lucene.search.ScoreMode; +import org.apache.lucene.search.SimpleCollector; +import org.apache.lucene.store.Directory; +import org.apache.lucene.tests.index.RandomIndexWriter; +import org.apache.lucene.tests.util.TestUtil; +import org.apache.lucene.util.BytesRef; +import org.apache.lucene.util.BytesRefBuilder; +import org.apache.lucene.util.FixedBitSet; +import org.apache.solr.SolrTestCase; +import org.junit.Test; + +/** + * Unit tests for {@link MultiBinaryRangeDocValuesQuery}, the binary docValues-backed range + * verifier. + * + * <p>Each doc indexes its range(s) both as BKD ({@link IntRange}) and as a single {@code + * BinaryDocValues} blob (all ranges concatenated, as {@code AbstractNumericRangeField} does), + * letting us assert the docValues verifier returns exactly the same documents as the trusted BKD + * query across all four relations - for both single-valued docs (one range) and multiValued docs + * (several ranges in one blob), where "any range matches" (not the union of ranges) is correct. + */ +public class MultiBinaryRangeDocValuesQueryTest extends SolrTestCase { + + private static final String FIELD = "r"; + + /** Encodes a 1-D query range the same way indexed ranges are encoded. */ + private static byte[] encode(int min, int max) { + BytesRef b = new IntRangeDocValuesField(FIELD, new int[] {min}, new int[] {max}).binaryValue(); + return Arrays.copyOfRange(b.bytes, b.offset, b.offset + b.length); + } + + private static Query dv(QueryType type, int min, int max) { + return new MultiBinaryRangeDocValuesQuery(FIELD, encode(min, max), 1, IntRange.BYTES, type); + } + + private static Query bkd(QueryType type, int min, int max) { + int[] mn = {min}; + int[] mx = {max}; + return switch (type) { + case INTERSECTS -> IntRange.newIntersectsQuery(FIELD, mn, mx); + case CONTAINS -> IntRange.newContainsQuery(FIELD, mn, mx); + case WITHIN -> IntRange.newWithinQuery(FIELD, mn, mx); + case CROSSES -> IntRange.newCrossesQuery(FIELD, mn, mx); + }; + } + + /** + * Adds a document whose (possibly multiple) ranges are indexed as BKD and as one BinaryDocValues + * blob holding every range concatenated (a single-range doc is just the one-range case). + */ + private static void addDoc(RandomIndexWriter w, int[]... ranges) throws IOException { + Document doc = new Document(); + BytesRefBuilder blob = new BytesRefBuilder(); + for (int[] r : ranges) { + int[] mn = {r[0]}; + int[] mx = {r[1]}; + doc.add(new IntRange(FIELD, mn, mx)); // BKD (ground truth) + blob.append(new IntRangeDocValuesField(FIELD, mn, mx).binaryValue()); // concat into one blob + } + doc.add(new BinaryDocValuesField(FIELD, blob.toBytesRef())); + w.addDocument(doc); + } + + private static FixedBitSet matches(IndexSearcher s, Query q) throws IOException { + int maxDoc = Math.max(1, s.getIndexReader().maxDoc()); + return s.search( + q, + new CollectorManager<BitSetCollector, FixedBitSet>() { + @Override + public BitSetCollector newCollector() { + return new BitSetCollector(maxDoc); + } + + @Override + public FixedBitSet reduce(Collection<BitSetCollector> collectors) { + FixedBitSet merged = new FixedBitSet(maxDoc); + for (BitSetCollector c : collectors) { + merged.or(c.bits); + } + return merged; + } + }); + } + + /** + * Collects matching doc ids (segment-local -> absolute) into a bitset, per CollectorManager. + */ + private static final class BitSetCollector extends SimpleCollector { + final FixedBitSet bits; + int base; + + BitSetCollector(int maxDoc) { + this.bits = new FixedBitSet(maxDoc); + } + + @Override + public void collect(int doc) { + bits.set(base + doc); + } + + @Override + protected void doSetNextReader(LeafReaderContext ctx) { + base = ctx.docBase; + } + + @Override + public ScoreMode scoreMode() { + return ScoreMode.COMPLETE_NO_SCORES; + } + } + + @Test + public void testMultiValuedPerRangeSemantics() throws Exception { Review Comment: The way to call through Solr APIs is by QParser which we are already doing in many test classes. So, I think it makes sense to get rid of Lucene specific low level test here. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
