jpountz commented on a change in pull request #1351:
URL: https://github.com/apache/lucene-solr/pull/1351#discussion_r429949584



##########
File path: 
lucene/core/src/java/org/apache/lucene/search/FilteringNumericComparator.java
##########
@@ -0,0 +1,49 @@
+/*
+ * 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 org.apache.lucene.index.LeafReaderContext;
+
+import java.io.IOException;
+
+/**
+ * A wrapper over {@code NumericComparator} that provides a leaf comparator 
that can filter non-competitive docs.
+ */
+public class FilteringNumericComparator<T extends Number> extends 
FilteringFieldComparator<T> {
+  public FilteringNumericComparator(NumericComparator<T> in, boolean reverse, 
boolean singleSort) {
+    super(in, reverse, singleSort);
+  }
+
+  @Override
+  public final FilteringLeafFieldComparator 
getLeafComparator(LeafReaderContext context) throws IOException {
+    ((NumericComparator) in).doSetNextReader(context);

Review comment:
       this relies on the implementation detail that NumericComparator extends 
SimpleFieldCompatator, can we instead call `LeafFieldComparator 
inLeafComparator = in.getLeafComparator(context);` and then apply the below if 
statements over `inLeafComparator` rather than `in`?

##########
File path: 
lucene/core/src/java/org/apache/lucene/search/FilteringNumericComparator.java
##########
@@ -0,0 +1,49 @@
+/*
+ * 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 org.apache.lucene.index.LeafReaderContext;
+
+import java.io.IOException;
+
+/**
+ * A wrapper over {@code NumericComparator} that provides a leaf comparator 
that can filter non-competitive docs.
+ */
+public class FilteringNumericComparator<T extends Number> extends 
FilteringFieldComparator<T> {
+  public FilteringNumericComparator(NumericComparator<T> in, boolean reverse, 
boolean singleSort) {
+    super(in, reverse, singleSort);
+  }
+
+  @Override
+  public final FilteringLeafFieldComparator 
getLeafComparator(LeafReaderContext context) throws IOException {
+    ((NumericComparator) in).doSetNextReader(context);
+    if (in instanceof FieldComparator.LongComparator) {
+      return new 
FilteringNumericLeafComparator.FilteringLongLeafComparator((FieldComparator.LongComparator)
 in, context,
+          ((LongComparator) in).field, reverse, singleSort, hasTopValue);
+    } else if (in instanceof FieldComparator.IntComparator) {
+      return new 
FilteringNumericLeafComparator.FilteringIntLeafComparator((FieldComparator.IntComparator)
 in, context,
+          ((IntComparator) in).field, reverse, singleSort, hasTopValue);
+    } else if (in instanceof FieldComparator.DoubleComparator) {
+      return new 
FilteringNumericLeafComparator.FilteringDoubleLeafComparator((FieldComparator.DoubleComparator)
 in, context,
+          ((DoubleComparator) in).field, reverse, singleSort, hasTopValue);
+    } else { // instanceof FieldComparator.FloatComparator

Review comment:
       can you actually do the instanceof check so that it would be more future 
proof? E.g. in case we add support for bfloat16 one day.

##########
File path: lucene/core/src/java/org/apache/lucene/search/TopFieldCollector.java
##########
@@ -302,18 +327,24 @@ private TopFieldCollector(FieldValueHitQueue<Entry> pq, 
int numHits,
     this.numHits = numHits;
     this.hitsThresholdChecker = hitsThresholdChecker;
     this.numComparators = pq.getComparators().length;
-    FieldComparator<?> fieldComparator = pq.getComparators()[0];
+    FieldComparator<?> firstComparator = pq.getComparators()[0];
     int reverseMul = pq.reverseMul[0];
-    if 
(fieldComparator.getClass().equals(FieldComparator.RelevanceComparator.class)
-          && reverseMul == 1 // if the natural sort is preserved (sort by 
descending relevance)
-          && hitsThresholdChecker.getHitsThreshold() != Integer.MAX_VALUE) {
-      firstComparator = (FieldComparator.RelevanceComparator) fieldComparator;
+
+    if 
(firstComparator.getClass().equals(FieldComparator.RelevanceComparator.class)
+            && reverseMul == 1 // if the natural sort is preserved (sort by 
descending relevance)
+            && hitsThresholdChecker.getHitsThreshold() != Integer.MAX_VALUE) {
+      relevanceComparator = (FieldComparator.RelevanceComparator) 
firstComparator;
       scoreMode = ScoreMode.TOP_SCORES;
       canSetMinScore = true;
     } else {
-      firstComparator = null;
-      scoreMode = needsScores ? ScoreMode.COMPLETE : 
ScoreMode.COMPLETE_NO_SCORES;
+      relevanceComparator = null;
       canSetMinScore = false;
+      if (firstComparator instanceof FilteringFieldComparator) {
+        assert(hitsThresholdChecker.getHitsThreshold() != Integer.MAX_VALUE);

Review comment:
       no need for parentheses with `assert`

##########
File path: 
lucene/core/src/java/org/apache/lucene/search/FilteringNumericComparator.java
##########
@@ -0,0 +1,49 @@
+/*
+ * 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 org.apache.lucene.index.LeafReaderContext;
+
+import java.io.IOException;
+
+/**
+ * A wrapper over {@code NumericComparator} that provides a leaf comparator 
that can filter non-competitive docs.
+ */
+public class FilteringNumericComparator<T extends Number> extends 
FilteringFieldComparator<T> {
+  public FilteringNumericComparator(NumericComparator<T> in, boolean reverse, 
boolean singleSort) {
+    super(in, reverse, singleSort);
+  }
+
+  @Override
+  public final FilteringLeafFieldComparator 
getLeafComparator(LeafReaderContext context) throws IOException {
+    ((NumericComparator) in).doSetNextReader(context);
+    if (in instanceof FieldComparator.LongComparator) {

Review comment:
       `FieldComparator.LongComparator` allows to customize the way that it 
gets NumericDocValues from the index, so I think that it needs to check the 
exact class rather than do an `instanceof`?

##########
File path: 
lucene/core/src/test/org/apache/lucene/search/TestSortOptimization.java
##########
@@ -0,0 +1,294 @@
+/*
+ * 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 org.apache.lucene.document.Document;
+import org.apache.lucene.document.FloatDocValuesField;
+import org.apache.lucene.document.LongPoint;
+import org.apache.lucene.document.IntPoint;
+import org.apache.lucene.document.FloatPoint;
+import org.apache.lucene.document.NumericDocValuesField;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.LuceneTestCase;
+
+import java.io.IOException;
+
+import static org.apache.lucene.search.SortField.FIELD_SCORE;
+
+public class TestSortOptimization extends LuceneTestCase {

Review comment:
       Let's use a less generic name, e.g. `TestFieldSortOptimization` to make 
it clear it doesn't apply to scores, or maybe something even more describing 
such as `TestFieldSortOptimizationSkipping`?




----------------------------------------------------------------
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.

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

Reply via email to