Apache Lucene open-source search software
LUCENE-5493: commit current state
Robert Muirc2fccd80e4a0
diff --git a/lucene/misc/src/java/org/apache/lucene/index/sorter/NumericDocValuesSorter.java b/lucene/misc/src/java/org/apache/lucene/index/sorter/NumericDocValuesSorter.javadeleted file mode 100644index 983fc67ac7f..00000000000--- a/lucene/misc/src/java/org/apache/lucene/index/sorter/NumericDocValuesSorter.java+++ /dev/null@@ -1,81 +0,0 @@-package org.apache.lucene.index.sorter;--/*- * 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.- */--import java.io.IOException;--import org.apache.lucene.index.AtomicReader;-import org.apache.lucene.index.NumericDocValues;--/**- * A {@link Sorter} which sorts documents according to their- * {@link NumericDocValues}. One can specify ascending or descending sort order.- *- * @lucene.experimental- */-public class NumericDocValuesSorter extends Sorter {-- private final String fieldName;- private final boolean ascending;-- /** Constructor over the given field name, and ascending sort order. */- public NumericDocValuesSorter(final String fieldName) {- this(fieldName, true);- }-- /**- * Constructor over the given field name, and whether sorting should be- * ascending ({@code true}) or descending ({@code false}).- */- public NumericDocValuesSorter(final String fieldName, boolean ascending) {- this.fieldName = fieldName;- this.ascending = ascending;- }-- @Override- public Sorter.DocMap sort(final AtomicReader reader) throws IOException {- final NumericDocValues ndv = reader.getNumericDocValues(fieldName);- final DocComparator comparator;- if (ascending) {- comparator = new DocComparator() {- @Override- public int compare(int docID1, int docID2) {- final long v1 = ndv.get(docID1);- final long v2 = ndv.get(docID2);- return v1 < v2 ? -1 : v1 == v2 ? 0 : 1;- }- };- } else {- comparator = new DocComparator() {- @Override- public int compare(int docID1, int docID2) {- final long v1 = ndv.get(docID1);- final long v2 = ndv.get(docID2);- return v1 > v2 ? -1 : v1 == v2 ? 0 : 1;- }- };- }- return sort(reader.maxDoc(), comparator);- }-- @Override- public String getID() {- return "DocValues(" + fieldName + "," + (ascending ? "ascending" : "descending") + ")";- }--}diff --git a/lucene/misc/src/java/org/apache/lucene/index/sorter/SortSorter.java b/lucene/misc/src/java/org/apache/lucene/index/sorter/SortSorter.javanew file mode 100644index 00000000000..bb5020c4292--- /dev/null+++ b/lucene/misc/src/java/org/apache/lucene/index/sorter/SortSorter.java@@ -0,0 +1,94 @@+package org.apache.lucene.index.sorter;++/*+ * 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.+ */++import java.io.IOException;++import org.apache.lucene.index.AtomicReader;+import org.apache.lucene.search.FieldComparator;+import org.apache.lucene.search.Scorer;+import org.apache.lucene.search.Sort;+import org.apache.lucene.search.SortField;++// nocommit: temporary class to engage the cutover!+public class SortSorter extends Sorter {+ final Sort sort;++ public SortSorter(Sort sort) {+ this.sort = sort;+ }++ @Override+ public DocMap sort(AtomicReader reader) throws IOException {+ SortField fields[] = sort.getSort();+ final int reverseMul[] = new int[fields.length];+ final FieldComparator<?> comparators[] = new FieldComparator[fields.length];++ for (int i = 0; i < fields.length; i++) {+ reverseMul[i] = fields[i].getReverse() ? -1 : 1;+ comparators[i] = fields[i].getComparator(2, i);+ comparators[i].setNextReader(reader.getContext());+ comparators[i].setScorer(FAKESCORER);+ }+ final DocComparator comparator = new DocComparator() {+ @Override+ public int compare(int docID1, int docID2) {+ try {+ for (int i = 0; i < comparators.length; i++) {+ comparators[i].copy(0, docID1);+ comparators[i].copy(1, docID2);+ int comp = reverseMul[i] * comparators[i].compare(0, 1);+ if (comp != 0) {+ return comp;+ }+ }+ return Integer.compare(docID1, docID2); // docid order tiebreak+ } catch (IOException e) {+ throw new RuntimeException(e);+ }+ }+ };+ return sort(reader.maxDoc(), comparator);+ }++ @Override+ public String getID() {+ return sort.toString();+ }++ static final Scorer FAKESCORER = new Scorer(null) {++ @Override+ public float score() throws IOException { throw new UnsupportedOperationException(); }++ @Override+ public int freq() throws IOException { throw new UnsupportedOperationException(); }++ @Override+ public int docID() { throw new UnsupportedOperationException(); }++ @Override+ public int nextDoc() throws IOException { throw new UnsupportedOperationException(); }++ @Override+ public int advance(int target) throws IOException { throw new UnsupportedOperationException(); }++ @Override+ public long cost() { throw new UnsupportedOperationException(); }+ };+}diff --git a/lucene/misc/src/test/org/apache/lucene/index/sorter/IndexSortingTest.java b/lucene/misc/src/test/org/apache/lucene/index/sorter/IndexSortingTest.javaindex 5b28375f3d4..0674bbd5135 100644--- a/lucene/misc/src/test/org/apache/lucene/index/sorter/IndexSortingTest.java+++ b/lucene/misc/src/test/org/apache/lucene/index/sorter/IndexSortingTest.java@@ -24,6 +24,8 @@ import java.util.List;import org.apache.lucene.index.DirectoryReader;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.SlowCompositeReaderWrapper;+import org.apache.lucene.search.Sort;+import org.apache.lucene.search.SortField;import org.apache.lucene.store.Directory;import org.apache.lucene.util.Bits;import org.apache.lucene.util.TestUtil;@@ -32,7 +34,7 @@ import org.junit.BeforeClass;public class IndexSortingTest extends SorterTestBase {private static final Sorter[] SORTERS = new Sorter[] {- new NumericDocValuesSorter(NUMERIC_DV_FIELD, true),+ new SortSorter(new Sort(new SortField(NUMERIC_DV_FIELD, SortField.Type.LONG))),Sorter.REVERSE_DOCS,};@@ -52,8 +54,8 @@ public class IndexSortingTest extends SorterTestBase {Collections.reverse(values);} else {Collections.sort(values);- if (sorter instanceof NumericDocValuesSorter && random().nextBoolean()) {- sorter = new NumericDocValuesSorter(NUMERIC_DV_FIELD, false); // descending+ if (sorter instanceof SortSorter && random().nextBoolean()) {+ sorter = new SortSorter(new Sort(new SortField(NUMERIC_DV_FIELD, SortField.Type.LONG, true))); // descendingCollections.reverse(values);}}diff --git a/lucene/misc/src/test/org/apache/lucene/index/sorter/TestEarlyTermination.java b/lucene/misc/src/test/org/apache/lucene/index/sorter/TestEarlyTermination.javaindex a5bc3b23d4e..fa82a303bda 100644--- a/lucene/misc/src/test/org/apache/lucene/index/sorter/TestEarlyTermination.java+++ b/lucene/misc/src/test/org/apache/lucene/index/sorter/TestEarlyTermination.java@@ -58,7 +58,7 @@ public class TestEarlyTermination extends LuceneTestCase {@Overridepublic void setUp() throws Exception {super.setUp();- sorter = new NumericDocValuesSorter("ndv1");+ sorter = new SortSorter(new Sort(new SortField("ndv1", SortField.Type.LONG)));}private Document randomDocument() {@@ -144,7 +144,8 @@ public class TestEarlyTermination extends LuceneTestCase {for (int i = 0; i < iters; ++i) {final TermQuery query = new TermQuery(new Term("s", RandomPicks.randomFrom(random(), terms)));searcher.search(query, collector1);- searcher.search(query, new EarlyTerminatingSortingCollector(collector2, new NumericDocValuesSorter("ndv2"), numHits) {+ Sorter different = new SortSorter(new Sort(new SortField("ndv2", SortField.Type.LONG)));+ searcher.search(query, new EarlyTerminatingSortingCollector(collector2, different, numHits) {@Overridepublic void setNextReader(AtomicReaderContext context) throws IOException {super.setNextReader(context);diff --git a/lucene/misc/src/test/org/apache/lucene/index/sorter/TestSortingMergePolicy.java b/lucene/misc/src/test/org/apache/lucene/index/sorter/TestSortingMergePolicy.javaindex 93be226e1d5..bba5cb58d93 100644--- a/lucene/misc/src/test/org/apache/lucene/index/sorter/TestSortingMergePolicy.java+++ b/lucene/misc/src/test/org/apache/lucene/index/sorter/TestSortingMergePolicy.java@@ -40,6 +40,8 @@ import org.apache.lucene.index.RandomIndexWriter;import org.apache.lucene.index.SlowCompositeReaderWrapper;import org.apache.lucene.index.Term;import org.apache.lucene.index.TieredMergePolicy;+import org.apache.lucene.search.Sort;+import org.apache.lucene.search.SortField;import org.apache.lucene.store.Directory;import org.apache.lucene.util.LuceneTestCase;import org.apache.lucene.util.TestUtil;@@ -57,7 +59,7 @@ public class TestSortingMergePolicy extends LuceneTestCase {@Overridepublic void setUp() throws Exception {super.setUp();- sorter = new NumericDocValuesSorter("ndv");+ sorter = new SortSorter(new Sort(new SortField("ndv", SortField.Type.LONG)));createRandomIndexes();}diff --git a/lucene/suggest/src/java/org/apache/lucene/search/suggest/analyzing/AnalyzingInfixSuggester.java b/lucene/suggest/src/java/org/apache/lucene/search/suggest/analyzing/AnalyzingInfixSuggester.javaindex 354d1cfb1e8..56f88b4d1bf 100644--- a/lucene/suggest/src/java/org/apache/lucene/search/suggest/analyzing/AnalyzingInfixSuggester.java+++ b/lucene/suggest/src/java/org/apache/lucene/search/suggest/analyzing/AnalyzingInfixSuggester.java@@ -50,7 +50,6 @@ import org.apache.lucene.index.IndexReader;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.IndexWriterConfig;import org.apache.lucene.index.MultiDocValues;-import org.apache.lucene.index.NumericDocValues;import org.apache.lucene.index.SegmentReader;import org.apache.lucene.index.SlowCompositeReaderWrapper;import org.apache.lucene.index.Term;@@ -58,6 +57,7 @@ import org.apache.lucene.index.sorter.EarlyTerminatingSortingCollector;import org.apache.lucene.index.sorter.Sorter;import org.apache.lucene.index.sorter.SortingAtomicReader;import org.apache.lucene.index.sorter.SortingMergePolicy;+import org.apache.lucene.index.sorter.SortSorter;import org.apache.lucene.search.BooleanClause;import org.apache.lucene.search.BooleanQuery;import org.apache.lucene.search.Collector;@@ -360,36 +360,7 @@ public class AnalyzingInfixSuggester extends Lookup implements Closeable {}private void initSorter() {- sorter = new Sorter() {-- @Override- public Sorter.DocMap sort(AtomicReader reader) throws IOException {- final NumericDocValues weights = reader.getNumericDocValues("weight");- final Sorter.DocComparator comparator = new Sorter.DocComparator() {- @Override- public int compare(int docID1, int docID2) {- final long v1 = weights.get(docID1);- final long v2 = weights.get(docID2);- // Reverse sort (highest weight first);- // java7 only:- //return Long.compare(v2, v1);- if (v1 > v2) {- return -1;- } else if (v1 < v2) {- return 1;- } else {- return 0;- }- }- };- return Sorter.sort(reader.maxDoc(), comparator);- }-- @Override- public String getID() {- return "BySuggestWeight";- }- };+ sorter = new SortSorter(new Sort(new SortField("weight", SortField.Type.LONG, true)));}/**