Example usage for org.apache.lucene.search Sort Sort

List of usage examples for org.apache.lucene.search Sort Sort

Introduction

In this page you can find the example usage for org.apache.lucene.search Sort Sort.

Prototype

public Sort(SortField... fields) 

Source Link

Document

Sets the sort to the given criteria in succession: the first SortField is checked first, but if it produces a tie, then the second SortField is used to break the tie, etc.

Usage

From source file:org.elasticsearch.index.fielddata.AbstractFieldDataImplTestCase.java

License:Apache License

@Test
public void testMultiValueAllSet() throws Exception {
    fillMultiValueAllSet();//from   w  w  w .ja  v  a  2  s  .  c  o m
    IndexFieldData indexFieldData = getForField("value");
    AtomicFieldData fieldData = indexFieldData.load(refreshReader());
    assertThat(fieldData.ramBytesUsed(), greaterThanOrEqualTo(minRamBytesUsed()));

    SortedBinaryDocValues bytesValues = fieldData.getBytesValues();

    assertValues(bytesValues, 0, two(), four());
    assertValues(bytesValues, 1, one());
    assertValues(bytesValues, 2, three());

    IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(writer, true));
    TopFieldDocs topDocs = searcher.search(new MatchAllDocsQuery(), 10,
            new Sort(new SortField("value", indexFieldData.comparatorSource(null, MultiValueMode.MIN, null))));
    assertThat(topDocs.totalHits, equalTo(3));
    assertThat(topDocs.scoreDocs.length, equalTo(3));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(1));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(0));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(2));

    topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(
            new SortField("value", indexFieldData.comparatorSource(null, MultiValueMode.MAX, null), true)));
    assertThat(topDocs.totalHits, equalTo(3));
    assertThat(topDocs.scoreDocs.length, equalTo(3));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(0));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(2));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(1));
}

From source file:org.elasticsearch.index.fielddata.AbstractFieldDataImplTestCase.java

License:Apache License

@Test
public void testSortMultiValuesFields() throws Exception {
    fillExtendedMvSet();/*from w  ww .  j a  va2s. c om*/
    IndexFieldData indexFieldData = getForField("value");

    IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(writer, true));
    TopFieldDocs topDocs = searcher.search(new MatchAllDocsQuery(), 10,
            new Sort(new SortField("value", indexFieldData.comparatorSource(null, MultiValueMode.MIN, null))));
    assertThat(topDocs.totalHits, equalTo(8));
    assertThat(topDocs.scoreDocs.length, equalTo(8));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(7));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).utf8ToString(), equalTo("!08"));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(0));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).utf8ToString(), equalTo("02"));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(2));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).utf8ToString(), equalTo("03"));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(3));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).utf8ToString(), equalTo("04"));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(4));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).utf8ToString(), equalTo("06"));
    assertThat(topDocs.scoreDocs[5].doc, equalTo(6));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[5]).fields[0]).utf8ToString(), equalTo("08"));
    assertThat(topDocs.scoreDocs[6].doc, equalTo(1));
    assertThat((BytesRef) ((FieldDoc) topDocs.scoreDocs[6]).fields[0], equalTo(null));
    assertThat(topDocs.scoreDocs[7].doc, equalTo(5));
    assertThat((BytesRef) ((FieldDoc) topDocs.scoreDocs[7]).fields[0], equalTo(null));

    topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(
            new SortField("value", indexFieldData.comparatorSource(null, MultiValueMode.MAX, null), true)));
    assertThat(topDocs.totalHits, equalTo(8));
    assertThat(topDocs.scoreDocs.length, equalTo(8));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(6));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).utf8ToString(), equalTo("10"));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(4));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).utf8ToString(), equalTo("08"));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(3));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).utf8ToString(), equalTo("06"));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(0));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).utf8ToString(), equalTo("04"));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(2));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).utf8ToString(), equalTo("03"));
    assertThat(topDocs.scoreDocs[5].doc, equalTo(7));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[5]).fields[0]).utf8ToString(), equalTo("!10"));
    assertThat(topDocs.scoreDocs[6].doc, equalTo(1));
    assertThat(((FieldDoc) topDocs.scoreDocs[6]).fields[0], equalTo(null));
    assertThat(topDocs.scoreDocs[7].doc, equalTo(5));
    assertThat(((FieldDoc) topDocs.scoreDocs[7]).fields[0], equalTo(null));
}

From source file:org.elasticsearch.index.fielddata.AbstractFieldDataImplTests.java

License:Apache License

@Test
public void testSingleValueAllSet() throws Exception {
    fillSingleValueAllSet();/*  www .j a  v a  2  s.c  om*/
    IndexFieldData indexFieldData = getForField("value");
    AtomicReaderContext readerContext = refreshReader();
    AtomicFieldData fieldData = indexFieldData.load(readerContext);
    assertThat(fieldData.getMemorySizeInBytes(), greaterThan(0l));

    assertThat(fieldData.getNumDocs(), equalTo(3));

    BytesValues bytesValues = fieldData.getBytesValues(randomBoolean());

    assertThat(bytesValues.isMultiValued(), equalTo(false));

    assertThat(bytesValues.setDocument(0), equalTo(1));
    assertThat(bytesValues.nextValue(), equalTo(new BytesRef(two())));
    assertThat(bytesValues.setDocument(1), equalTo(1));
    assertThat(bytesValues.nextValue(), equalTo(new BytesRef(one())));
    assertThat(bytesValues.setDocument(2), equalTo(1));
    assertThat(bytesValues.nextValue(), equalTo(new BytesRef(three())));

    assertValues(bytesValues, 0, two());
    assertValues(bytesValues, 1, one());
    assertValues(bytesValues, 2, three());

    BytesValues hashedBytesValues = fieldData.getBytesValues(randomBoolean());

    assertThat(convert(hashedBytesValues, 0), equalTo(new HashedBytesRef(two())));
    assertThat(convert(hashedBytesValues, 1), equalTo(new HashedBytesRef(one())));
    assertThat(convert(hashedBytesValues, 2), equalTo(new HashedBytesRef(three())));
    assertHashedValues(hashedBytesValues, 0, two());
    assertHashedValues(hashedBytesValues, 1, one());
    assertHashedValues(hashedBytesValues, 2, three());

    IndexSearcher searcher = new IndexSearcher(readerContext.reader());
    TopFieldDocs topDocs;

    topDocs = searcher.search(new MatchAllDocsQuery(), 10,
            new Sort(new SortField("value", indexFieldData.comparatorSource(null, SortMode.MIN))));
    assertThat(topDocs.totalHits, equalTo(3));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(1));
    assertThat(toString(((FieldDoc) topDocs.scoreDocs[0]).fields[0]), equalTo(one()));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(0));
    assertThat(toString(((FieldDoc) topDocs.scoreDocs[1]).fields[0]), equalTo(two()));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(2));
    assertThat(toString(((FieldDoc) topDocs.scoreDocs[2]).fields[0]), equalTo(three()));

    topDocs = searcher.search(new MatchAllDocsQuery(), 10,
            new Sort(new SortField("value", indexFieldData.comparatorSource(null, SortMode.MAX), true)));
    assertThat(topDocs.totalHits, equalTo(3));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(2));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(0));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(1));
}

From source file:org.elasticsearch.index.fielddata.AbstractFieldDataImplTests.java

License:Apache License

@Test
public void testMultiValueAllSet() throws Exception {
    fillMultiValueAllSet();/*from ww  w .j  a  v  a2  s .  c om*/
    IndexFieldData indexFieldData = getForField("value");
    AtomicFieldData fieldData = indexFieldData.load(refreshReader());
    assertThat(fieldData.getMemorySizeInBytes(), greaterThan(0l));

    assertThat(fieldData.getNumDocs(), equalTo(3));

    BytesValues bytesValues = fieldData.getBytesValues(randomBoolean());

    assertThat(bytesValues.isMultiValued(), equalTo(true));

    assertValues(bytesValues, 0, two(), four());
    assertValues(bytesValues, 1, one());
    assertValues(bytesValues, 2, three());

    BytesValues hashedBytesValues = fieldData.getBytesValues(randomBoolean());

    assertThat(convert(hashedBytesValues, 0), equalTo(new HashedBytesRef(two())));
    assertThat(convert(hashedBytesValues, 1), equalTo(new HashedBytesRef(one())));
    assertThat(convert(hashedBytesValues, 2), equalTo(new HashedBytesRef(three())));

    assertHashedValues(hashedBytesValues, 0, two(), four());

    IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(writer, true));
    TopFieldDocs topDocs = searcher.search(new MatchAllDocsQuery(), 10,
            new Sort(new SortField("value", indexFieldData.comparatorSource(null, SortMode.MIN))));
    assertThat(topDocs.totalHits, equalTo(3));
    assertThat(topDocs.scoreDocs.length, equalTo(3));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(1));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(0));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(2));

    topDocs = searcher.search(new MatchAllDocsQuery(), 10,
            new Sort(new SortField("value", indexFieldData.comparatorSource(null, SortMode.MAX), true)));
    assertThat(topDocs.totalHits, equalTo(3));
    assertThat(topDocs.scoreDocs.length, equalTo(3));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(0));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(2));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(1));
}

From source file:org.elasticsearch.index.fielddata.AbstractFieldDataImplTests.java

License:Apache License

@Test
public void testSortMultiValuesFields() throws Exception {
    fillExtendedMvSet();//w  ww  .j  ava2s.c  om
    IndexFieldData indexFieldData = getForField("value");

    IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(writer, true));
    TopFieldDocs topDocs = searcher.search(new MatchAllDocsQuery(), 10,
            new Sort(new SortField("value", indexFieldData.comparatorSource(null, SortMode.MIN))));
    assertThat(topDocs.totalHits, equalTo(8));
    assertThat(topDocs.scoreDocs.length, equalTo(8));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(7));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).utf8ToString(), equalTo("!08"));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(0));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).utf8ToString(), equalTo("02"));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(2));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).utf8ToString(), equalTo("03"));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(3));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).utf8ToString(), equalTo("04"));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(4));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).utf8ToString(), equalTo("06"));
    assertThat(topDocs.scoreDocs[5].doc, equalTo(6));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[5]).fields[0]).utf8ToString(), equalTo("08"));
    assertThat(topDocs.scoreDocs[6].doc, equalTo(1));
    assertThat((BytesRef) ((FieldDoc) topDocs.scoreDocs[6]).fields[0],
            equalTo(BytesRefFieldComparatorSource.MAX_TERM));
    assertThat(topDocs.scoreDocs[7].doc, equalTo(5));
    assertThat((BytesRef) ((FieldDoc) topDocs.scoreDocs[7]).fields[0],
            equalTo(BytesRefFieldComparatorSource.MAX_TERM));

    topDocs = searcher.search(new MatchAllDocsQuery(), 10,
            new Sort(new SortField("value", indexFieldData.comparatorSource(null, SortMode.MAX), true)));
    assertThat(topDocs.totalHits, equalTo(8));
    assertThat(topDocs.scoreDocs.length, equalTo(8));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(6));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).utf8ToString(), equalTo("10"));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(4));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).utf8ToString(), equalTo("08"));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(3));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).utf8ToString(), equalTo("06"));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(0));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).utf8ToString(), equalTo("04"));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(2));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).utf8ToString(), equalTo("03"));
    assertThat(topDocs.scoreDocs[5].doc, equalTo(7));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[5]).fields[0]).utf8ToString(), equalTo("!10"));
    assertThat(topDocs.scoreDocs[6].doc, equalTo(1));
    assertThat(((FieldDoc) topDocs.scoreDocs[6]).fields[0], equalTo(null));
    assertThat(topDocs.scoreDocs[7].doc, equalTo(5));
    assertThat(((FieldDoc) topDocs.scoreDocs[7]).fields[0], equalTo(null));
}

From source file:org.elasticsearch.index.fielddata.AbstractNumericFieldDataTestCase.java

License:Apache License

@Test
public void testSingleValueAllSetNumber() throws Exception {
    fillSingleValueAllSet();/*from w  ww.ja v a 2  s .c om*/
    IndexNumericFieldData indexFieldData = getForField("value");
    AtomicNumericFieldData fieldData = indexFieldData.load(refreshReader());

    SortedNumericDocValues longValues = fieldData.getLongValues();

    assertThat(FieldData.isMultiValued(longValues), equalTo(false));

    longValues.setDocument(0);
    assertThat(longValues.count(), equalTo(1));
    assertThat(longValues.valueAt(0), equalTo(2l));

    longValues.setDocument(1);
    assertThat(longValues.count(), equalTo(1));
    assertThat(longValues.valueAt(0), equalTo(1l));

    longValues.setDocument(2);
    assertThat(longValues.count(), equalTo(1));
    assertThat(longValues.valueAt(0), equalTo(3l));

    SortedNumericDoubleValues doubleValues = fieldData.getDoubleValues();

    assertThat(FieldData.isMultiValued(doubleValues), equalTo(false));

    doubleValues.setDocument(0);
    assertThat(1, equalTo(doubleValues.count()));
    assertThat(doubleValues.valueAt(0), equalTo(2d));

    doubleValues.setDocument(1);
    assertThat(1, equalTo(doubleValues.count()));
    assertThat(doubleValues.valueAt(0), equalTo(1d));

    doubleValues.setDocument(2);
    assertThat(1, equalTo(doubleValues.count()));
    assertThat(doubleValues.valueAt(0), equalTo(3d));

    IndexSearcher searcher = new IndexSearcher(readerContext.reader());
    TopFieldDocs topDocs;

    topDocs = searcher.search(new MatchAllDocsQuery(), 10,
            new Sort(new SortField("value", indexFieldData.comparatorSource(null, MultiValueMode.MIN, null))));
    assertThat(topDocs.totalHits, equalTo(3));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(1));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(0));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(2));

    topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(
            new SortField("value", indexFieldData.comparatorSource(null, MultiValueMode.MAX, null), true)));
    assertThat(topDocs.totalHits, equalTo(3));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(2));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(0));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(1));
}

From source file:org.elasticsearch.index.fielddata.AbstractNumericFieldDataTestCase.java

License:Apache License

@Test
public void testSingleValueWithMissingNumber() throws Exception {
    fillSingleValueWithMissing();//from  w  w w.  j a v a 2  s. c o  m
    IndexNumericFieldData indexFieldData = getForField("value");
    AtomicNumericFieldData fieldData = indexFieldData.load(refreshReader());

    SortedNumericDocValues longValues = fieldData.getLongValues();

    assertThat(FieldData.isMultiValued(longValues), equalTo(false));

    longValues.setDocument(0);
    assertThat(longValues.count(), equalTo(1));
    assertThat(longValues.valueAt(0), equalTo(2l));

    longValues.setDocument(1);
    assertThat(longValues.count(), equalTo(0));

    longValues.setDocument(2);
    assertThat(longValues.count(), equalTo(1));
    assertThat(longValues.valueAt(0), equalTo(3l));

    SortedNumericDoubleValues doubleValues = fieldData.getDoubleValues();

    assertThat(FieldData.isMultiValued(doubleValues), equalTo(false));

    doubleValues.setDocument(0);
    assertThat(1, equalTo(doubleValues.count()));
    assertThat(doubleValues.valueAt(0), equalTo(2d));

    doubleValues.setDocument(1);
    assertThat(0, equalTo(doubleValues.count()));

    doubleValues.setDocument(2);
    assertThat(1, equalTo(doubleValues.count()));
    assertThat(doubleValues.valueAt(0), equalTo(3d));

    IndexSearcher searcher = new IndexSearcher(readerContext.reader());
    TopFieldDocs topDocs;

    topDocs = searcher.search(new MatchAllDocsQuery(), 10,
            new Sort(new SortField("value", indexFieldData.comparatorSource(null, MultiValueMode.MIN, null)))); // defaults to _last
    assertThat(topDocs.totalHits, equalTo(3));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(0));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(2));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(1));

    topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(
            new SortField("value", indexFieldData.comparatorSource(null, MultiValueMode.MAX, null), true))); // defaults to _last
    assertThat(topDocs.totalHits, equalTo(3));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(2));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(0));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(1));

    topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(
            new SortField("value", indexFieldData.comparatorSource("_first", MultiValueMode.MIN, null))));
    assertThat(topDocs.totalHits, equalTo(3));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(1));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(0));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(2));

    topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(
            new SortField("value", indexFieldData.comparatorSource("_first", MultiValueMode.MAX, null), true)));
    assertThat(topDocs.totalHits, equalTo(3));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(1));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(2));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(0));

    topDocs = searcher.search(new MatchAllDocsQuery(), 10,
            new Sort(new SortField("value", indexFieldData.comparatorSource("1", MultiValueMode.MIN, null))));
    assertThat(topDocs.totalHits, equalTo(3));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(1));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(0));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(2));

    topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(
            new SortField("value", indexFieldData.comparatorSource("1", MultiValueMode.MAX, null), true)));
    assertThat(topDocs.totalHits, equalTo(3));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(2));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(0));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(1));
}

From source file:org.elasticsearch.index.fielddata.AbstractNumericFieldDataTestCase.java

License:Apache License

@Override
@Test/*from   w  w  w.  j a v  a2 s .  com*/
public void testSortMultiValuesFields() throws Exception {
    fillExtendedMvSet();
    IndexFieldData indexFieldData = getForField("value");

    IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(writer, true));
    TopFieldDocs topDocs = searcher.search(new MatchAllDocsQuery(), 10,
            new Sort(new SortField("value", indexFieldData.comparatorSource(null, MultiValueMode.MIN, null)))); // defaults to _last
    assertThat(topDocs.totalHits, equalTo(8));
    assertThat(topDocs.scoreDocs.length, equalTo(8));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(7));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(-10));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(0));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(2));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(2));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(3));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(4));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(4));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(6));
    assertThat(topDocs.scoreDocs[5].doc, equalTo(6));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[5]).fields[0]).intValue(), equalTo(8));
    assertThat(topDocs.scoreDocs[6].doc, equalTo(1));
    //        assertThat(((FieldDoc) topDocs.scoreDocs[6]).fields[0], equalTo(null));
    assertThat(topDocs.scoreDocs[7].doc, equalTo(5));
    //        assertThat(((FieldDoc) topDocs.scoreDocs[7]).fields[0], equalTo(null));

    topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(
            new SortField("value", indexFieldData.comparatorSource(null, MultiValueMode.MAX, null), true))); // defaults to _last
    assertThat(topDocs.totalHits, equalTo(8));
    assertThat(topDocs.scoreDocs.length, equalTo(8));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(6));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(10));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(4));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(8));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(3));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(6));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(0));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(4));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(2));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[5].doc, equalTo(7));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[5]).fields[0]).intValue(), equalTo(-8));
    assertThat(topDocs.scoreDocs[6].doc, equalTo(1));
    //        assertThat(((FieldDoc) topDocs.scoreDocs[6]).fields[0], equalTo(null));
    assertThat(topDocs.scoreDocs[7].doc, equalTo(5));
    //        assertThat(((FieldDoc) topDocs.scoreDocs[7]).fields[0], equalTo(null));

    searcher = new IndexSearcher(DirectoryReader.open(writer, true));
    topDocs = searcher.search(new MatchAllDocsQuery(), 10,
            new Sort(new SortField("value", indexFieldData.comparatorSource(null, MultiValueMode.SUM, null)))); // defaults to _last
    assertThat(topDocs.totalHits, equalTo(8));
    assertThat(topDocs.scoreDocs.length, equalTo(8));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(7));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(-27));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(2));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(0));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(6));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(3));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(15));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(4));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(21));
    assertThat(topDocs.scoreDocs[5].doc, equalTo(6));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[5]).fields[0]).intValue(), equalTo(27));
    assertThat(topDocs.scoreDocs[6].doc, equalTo(1));
    //        assertThat(((FieldDoc) topDocs.scoreDocs[6]).fields[0], equalTo(null));
    assertThat(topDocs.scoreDocs[7].doc, equalTo(5));
    //        assertThat(((FieldDoc) topDocs.scoreDocs[7]).fields[0], equalTo(null));

    searcher = new IndexSearcher(DirectoryReader.open(writer, true));
    topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(
            new SortField("value", indexFieldData.comparatorSource(null, MultiValueMode.SUM, null), true))); // defaults to _last
    assertThat(topDocs.totalHits, equalTo(8));
    assertThat(topDocs.scoreDocs.length, equalTo(8));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(6));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(27));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(4));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(21));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(3));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(15));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(0));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(6));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(2));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[5].doc, equalTo(7));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[5]).fields[0]).intValue(), equalTo(-27));
    assertThat(topDocs.scoreDocs[6].doc, equalTo(1));
    //        assertThat(((FieldDoc) topDocs.scoreDocs[6]).fields[0], equalTo(null));
    assertThat(topDocs.scoreDocs[7].doc, equalTo(5));
    //        assertThat(((FieldDoc) topDocs.scoreDocs[7]).fields[0], equalTo(null));

    searcher = new IndexSearcher(DirectoryReader.open(writer, true));
    topDocs = searcher.search(new MatchAllDocsQuery(), 10,
            new Sort(new SortField("value", indexFieldData.comparatorSource(null, MultiValueMode.AVG, null)))); // defaults to _last
    assertThat(topDocs.totalHits, equalTo(8));
    assertThat(topDocs.scoreDocs.length, equalTo(8));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(7));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(-9));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(0));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(2));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(3));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(5));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(4));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(7));
    assertThat(topDocs.scoreDocs[5].doc, equalTo(6));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[5]).fields[0]).intValue(), equalTo(9));
    assertThat(topDocs.scoreDocs[6].doc, equalTo(1));
    //        assertThat(((FieldDoc) topDocs.scoreDocs[6]).fields[0], equalTo(null));
    assertThat(topDocs.scoreDocs[7].doc, equalTo(5));
    //        assertThat(((FieldDoc) topDocs.scoreDocs[7]).fields[0], equalTo(null));

    searcher = new IndexSearcher(DirectoryReader.open(writer, true));
    topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(
            new SortField("value", indexFieldData.comparatorSource(null, MultiValueMode.AVG, null), true))); // defaults to _last
    assertThat(topDocs.totalHits, equalTo(8));
    assertThat(topDocs.scoreDocs.length, equalTo(8));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(6));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(9));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(4));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(7));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(3));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(5));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(0));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(2));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[5].doc, equalTo(7));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[5]).fields[0]).intValue(), equalTo(-9));
    assertThat(topDocs.scoreDocs[6].doc, equalTo(1));
    //        assertThat(((FieldDoc) topDocs.scoreDocs[6]).fields[0], equalTo(null));
    assertThat(topDocs.scoreDocs[7].doc, equalTo(5));
    //        assertThat(((FieldDoc) topDocs.scoreDocs[7]).fields[0], equalTo(null));

    topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(
            new SortField("value", indexFieldData.comparatorSource("_first", MultiValueMode.MIN, null))));
    assertThat(topDocs.totalHits, equalTo(8));
    assertThat(topDocs.scoreDocs.length, equalTo(8));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(1));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(5));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(7));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(0));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(2));
    assertThat(topDocs.scoreDocs[5].doc, equalTo(3));
    assertThat(topDocs.scoreDocs[6].doc, equalTo(4));
    assertThat(topDocs.scoreDocs[7].doc, equalTo(6));

    topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(
            new SortField("value", indexFieldData.comparatorSource("_first", MultiValueMode.MAX, null), true)));
    assertThat(topDocs.totalHits, equalTo(8));
    assertThat(topDocs.scoreDocs.length, equalTo(8));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(1));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(5));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(6));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(4));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(3));
    assertThat(topDocs.scoreDocs[5].doc, equalTo(0));
    assertThat(topDocs.scoreDocs[6].doc, equalTo(2));
    assertThat(topDocs.scoreDocs[7].doc, equalTo(7));

    topDocs = searcher.search(new MatchAllDocsQuery(), 10,
            new Sort(new SortField("value", indexFieldData.comparatorSource("-9", MultiValueMode.MIN, null))));
    assertThat(topDocs.totalHits, equalTo(8));
    assertThat(topDocs.scoreDocs.length, equalTo(8));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(7));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(1));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(5));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(0));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(2));
    assertThat(topDocs.scoreDocs[5].doc, equalTo(3));
    assertThat(topDocs.scoreDocs[6].doc, equalTo(4));
    assertThat(topDocs.scoreDocs[7].doc, equalTo(6));

    topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(
            new SortField("value", indexFieldData.comparatorSource("9", MultiValueMode.MAX, null), true)));
    assertThat(topDocs.totalHits, equalTo(8));
    assertThat(topDocs.scoreDocs.length, equalTo(8));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(6));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(1));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(5));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(4));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(3));
    assertThat(topDocs.scoreDocs[5].doc, equalTo(0));
    assertThat(topDocs.scoreDocs[6].doc, equalTo(2));
    assertThat(topDocs.scoreDocs[7].doc, equalTo(7));
}

From source file:org.elasticsearch.index.fielddata.AbstractNumericFieldDataTests.java

License:Apache License

@Test
public void testSingleValueAllSetNumber() throws Exception {
    fillSingleValueAllSet();//from w w  w  .j a v  a  2 s  . c  o  m
    IndexNumericFieldData indexFieldData = getForField("value");
    AtomicNumericFieldData fieldData = indexFieldData.load(refreshReader());

    assertThat(fieldData.getNumDocs(), equalTo(3));

    LongValues longValues = fieldData.getLongValues();

    assertThat(longValues.isMultiValued(), equalTo(false));

    assertThat(longValues.setDocument(0), equalTo(1));
    assertThat(longValues.nextValue(), equalTo(2l));

    assertThat(longValues.setDocument(1), equalTo(1));
    assertThat(longValues.nextValue(), equalTo(1l));

    assertThat(longValues.setDocument(2), equalTo(1));
    assertThat(longValues.nextValue(), equalTo(3l));

    DoubleValues doubleValues = fieldData.getDoubleValues();

    assertThat(doubleValues.isMultiValued(), equalTo(false));

    assertThat(1, equalTo(doubleValues.setDocument(0)));
    assertThat(doubleValues.nextValue(), equalTo(2d));

    assertThat(1, equalTo(doubleValues.setDocument(1)));
    assertThat(doubleValues.nextValue(), equalTo(1d));

    assertThat(1, equalTo(doubleValues.setDocument(2)));
    assertThat(doubleValues.nextValue(), equalTo(3d));

    IndexSearcher searcher = new IndexSearcher(readerContext.reader());
    TopFieldDocs topDocs;

    topDocs = searcher.search(new MatchAllDocsQuery(), 10,
            new Sort(new SortField("value", indexFieldData.comparatorSource(null, SortMode.MIN))));
    assertThat(topDocs.totalHits, equalTo(3));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(1));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(0));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(2));

    topDocs = searcher.search(new MatchAllDocsQuery(), 10,
            new Sort(new SortField("value", indexFieldData.comparatorSource(null, SortMode.MAX), true)));
    assertThat(topDocs.totalHits, equalTo(3));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(2));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(0));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(1));
}

From source file:org.elasticsearch.index.fielddata.AbstractNumericFieldDataTests.java

License:Apache License

@Test
public void testSingleValueWithMissingNumber() throws Exception {
    fillSingleValueWithMissing();//  w  ww.  j a v a 2 s.c o  m
    IndexNumericFieldData indexFieldData = getForField("value");
    AtomicNumericFieldData fieldData = indexFieldData.load(refreshReader());

    assertThat(fieldData.getNumDocs(), equalTo(3));

    LongValues longValues = fieldData.getLongValues();

    assertThat(longValues.isMultiValued(), equalTo(false));

    assertThat(longValues.setDocument(0), equalTo(1));
    assertThat(longValues.nextValue(), equalTo(2l));

    assertThat(longValues.setDocument(1), equalTo(0));

    assertThat(longValues.setDocument(2), equalTo(1));
    assertThat(longValues.nextValue(), equalTo(3l));

    DoubleValues doubleValues = fieldData.getDoubleValues();

    assertThat(doubleValues.isMultiValued(), equalTo(false));

    assertThat(1, equalTo(doubleValues.setDocument(0)));
    assertThat(doubleValues.nextValue(), equalTo(2d));

    assertThat(0, equalTo(doubleValues.setDocument(1)));

    assertThat(1, equalTo(doubleValues.setDocument(2)));
    assertThat(doubleValues.nextValue(), equalTo(3d));

    IndexSearcher searcher = new IndexSearcher(readerContext.reader());
    TopFieldDocs topDocs;

    topDocs = searcher.search(new MatchAllDocsQuery(), 10,
            new Sort(new SortField("value", indexFieldData.comparatorSource(null, SortMode.MIN)))); // defaults to _last
    assertThat(topDocs.totalHits, equalTo(3));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(0));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(2));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(1));

    topDocs = searcher.search(new MatchAllDocsQuery(), 10,
            new Sort(new SortField("value", indexFieldData.comparatorSource(null, SortMode.MAX), true))); // defaults to _last
    assertThat(topDocs.totalHits, equalTo(3));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(2));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(0));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(1));

    topDocs = searcher.search(new MatchAllDocsQuery(), 10,
            new Sort(new SortField("value", indexFieldData.comparatorSource("_first", SortMode.MIN))));
    assertThat(topDocs.totalHits, equalTo(3));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(1));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(0));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(2));

    topDocs = searcher.search(new MatchAllDocsQuery(), 10,
            new Sort(new SortField("value", indexFieldData.comparatorSource("_first", SortMode.MAX), true)));
    assertThat(topDocs.totalHits, equalTo(3));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(1));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(2));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(0));

    topDocs = searcher.search(new MatchAllDocsQuery(), 10,
            new Sort(new SortField("value", indexFieldData.comparatorSource("1", SortMode.MIN))));
    assertThat(topDocs.totalHits, equalTo(3));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(1));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(0));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(2));

    topDocs = searcher.search(new MatchAllDocsQuery(), 10,
            new Sort(new SortField("value", indexFieldData.comparatorSource("1", SortMode.MAX), true)));
    assertThat(topDocs.totalHits, equalTo(3));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(2));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(0));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(1));
}