mkhludnev commented on code in PR #13974: URL: https://github.com/apache/lucene/pull/13974#discussion_r1896618101
########## lucene/sandbox/src/java/org/apache/lucene/sandbox/search/SortedSetMultiRangeQuery.java: ########## @@ -0,0 +1,300 @@ +/* + * 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.sandbox.search; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import org.apache.lucene.document.SortedSetDocValuesField; +import org.apache.lucene.index.DocValues; +import org.apache.lucene.index.DocValuesSkipper; +import org.apache.lucene.index.LeafReaderContext; +import org.apache.lucene.index.SortedSetDocValues; +import org.apache.lucene.index.TermsEnum; +import org.apache.lucene.search.BooleanQuery; +import org.apache.lucene.search.ConstantScoreScorer; +import org.apache.lucene.search.ConstantScoreWeight; +import org.apache.lucene.search.DocIdSetIterator; +import org.apache.lucene.search.DocValuesRangeIterator; +import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.Query; +import org.apache.lucene.search.QueryVisitor; +import org.apache.lucene.search.ScoreMode; +import org.apache.lucene.search.Scorer; +import org.apache.lucene.search.ScorerSupplier; +import org.apache.lucene.search.TwoPhaseIterator; +import org.apache.lucene.search.Weight; +import org.apache.lucene.util.ArrayUtil; +import org.apache.lucene.util.BytesRef; +import org.apache.lucene.util.LongBitSet; + +/** A union multiple ranges over SortedSetDocValuesField */ +public class SortedSetMultiRangeQuery extends Query { + private final String field; + private final int bytesPerDim; + private final ArrayUtil.ByteArrayComparator comparator; + List<MultiRangeQuery.RangeClause> rangeClauses; + + SortedSetMultiRangeQuery( + String name, + List<MultiRangeQuery.RangeClause> clauses, + int bytes, + ArrayUtil.ByteArrayComparator comparator) { + this.field = name; + this.rangeClauses = clauses; + this.bytesPerDim = bytes; + this.comparator = comparator; + } + + /** Builder for creating a SortedSetMultiRangeQuery. */ + public static class Builder { + private final String name; + protected final List<MultiRangeQuery.RangeClause> clauses = new ArrayList<>(); + private final int bytes; + private final ArrayUtil.ByteArrayComparator comparator; + + public Builder(String name, int bytes) { + this.name = Objects.requireNonNull(name); + this.bytes = bytes; // TODO assrt positive + this.comparator = ArrayUtil.getUnsignedComparator(bytes); + } + + public Builder add(BytesRef lowerValue, BytesRef upperValue) { + byte[] low = lowerValue.clone().bytes; + byte[] up = upperValue.clone().bytes; + if (this.comparator.compare(low, 0, up, 0) > 0) { + throw new IllegalArgumentException("lowerValue must be <= upperValue"); + } else { + clauses.add(new MultiRangeQuery.RangeClause(low, up)); + } + return this; + } + + public Query build() { + if (clauses.isEmpty()) { + return new BooleanQuery.Builder().build(); + } + if (clauses.size() == 1) { + return SortedSetDocValuesField.newSlowRangeQuery( + name, + new BytesRef(clauses.getFirst().lowerValue), + new BytesRef(clauses.getFirst().upperValue), + true, + true); + } + return new SortedSetMultiRangeQuery(name, clauses, this.bytes, comparator); + } + } + + @Override + public Query rewrite(IndexSearcher indexSearcher) throws IOException { + ArrayList<MultiRangeQuery.RangeClause> sortedClauses = new ArrayList<>(this.rangeClauses); + sortedClauses.sort( + (o1, o2) -> { + // if (result == 0) { + // return comparator.compare(o1.upperValue, 0, o2.upperValue, 0); + // } else { + return comparator.compare(o1.lowerValue, 0, o2.lowerValue, 0); + // } + }); + if (!this.rangeClauses.equals(sortedClauses)) { + return new SortedSetMultiRangeQuery( + this.field, sortedClauses, this.bytesPerDim, this.comparator); + } else { + return this; + } + } + + @Override + public String toString(String fld) { + return "SortedSetMultiRangeQuery{" + + "field='" + + fld + + '\'' + + ", rangeClauses=" + + rangeClauses + + // TODO better toString + '}'; + } + + // what TODO with reverse ranges ??? + @Override + public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) + throws IOException { + return new ConstantScoreWeight(this, boost) { + @Override + public ScorerSupplier scorerSupplier(LeafReaderContext context) throws IOException { + if (context.reader().getFieldInfos().fieldInfo(field) == null) { + return null; + } + DocValuesSkipper skipper = context.reader().getDocValuesSkipper(field); Review Comment: Turns out, DV access should happen right here since `ScorerSupplier#cost()` needs DV for estimate. Actually I copied it from https://github.com/apache/lucene/blob/0494c824e0ac8049b757582f60d085932a890800/lucene/core/src/java/org/apache/lucene/document/SortedSetDocValuesRangeQuery.java#L122 -- 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