mirror of https://github.com/apache/lucene.git
LUCENE-4779: add docvalues and reverse tests
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1446440 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
5e688d6512
commit
ce81eb1c5c
|
@ -378,64 +378,6 @@ public class TestSort extends LuceneTestCase {
|
|||
super.tearDown();
|
||||
}
|
||||
|
||||
// test the sorts by score and document number
|
||||
public void testBuiltInSorts() throws Exception {
|
||||
sort = new Sort();
|
||||
assertMatches(full, queryX, sort, "ACEGI");
|
||||
assertMatches(full, queryY, sort, "BDFHJ");
|
||||
|
||||
sort.setSort(SortField.FIELD_DOC);
|
||||
assertMatches(full, queryX, sort, "ACEGI");
|
||||
assertMatches(full, queryY, sort, "BDFHJ");
|
||||
}
|
||||
|
||||
// test sorts where the type of field is specified
|
||||
public void testTypedSort() throws Exception {
|
||||
sort.setSort(new SortField("int", SortField.Type.INT), SortField.FIELD_DOC);
|
||||
assertMatches(full, queryX, sort, "IGAEC");
|
||||
assertMatches(full, queryY, sort, "DHFJB");
|
||||
|
||||
sort.setSort(new SortField("float", SortField.Type.FLOAT), SortField.FIELD_DOC);
|
||||
assertMatches(full, queryX, sort, "GCIEA");
|
||||
assertMatches(full, queryY, sort, "DHJFB");
|
||||
|
||||
sort.setSort(new SortField("long", SortField.Type.LONG), SortField.FIELD_DOC);
|
||||
assertMatches(full, queryX, sort, "EACGI");
|
||||
assertMatches(full, queryY, sort, "FBJHD");
|
||||
|
||||
sort.setSort(new SortField("double", SortField.Type.DOUBLE), SortField.FIELD_DOC);
|
||||
assertMatches(full, queryX, sort, "AGICE");
|
||||
assertMatches(full, queryY, sort, "DJHBF");
|
||||
|
||||
sort.setSort(new SortField("byte", SortField.Type.BYTE), SortField.FIELD_DOC);
|
||||
assertMatches(full, queryX, sort, "CIGAE");
|
||||
assertMatches(full, queryY, sort, "DHFBJ");
|
||||
|
||||
sort.setSort(new SortField("short", SortField.Type.SHORT), SortField.FIELD_DOC);
|
||||
assertMatches(full, queryX, sort, "IAGCE");
|
||||
assertMatches(full, queryY, sort, "DFHBJ");
|
||||
|
||||
sort.setSort(new SortField("string", SortField.Type.STRING), SortField.FIELD_DOC);
|
||||
assertMatches(full, queryX, sort, "AIGEC");
|
||||
assertMatches(full, queryY, sort, "DJHFB");
|
||||
|
||||
sort.setSort(new SortField("int_dv", SortField.Type.INT), SortField.FIELD_DOC);
|
||||
assertMatches(full, queryX, sort, "IGAEC");
|
||||
assertMatches(full, queryY, sort, "DHFJB");
|
||||
|
||||
sort.setSort(new SortField("float_dv", SortField.Type.FLOAT), SortField.FIELD_DOC);
|
||||
assertMatches(full, queryX, sort, "GCIEA");
|
||||
assertMatches(full, queryY, sort, "DHJFB");
|
||||
|
||||
sort.setSort(new SortField("double_dv", SortField.Type.DOUBLE), SortField.FIELD_DOC);
|
||||
assertMatches(full, queryX, sort, "AGICE");
|
||||
assertMatches(full, queryY, sort, "DJHBF");
|
||||
|
||||
sort.setSort(new SortField("string_dv", getDVStringSortType()), SortField.FIELD_DOC);
|
||||
assertMatches(full, queryX, sort, "AIGEC");
|
||||
assertMatches(full, queryY, sort, "DJHFB");
|
||||
}
|
||||
|
||||
private SortField.Type getDVStringSortType() {
|
||||
return getDVStringSortType(true);
|
||||
}
|
||||
|
@ -772,41 +714,6 @@ public class TestSort extends LuceneTestCase {
|
|||
assertMatches(full, queryA, sort, "JIHGFEDCBA");
|
||||
}
|
||||
|
||||
// test sorts in reverse
|
||||
public void testReverseSort() throws Exception {
|
||||
sort.setSort(new SortField(null, SortField.Type.SCORE, true), SortField.FIELD_DOC);
|
||||
assertMatches(full, queryX, sort, "IEGCA");
|
||||
assertMatches(full, queryY, sort, "JFHDB");
|
||||
|
||||
sort.setSort(new SortField(null, SortField.Type.DOC, true));
|
||||
assertMatches(full, queryX, sort, "IGECA");
|
||||
assertMatches(full, queryY, sort, "JHFDB");
|
||||
|
||||
sort.setSort(new SortField("int", SortField.Type.INT, true));
|
||||
assertMatches(full, queryX, sort, "CAEGI");
|
||||
assertMatches(full, queryY, sort, "BJFHD");
|
||||
|
||||
sort.setSort(new SortField("float", SortField.Type.FLOAT, true));
|
||||
assertMatches(full, queryX, sort, "AECIG");
|
||||
assertMatches(full, queryY, sort, "BFJHD");
|
||||
|
||||
sort.setSort(new SortField("string", SortField.Type.STRING, true));
|
||||
assertMatches(full, queryX, sort, "CEGIA");
|
||||
assertMatches(full, queryY, sort, "BFHJD");
|
||||
|
||||
sort.setSort(new SortField("int_dv", SortField.Type.INT, true));
|
||||
assertMatches(full, queryX, sort, "CAEGI");
|
||||
assertMatches(full, queryY, sort, "BJFHD");
|
||||
|
||||
sort.setSort(new SortField("float_dv", SortField.Type.FLOAT, true));
|
||||
assertMatches(full, queryX, sort, "AECIG");
|
||||
assertMatches(full, queryY, sort, "BFJHD");
|
||||
|
||||
sort.setSort(new SortField("string_dv", getDVStringSortType(), true));
|
||||
assertMatches(full, queryX, sort, "CEGIA");
|
||||
assertMatches(full, queryY, sort, "BFHJD");
|
||||
}
|
||||
|
||||
// test sorting when the sort field is empty (undefined) for some of the documents
|
||||
public void testEmptyFieldSort() throws Exception {
|
||||
|
||||
|
|
|
@ -76,6 +76,84 @@ public class TestSort2 extends LuceneTestCase {
|
|||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests reverse sorting on type string */
|
||||
public void testStringReverse() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(newStringField("value", "bar", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(newStringField("value", "foo", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(ir);
|
||||
Sort sort = new Sort(new SortField("value", SortField.Type.STRING, true));
|
||||
|
||||
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
|
||||
assertEquals(2, td.totalHits);
|
||||
// 'foo' comes after 'bar' in reverse order
|
||||
assertEquals("foo", searcher.doc(td.scoreDocs[0].doc).get("value"));
|
||||
assertEquals("bar", searcher.doc(td.scoreDocs[1].doc).get("value"));
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests sorting on type string_val */
|
||||
public void testStringVal() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(newStringField("value", "foo", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(newStringField("value", "bar", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(ir);
|
||||
Sort sort = new Sort(new SortField("value", SortField.Type.STRING_VAL));
|
||||
|
||||
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
|
||||
assertEquals(2, td.totalHits);
|
||||
// 'bar' comes before 'foo'
|
||||
assertEquals("bar", searcher.doc(td.scoreDocs[0].doc).get("value"));
|
||||
assertEquals("foo", searcher.doc(td.scoreDocs[1].doc).get("value"));
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests reverse sorting on type string_val */
|
||||
public void testStringValReverse() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(newStringField("value", "bar", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(newStringField("value", "foo", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(ir);
|
||||
Sort sort = new Sort(new SortField("value", SortField.Type.STRING_VAL, true));
|
||||
|
||||
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
|
||||
assertEquals(2, td.totalHits);
|
||||
// 'foo' comes after 'bar' in reverse order
|
||||
assertEquals("foo", searcher.doc(td.scoreDocs[0].doc).get("value"));
|
||||
assertEquals("bar", searcher.doc(td.scoreDocs[1].doc).get("value"));
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests sorting on internal docid order */
|
||||
public void testFieldDoc() throws Exception {
|
||||
Directory dir = newDirectory();
|
||||
|
@ -102,6 +180,32 @@ public class TestSort2 extends LuceneTestCase {
|
|||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests sorting on reverse internal docid order */
|
||||
public void testFieldDocReverse() throws Exception {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(newStringField("value", "foo", Field.Store.NO));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(newStringField("value", "bar", Field.Store.NO));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(ir);
|
||||
Sort sort = new Sort(new SortField(null, SortField.Type.DOC, true));
|
||||
|
||||
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
|
||||
assertEquals(2, td.totalHits);
|
||||
// docid 1, then docid 0
|
||||
assertEquals(1, td.scoreDocs[0].doc);
|
||||
assertEquals(0, td.scoreDocs[1].doc);
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests default sort (by score) */
|
||||
public void testFieldScore() throws Exception {
|
||||
Directory dir = newDirectory();
|
||||
|
@ -132,6 +236,35 @@ public class TestSort2 extends LuceneTestCase {
|
|||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests default sort (by score) in reverse */
|
||||
public void testFieldScoreReverse() throws Exception {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(newTextField("value", "foo bar bar bar bar", Field.Store.NO));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(newTextField("value", "foo foo foo foo foo", Field.Store.NO));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(ir);
|
||||
Sort sort = new Sort(new SortField(null, SortField.Type.SCORE, true));
|
||||
|
||||
TopDocs actual = searcher.search(new TermQuery(new Term("value", "foo")), 10, sort);
|
||||
assertEquals(2, actual.totalHits);
|
||||
|
||||
TopDocs expected = searcher.search(new TermQuery(new Term("value", "foo")), 10);
|
||||
// the two topdocs should be the reverse of each other
|
||||
assertEquals(expected.totalHits, actual.totalHits);
|
||||
assertEquals(actual.scoreDocs[0].doc, expected.scoreDocs[1].doc);
|
||||
assertEquals(actual.scoreDocs[1].doc, expected.scoreDocs[0].doc);
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests sorting on type byte */
|
||||
public void testByte() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
|
@ -162,6 +295,36 @@ public class TestSort2 extends LuceneTestCase {
|
|||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests sorting on type byte in reverse */
|
||||
public void testByteReverse() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(newStringField("value", "23", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(newStringField("value", "-1", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(newStringField("value", "4", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(ir);
|
||||
Sort sort = new Sort(new SortField("value", SortField.Type.BYTE, true));
|
||||
|
||||
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
|
||||
assertEquals(3, td.totalHits);
|
||||
// reverse numeric order
|
||||
assertEquals("23", searcher.doc(td.scoreDocs[0].doc).get("value"));
|
||||
assertEquals("4", searcher.doc(td.scoreDocs[1].doc).get("value"));
|
||||
assertEquals("-1", searcher.doc(td.scoreDocs[2].doc).get("value"));
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests sorting on type short */
|
||||
public void testShort() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
|
@ -192,6 +355,36 @@ public class TestSort2 extends LuceneTestCase {
|
|||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests sorting on type short in reverse */
|
||||
public void testShortReverse() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(newStringField("value", "300", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(newStringField("value", "-1", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(newStringField("value", "4", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(ir);
|
||||
Sort sort = new Sort(new SortField("value", SortField.Type.SHORT, true));
|
||||
|
||||
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
|
||||
assertEquals(3, td.totalHits);
|
||||
// reverse numeric order
|
||||
assertEquals("300", searcher.doc(td.scoreDocs[0].doc).get("value"));
|
||||
assertEquals("4", searcher.doc(td.scoreDocs[1].doc).get("value"));
|
||||
assertEquals("-1", searcher.doc(td.scoreDocs[2].doc).get("value"));
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests sorting on type int */
|
||||
public void testInt() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
|
@ -222,6 +415,36 @@ public class TestSort2 extends LuceneTestCase {
|
|||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests sorting on type int in reverse */
|
||||
public void testIntReverse() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(newStringField("value", "300000", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(newStringField("value", "-1", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(newStringField("value", "4", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(ir);
|
||||
Sort sort = new Sort(new SortField("value", SortField.Type.INT, true));
|
||||
|
||||
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
|
||||
assertEquals(3, td.totalHits);
|
||||
// reverse numeric order
|
||||
assertEquals("300000", searcher.doc(td.scoreDocs[0].doc).get("value"));
|
||||
assertEquals("4", searcher.doc(td.scoreDocs[1].doc).get("value"));
|
||||
assertEquals("-1", searcher.doc(td.scoreDocs[2].doc).get("value"));
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests sorting on type long */
|
||||
public void testLong() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
|
@ -252,6 +475,36 @@ public class TestSort2 extends LuceneTestCase {
|
|||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests sorting on type long in reverse */
|
||||
public void testLongReverse() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(newStringField("value", "3000000000", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(newStringField("value", "-1", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(newStringField("value", "4", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(ir);
|
||||
Sort sort = new Sort(new SortField("value", SortField.Type.LONG, true));
|
||||
|
||||
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
|
||||
assertEquals(3, td.totalHits);
|
||||
// reverse numeric order
|
||||
assertEquals("3000000000", searcher.doc(td.scoreDocs[0].doc).get("value"));
|
||||
assertEquals("4", searcher.doc(td.scoreDocs[1].doc).get("value"));
|
||||
assertEquals("-1", searcher.doc(td.scoreDocs[2].doc).get("value"));
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests sorting on type float */
|
||||
public void testFloat() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
|
@ -282,6 +535,36 @@ public class TestSort2 extends LuceneTestCase {
|
|||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests sorting on type float in reverse */
|
||||
public void testFloatReverse() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(newStringField("value", "30.1", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(newStringField("value", "-1.3", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(newStringField("value", "4.2", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(ir);
|
||||
Sort sort = new Sort(new SortField("value", SortField.Type.FLOAT, true));
|
||||
|
||||
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
|
||||
assertEquals(3, td.totalHits);
|
||||
// reverse numeric order
|
||||
assertEquals("30.1", searcher.doc(td.scoreDocs[0].doc).get("value"));
|
||||
assertEquals("4.2", searcher.doc(td.scoreDocs[1].doc).get("value"));
|
||||
assertEquals("-1.3", searcher.doc(td.scoreDocs[2].doc).get("value"));
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests sorting on type double */
|
||||
public void testDouble() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
|
@ -316,6 +599,40 @@ public class TestSort2 extends LuceneTestCase {
|
|||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests sorting on type double in reverse */
|
||||
public void testDoubleReverse() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(newStringField("value", "30.1", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(newStringField("value", "-1.3", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(newStringField("value", "4.2333333333333", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(newStringField("value", "4.2333333333332", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(ir);
|
||||
Sort sort = new Sort(new SortField("value", SortField.Type.DOUBLE, true));
|
||||
|
||||
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
|
||||
assertEquals(4, td.totalHits);
|
||||
// numeric order
|
||||
assertEquals("30.1", searcher.doc(td.scoreDocs[0].doc).get("value"));
|
||||
assertEquals("4.2333333333333", searcher.doc(td.scoreDocs[1].doc).get("value"));
|
||||
assertEquals("4.2333333333332", searcher.doc(td.scoreDocs[2].doc).get("value"));
|
||||
assertEquals("-1.3", searcher.doc(td.scoreDocs[3].doc).get("value"));
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
public void testEmptyStringVsNullStringSort() throws Exception {
|
||||
Directory dir = newDirectory();
|
||||
IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(
|
||||
|
|
|
@ -0,0 +1,643 @@
|
|||
package org.apache.lucene.search;
|
||||
|
||||
/*
|
||||
* 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.document.BinaryDocValuesField;
|
||||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.document.DoubleDocValuesField;
|
||||
import org.apache.lucene.document.Field;
|
||||
import org.apache.lucene.document.FloatDocValuesField;
|
||||
import org.apache.lucene.document.NumericDocValuesField;
|
||||
import org.apache.lucene.document.SortedDocValuesField;
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.index.RandomIndexWriter;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.util.BytesRef;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
|
||||
/** Tests basic sorting on docvalues fields.
|
||||
* These are mostly like TestSort's tests, except each test
|
||||
* indexes the field up-front as docvalues, and checks no fieldcaches were made */
|
||||
public class TestSortDocValues extends LuceneTestCase {
|
||||
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
// ensure there is nothing in fieldcache before test starts
|
||||
FieldCache.DEFAULT.purgeAllCaches();
|
||||
}
|
||||
|
||||
private void assertNoFieldCaches() {
|
||||
// docvalues sorting should NOT create any fieldcache entries!
|
||||
assertEquals(0, FieldCache.DEFAULT.getCacheEntries().length);
|
||||
}
|
||||
|
||||
/** Tests sorting on type string */
|
||||
public void testString() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(new SortedDocValuesField("value", new BytesRef("foo")));
|
||||
doc.add(newStringField("value", "foo", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new SortedDocValuesField("value", new BytesRef("bar")));
|
||||
doc.add(newStringField("value", "bar", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(ir);
|
||||
Sort sort = new Sort(new SortField("value", SortField.Type.STRING));
|
||||
|
||||
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
|
||||
assertEquals(2, td.totalHits);
|
||||
// 'bar' comes before 'foo'
|
||||
assertEquals("bar", searcher.doc(td.scoreDocs[0].doc).get("value"));
|
||||
assertEquals("foo", searcher.doc(td.scoreDocs[1].doc).get("value"));
|
||||
assertNoFieldCaches();
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests reverse sorting on type string */
|
||||
public void testStringReverse() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(new SortedDocValuesField("value", new BytesRef("bar")));
|
||||
doc.add(newStringField("value", "bar", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new SortedDocValuesField("value", new BytesRef("foo")));
|
||||
doc.add(newStringField("value", "foo", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(ir);
|
||||
Sort sort = new Sort(new SortField("value", SortField.Type.STRING, true));
|
||||
|
||||
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
|
||||
assertEquals(2, td.totalHits);
|
||||
// 'foo' comes after 'bar' in reverse order
|
||||
assertEquals("foo", searcher.doc(td.scoreDocs[0].doc).get("value"));
|
||||
assertEquals("bar", searcher.doc(td.scoreDocs[1].doc).get("value"));
|
||||
assertNoFieldCaches();
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests sorting on type string_val */
|
||||
public void testStringVal() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(new BinaryDocValuesField("value", new BytesRef("foo")));
|
||||
doc.add(newStringField("value", "foo", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new BinaryDocValuesField("value", new BytesRef("bar")));
|
||||
doc.add(newStringField("value", "bar", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(ir);
|
||||
Sort sort = new Sort(new SortField("value", SortField.Type.STRING_VAL));
|
||||
|
||||
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
|
||||
assertEquals(2, td.totalHits);
|
||||
// 'bar' comes before 'foo'
|
||||
assertEquals("bar", searcher.doc(td.scoreDocs[0].doc).get("value"));
|
||||
assertEquals("foo", searcher.doc(td.scoreDocs[1].doc).get("value"));
|
||||
assertNoFieldCaches();
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests reverse sorting on type string_val */
|
||||
public void testStringValReverse() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(new BinaryDocValuesField("value", new BytesRef("bar")));
|
||||
doc.add(newStringField("value", "bar", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new BinaryDocValuesField("value", new BytesRef("foo")));
|
||||
doc.add(newStringField("value", "foo", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(ir);
|
||||
Sort sort = new Sort(new SortField("value", SortField.Type.STRING_VAL, true));
|
||||
|
||||
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
|
||||
assertEquals(2, td.totalHits);
|
||||
// 'foo' comes after 'bar' in reverse order
|
||||
assertEquals("foo", searcher.doc(td.scoreDocs[0].doc).get("value"));
|
||||
assertEquals("bar", searcher.doc(td.scoreDocs[1].doc).get("value"));
|
||||
assertNoFieldCaches();
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests sorting on type string_val, but with a SortedDocValuesField */
|
||||
public void testStringValSorted() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(new SortedDocValuesField("value", new BytesRef("foo")));
|
||||
doc.add(newStringField("value", "foo", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new SortedDocValuesField("value", new BytesRef("bar")));
|
||||
doc.add(newStringField("value", "bar", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(ir);
|
||||
Sort sort = new Sort(new SortField("value", SortField.Type.STRING_VAL));
|
||||
|
||||
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
|
||||
assertEquals(2, td.totalHits);
|
||||
// 'bar' comes before 'foo'
|
||||
assertEquals("bar", searcher.doc(td.scoreDocs[0].doc).get("value"));
|
||||
assertEquals("foo", searcher.doc(td.scoreDocs[1].doc).get("value"));
|
||||
assertNoFieldCaches();
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests reverse sorting on type string_val, but with a SortedDocValuesField */
|
||||
public void testStringValReverseSorted() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(new SortedDocValuesField("value", new BytesRef("bar")));
|
||||
doc.add(newStringField("value", "bar", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new SortedDocValuesField("value", new BytesRef("foo")));
|
||||
doc.add(newStringField("value", "foo", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(ir);
|
||||
Sort sort = new Sort(new SortField("value", SortField.Type.STRING_VAL, true));
|
||||
|
||||
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
|
||||
assertEquals(2, td.totalHits);
|
||||
// 'foo' comes after 'bar' in reverse order
|
||||
assertEquals("foo", searcher.doc(td.scoreDocs[0].doc).get("value"));
|
||||
assertEquals("bar", searcher.doc(td.scoreDocs[1].doc).get("value"));
|
||||
assertNoFieldCaches();
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests sorting on type byte */
|
||||
public void testByte() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(new NumericDocValuesField("value", 23));
|
||||
doc.add(newStringField("value", "23", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new NumericDocValuesField("value", -1));
|
||||
doc.add(newStringField("value", "-1", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new NumericDocValuesField("value", 4));
|
||||
doc.add(newStringField("value", "4", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(ir);
|
||||
Sort sort = new Sort(new SortField("value", SortField.Type.BYTE));
|
||||
|
||||
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
|
||||
assertEquals(3, td.totalHits);
|
||||
// numeric order
|
||||
assertEquals("-1", searcher.doc(td.scoreDocs[0].doc).get("value"));
|
||||
assertEquals("4", searcher.doc(td.scoreDocs[1].doc).get("value"));
|
||||
assertEquals("23", searcher.doc(td.scoreDocs[2].doc).get("value"));
|
||||
assertNoFieldCaches();
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests sorting on type byte in reverse */
|
||||
public void testByteReverse() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(new NumericDocValuesField("value", 23));
|
||||
doc.add(newStringField("value", "23", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new NumericDocValuesField("value", -1));
|
||||
doc.add(newStringField("value", "-1", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new NumericDocValuesField("value", 4));
|
||||
doc.add(newStringField("value", "4", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(ir);
|
||||
Sort sort = new Sort(new SortField("value", SortField.Type.BYTE, true));
|
||||
|
||||
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
|
||||
assertEquals(3, td.totalHits);
|
||||
// reverse numeric order
|
||||
assertEquals("23", searcher.doc(td.scoreDocs[0].doc).get("value"));
|
||||
assertEquals("4", searcher.doc(td.scoreDocs[1].doc).get("value"));
|
||||
assertEquals("-1", searcher.doc(td.scoreDocs[2].doc).get("value"));
|
||||
assertNoFieldCaches();
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests sorting on type short */
|
||||
public void testShort() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(new NumericDocValuesField("value", 300));
|
||||
doc.add(newStringField("value", "300", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new NumericDocValuesField("value", -1));
|
||||
doc.add(newStringField("value", "-1", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new NumericDocValuesField("value", 4));
|
||||
doc.add(newStringField("value", "4", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(ir);
|
||||
Sort sort = new Sort(new SortField("value", SortField.Type.SHORT));
|
||||
|
||||
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
|
||||
assertEquals(3, td.totalHits);
|
||||
// numeric order
|
||||
assertEquals("-1", searcher.doc(td.scoreDocs[0].doc).get("value"));
|
||||
assertEquals("4", searcher.doc(td.scoreDocs[1].doc).get("value"));
|
||||
assertEquals("300", searcher.doc(td.scoreDocs[2].doc).get("value"));
|
||||
assertNoFieldCaches();
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests sorting on type short in reverse */
|
||||
public void testShortReverse() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(new NumericDocValuesField("value", 300));
|
||||
doc.add(newStringField("value", "300", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new NumericDocValuesField("value", -1));
|
||||
doc.add(newStringField("value", "-1", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new NumericDocValuesField("value", 4));
|
||||
doc.add(newStringField("value", "4", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(ir);
|
||||
Sort sort = new Sort(new SortField("value", SortField.Type.SHORT, true));
|
||||
|
||||
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
|
||||
assertEquals(3, td.totalHits);
|
||||
// reverse numeric order
|
||||
assertEquals("300", searcher.doc(td.scoreDocs[0].doc).get("value"));
|
||||
assertEquals("4", searcher.doc(td.scoreDocs[1].doc).get("value"));
|
||||
assertEquals("-1", searcher.doc(td.scoreDocs[2].doc).get("value"));
|
||||
assertNoFieldCaches();
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests sorting on type int */
|
||||
public void testInt() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(new NumericDocValuesField("value", 300000));
|
||||
doc.add(newStringField("value", "300000", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new NumericDocValuesField("value", -1));
|
||||
doc.add(newStringField("value", "-1", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new NumericDocValuesField("value", 4));
|
||||
doc.add(newStringField("value", "4", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(ir);
|
||||
Sort sort = new Sort(new SortField("value", SortField.Type.INT));
|
||||
|
||||
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
|
||||
assertEquals(3, td.totalHits);
|
||||
// numeric order
|
||||
assertEquals("-1", searcher.doc(td.scoreDocs[0].doc).get("value"));
|
||||
assertEquals("4", searcher.doc(td.scoreDocs[1].doc).get("value"));
|
||||
assertEquals("300000", searcher.doc(td.scoreDocs[2].doc).get("value"));
|
||||
assertNoFieldCaches();
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests sorting on type int in reverse */
|
||||
public void testIntReverse() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(new NumericDocValuesField("value", 300000));
|
||||
doc.add(newStringField("value", "300000", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new NumericDocValuesField("value", -1));
|
||||
doc.add(newStringField("value", "-1", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new NumericDocValuesField("value", 4));
|
||||
doc.add(newStringField("value", "4", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(ir);
|
||||
Sort sort = new Sort(new SortField("value", SortField.Type.INT, true));
|
||||
|
||||
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
|
||||
assertEquals(3, td.totalHits);
|
||||
// reverse numeric order
|
||||
assertEquals("300000", searcher.doc(td.scoreDocs[0].doc).get("value"));
|
||||
assertEquals("4", searcher.doc(td.scoreDocs[1].doc).get("value"));
|
||||
assertEquals("-1", searcher.doc(td.scoreDocs[2].doc).get("value"));
|
||||
assertNoFieldCaches();
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests sorting on type long */
|
||||
public void testLong() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(new NumericDocValuesField("value", 3000000000L));
|
||||
doc.add(newStringField("value", "3000000000", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new NumericDocValuesField("value", -1));
|
||||
doc.add(newStringField("value", "-1", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new NumericDocValuesField("value", 4));
|
||||
doc.add(newStringField("value", "4", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(ir);
|
||||
Sort sort = new Sort(new SortField("value", SortField.Type.LONG));
|
||||
|
||||
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
|
||||
assertEquals(3, td.totalHits);
|
||||
// numeric order
|
||||
assertEquals("-1", searcher.doc(td.scoreDocs[0].doc).get("value"));
|
||||
assertEquals("4", searcher.doc(td.scoreDocs[1].doc).get("value"));
|
||||
assertEquals("3000000000", searcher.doc(td.scoreDocs[2].doc).get("value"));
|
||||
assertNoFieldCaches();
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests sorting on type long in reverse */
|
||||
public void testLongReverse() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(new NumericDocValuesField("value", 3000000000L));
|
||||
doc.add(newStringField("value", "3000000000", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new NumericDocValuesField("value", -1));
|
||||
doc.add(newStringField("value", "-1", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new NumericDocValuesField("value", 4));
|
||||
doc.add(newStringField("value", "4", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(ir);
|
||||
Sort sort = new Sort(new SortField("value", SortField.Type.LONG, true));
|
||||
|
||||
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
|
||||
assertEquals(3, td.totalHits);
|
||||
// reverse numeric order
|
||||
assertEquals("3000000000", searcher.doc(td.scoreDocs[0].doc).get("value"));
|
||||
assertEquals("4", searcher.doc(td.scoreDocs[1].doc).get("value"));
|
||||
assertEquals("-1", searcher.doc(td.scoreDocs[2].doc).get("value"));
|
||||
assertNoFieldCaches();
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests sorting on type float */
|
||||
public void testFloat() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(new FloatDocValuesField("value", 30.1F));
|
||||
doc.add(newStringField("value", "30.1", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new FloatDocValuesField("value", -1.3F));
|
||||
doc.add(newStringField("value", "-1.3", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new FloatDocValuesField("value", 4.2F));
|
||||
doc.add(newStringField("value", "4.2", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(ir);
|
||||
Sort sort = new Sort(new SortField("value", SortField.Type.FLOAT));
|
||||
|
||||
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
|
||||
assertEquals(3, td.totalHits);
|
||||
// numeric order
|
||||
assertEquals("-1.3", searcher.doc(td.scoreDocs[0].doc).get("value"));
|
||||
assertEquals("4.2", searcher.doc(td.scoreDocs[1].doc).get("value"));
|
||||
assertEquals("30.1", searcher.doc(td.scoreDocs[2].doc).get("value"));
|
||||
assertNoFieldCaches();
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests sorting on type float in reverse */
|
||||
public void testFloatReverse() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(new FloatDocValuesField("value", 30.1F));
|
||||
doc.add(newStringField("value", "30.1", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new FloatDocValuesField("value", -1.3F));
|
||||
doc.add(newStringField("value", "-1.3", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new FloatDocValuesField("value", 4.2F));
|
||||
doc.add(newStringField("value", "4.2", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(ir);
|
||||
Sort sort = new Sort(new SortField("value", SortField.Type.FLOAT, true));
|
||||
|
||||
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
|
||||
assertEquals(3, td.totalHits);
|
||||
// reverse numeric order
|
||||
assertEquals("30.1", searcher.doc(td.scoreDocs[0].doc).get("value"));
|
||||
assertEquals("4.2", searcher.doc(td.scoreDocs[1].doc).get("value"));
|
||||
assertEquals("-1.3", searcher.doc(td.scoreDocs[2].doc).get("value"));
|
||||
assertNoFieldCaches();
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests sorting on type double */
|
||||
public void testDouble() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(new DoubleDocValuesField("value", 30.1));
|
||||
doc.add(newStringField("value", "30.1", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new DoubleDocValuesField("value", -1.3));
|
||||
doc.add(newStringField("value", "-1.3", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new DoubleDocValuesField("value", 4.2333333333333));
|
||||
doc.add(newStringField("value", "4.2333333333333", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new DoubleDocValuesField("value", 4.2333333333332));
|
||||
doc.add(newStringField("value", "4.2333333333332", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(ir);
|
||||
Sort sort = new Sort(new SortField("value", SortField.Type.DOUBLE));
|
||||
|
||||
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
|
||||
assertEquals(4, td.totalHits);
|
||||
// numeric order
|
||||
assertEquals("-1.3", searcher.doc(td.scoreDocs[0].doc).get("value"));
|
||||
assertEquals("4.2333333333332", searcher.doc(td.scoreDocs[1].doc).get("value"));
|
||||
assertEquals("4.2333333333333", searcher.doc(td.scoreDocs[2].doc).get("value"));
|
||||
assertEquals("30.1", searcher.doc(td.scoreDocs[3].doc).get("value"));
|
||||
assertNoFieldCaches();
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
/** Tests sorting on type double in reverse */
|
||||
public void testDoubleReverse() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(new DoubleDocValuesField("value", 30.1));
|
||||
doc.add(newStringField("value", "30.1", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new DoubleDocValuesField("value", -1.3));
|
||||
doc.add(newStringField("value", "-1.3", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new DoubleDocValuesField("value", 4.2333333333333));
|
||||
doc.add(newStringField("value", "4.2333333333333", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(new DoubleDocValuesField("value", 4.2333333333332));
|
||||
doc.add(newStringField("value", "4.2333333333332", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(ir);
|
||||
Sort sort = new Sort(new SortField("value", SortField.Type.DOUBLE, true));
|
||||
|
||||
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
|
||||
assertEquals(4, td.totalHits);
|
||||
// numeric order
|
||||
assertEquals("30.1", searcher.doc(td.scoreDocs[0].doc).get("value"));
|
||||
assertEquals("4.2333333333333", searcher.doc(td.scoreDocs[1].doc).get("value"));
|
||||
assertEquals("4.2333333333332", searcher.doc(td.scoreDocs[2].doc).get("value"));
|
||||
assertEquals("-1.3", searcher.doc(td.scoreDocs[3].doc).get("value"));
|
||||
assertNoFieldCaches();
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue