-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Allow Collectors to re-order segments for non-exhaustive searches #15436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Comparator; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.function.ToLongFunction; | ||
| import org.apache.lucene.index.DocValuesSkipper; | ||
| import org.apache.lucene.index.LeafReader; | ||
| import org.apache.lucene.index.LeafReaderContext; | ||
| import org.apache.lucene.index.PointValues; | ||
|
|
||
| /** | ||
| * Given a sort field, compares segments by the range of values in that field such that documents in | ||
| * better comparing segments are more likely to appear higher in search results. | ||
| */ | ||
| class NumericFieldReaderContextComparator implements Comparator<LeafReaderContext> { | ||
|
|
||
| private final Map<Integer, Long> cachedSortValues = new HashMap<>(); | ||
| private final String field; | ||
| private final boolean reverse; | ||
| private final Long missingValue; | ||
| private final ToLongFunction<byte[]> pointDecoder; | ||
|
|
||
| NumericFieldReaderContextComparator( | ||
| String field, Long missingValue, boolean reverse, ToLongFunction<byte[]> pointDecoder) { | ||
| this.field = field; | ||
| this.missingValue = missingValue; | ||
| this.reverse = reverse; | ||
| this.pointDecoder = pointDecoder; | ||
| } | ||
|
|
||
| @Override | ||
| public int compare(LeafReaderContext o1, LeafReaderContext o2) { | ||
| return reverse | ||
| ? Long.compare(getSortValue(o2), getSortValue(o1)) | ||
| : Long.compare(getSortValue(o1), getSortValue(o2)); | ||
| } | ||
|
|
||
| private long getSortValue(LeafReaderContext ctx) { | ||
| if (cachedSortValues.containsKey(ctx.ord) == false) { | ||
| cachedSortValues.put(ctx.ord, loadSortValue(ctx)); | ||
| } | ||
| return cachedSortValues.get(ctx.ord); | ||
| } | ||
|
|
||
| private long loadSortValue(LeafReaderContext ctx) { | ||
| LeafReader reader = ctx.reader(); | ||
| try { | ||
| DocValuesSkipper skipper = reader.getDocValuesSkipper(field); | ||
| if (skipper != null) { | ||
| if (skipper.docCount() == reader.maxDoc() || missingValue == null) { | ||
| return reverse ? skipper.maxValue() : skipper.minValue(); | ||
| } | ||
| if (reverse) { | ||
| return Math.max(skipper.maxValue(), missingValue); | ||
| } else { | ||
| return Math.min(skipper.minValue(), missingValue); | ||
| } | ||
| } | ||
| PointValues pointValues = reader.getPointValues(field); | ||
| if (pointValues != null) { | ||
| if (pointValues.getDocCount() == reader.maxDoc() || missingValue == null) { | ||
| if (reverse) { | ||
| return pointDecoder.applyAsLong(pointValues.getMaxPackedValue()); | ||
| } else { | ||
| return pointDecoder.applyAsLong(pointValues.getMinPackedValue()); | ||
| } | ||
| } | ||
| if (reverse) { | ||
| return Math.max(pointDecoder.applyAsLong(pointValues.getMaxPackedValue()), missingValue); | ||
| } else { | ||
| return Math.min(pointDecoder.applyAsLong(pointValues.getMinPackedValue()), missingValue); | ||
| } | ||
| } | ||
| } catch (IOException _) { | ||
| // We can't rethrow exceptions from inside a Comparator, so we instead | ||
| // return as if there are no index structures to read values from. | ||
| return reverse ? Long.MAX_VALUE : Long.MIN_VALUE; | ||
martijnvg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| return reverse ? Long.MAX_VALUE : Long.MIN_VALUE; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -217,6 +217,14 @@ public void collect(int doc) throws IOException { | |
|
|
||
| return collector; | ||
| } | ||
|
|
||
| @Override | ||
| public Comparator<LeafReaderContext> getLeafReaderComparator() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the case that the original ordering of leaves is already optimal (because leaf sorter has been configured on IndexWriterConfig), would this be the place where subclasses overwrite and return null? In this case there shouldn't be a need to do the re-ordering of segments?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The case where there's a configured leaf sorter is tricky, as it might be optimal or it might be entirely adverse depending on whether the query sort is reversed or not. You could override here and turn off query-time segment sorting if you wanted. But maybe we need a better escape hatch?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think so. What do you think would be a good escape hatch here? |
||
| if (scoreMode.isExhaustive()) { | ||
| return null; | ||
| } | ||
| return this.sort.getLeafReaderComparator(); | ||
| } | ||
| } | ||
|
|
||
| /* | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.