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.search.query.SortParseElement.java

License:Apache License

@Override
public void parse(JsonParser jp, SearchContext context) throws Exception {
    JsonToken token = jp.getCurrentToken();
    List<SortField> sortFields = Lists.newArrayListWithCapacity(2);
    if (token == JsonToken.START_ARRAY) {
        while ((token = jp.nextToken()) != JsonToken.END_ARRAY) {
            if (token == JsonToken.START_OBJECT) {
                addCompoundSortField(jp, context, sortFields);
            } else if (token == JsonToken.VALUE_STRING) {
                addSortField(context, sortFields, jp.getText(), false, -1);
            }//from   w ww .ja  v a 2s.  c o  m
        }
    } else {
        addCompoundSortField(jp, context, sortFields);
    }
    if (!sortFields.isEmpty()) {
        context.sort(new Sort(sortFields.toArray(new SortField[sortFields.size()])));
    }
}

From source file:org.elasticsearch.search.sort.SortParseElement.java

License:Apache License

@Override
public void parse(XContentParser parser, SearchContext context) throws Exception {
    XContentParser.Token token = parser.currentToken();
    List<SortField> sortFields = Lists.newArrayListWithCapacity(2);
    if (token == XContentParser.Token.START_ARRAY) {
        while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
            if (token == XContentParser.Token.START_OBJECT) {
                addCompoundSortField(parser, context, sortFields);
            } else if (token == XContentParser.Token.VALUE_STRING) {
                addSortField(context, sortFields, parser.text(), false, false, null, null, null, null);
            } else {
                throw new ElasticsearchIllegalArgumentException(
                        "malformed sort format, within the sort array, an object, or an actual string are allowed");
            }//from  w  ww.  ja va2 s  .  co m
        }
    } else if (token == XContentParser.Token.VALUE_STRING) {
        addSortField(context, sortFields, parser.text(), false, false, null, null, null, null);
    } else if (token == XContentParser.Token.START_OBJECT) {
        addCompoundSortField(parser, context, sortFields);
    } else {
        throw new ElasticsearchIllegalArgumentException(
                "malformed sort format, either start with array, object, or an actual string");
    }
    if (!sortFields.isEmpty()) {
        // optimize if we just sort on score non reversed, we don't really need sorting
        boolean sort;
        if (sortFields.size() > 1) {
            sort = true;
        } else {
            SortField sortField = sortFields.get(0);
            if (sortField.getType() == SortField.Type.SCORE && !sortField.getReverse()) {
                sort = false;
            } else {
                sort = true;
            }
        }
        if (sort) {
            context.sort(new Sort(sortFields.toArray(new SortField[sortFields.size()])));
        }
    }
}

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

License:Apache License

@Test
public void testSingleValueAllSet() throws Exception {
    fillSingleValueAllSet();/*from ww w .  j  av  a2s. com*/
    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();

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

    assertThat(bytesValues.hasValue(0), equalTo(true));
    assertThat(bytesValues.hasValue(1), equalTo(true));
    assertThat(bytesValues.hasValue(2), equalTo(true));

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

    BytesRef bytesRef = new BytesRef();
    assertThat(bytesValues.getValueScratch(0, bytesRef), equalTo(new BytesRef(two())));
    assertThat(bytesRef, equalTo(new BytesRef(two())));
    assertThat(bytesValues.getValueScratch(1, bytesRef), equalTo(new BytesRef(one())));
    assertThat(bytesRef, equalTo(new BytesRef(one())));
    assertThat(bytesValues.getValueScratch(2, bytesRef), equalTo(new BytesRef(three())));
    assertThat(bytesRef, equalTo(new BytesRef(three())));

    BytesValues.Iter bytesValuesIter = bytesValues.getIter(0);
    assertThat(bytesValuesIter.hasNext(), equalTo(true));
    assertThat(bytesValuesIter.next(), equalTo(new BytesRef(two())));
    assertThat(bytesValuesIter.hasNext(), equalTo(false));

    BytesValues hashedBytesValues = fieldData.getBytesValues();

    assertThat(hashedBytesValues.hasValue(0), equalTo(true));
    assertThat(hashedBytesValues.hasValue(1), equalTo(true));
    assertThat(hashedBytesValues.hasValue(2), equalTo(true));

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

    BytesValues.Iter hashedBytesValuesIter = hashedBytesValues.getIter(0);
    assertThat(hashedBytesValuesIter.hasNext(), equalTo(true));
    assertThat(new HashedBytesRef(hashedBytesValuesIter.next(), hashedBytesValuesIter.hash()),
            equalTo(new HashedBytesRef(two())));
    assertThat(hashedBytesValuesIter.hasNext(), equalTo(false));

    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.test.unit.index.fielddata.AbstractFieldDataImplTests.java

License:Apache License

@Test
public void testMultiValueAllSet() throws Exception {
    fillMultiValueAllSet();//from w w  w.  ja  v  a 2s  . co  m
    IndexFieldData indexFieldData = getForField("value");
    AtomicFieldData fieldData = indexFieldData.load(refreshReader());
    assertThat(fieldData.getMemorySizeInBytes(), greaterThan(0l));

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

    BytesValues bytesValues = fieldData.getBytesValues();

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

    assertThat(bytesValues.hasValue(0), equalTo(true));
    assertThat(bytesValues.hasValue(1), equalTo(true));
    assertThat(bytesValues.hasValue(2), equalTo(true));

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

    BytesRef bytesRef = new BytesRef();
    assertThat(bytesValues.getValueScratch(0, bytesRef), equalTo(new BytesRef(two())));
    assertThat(bytesRef, equalTo(new BytesRef(two())));
    assertThat(bytesValues.getValueScratch(1, bytesRef), equalTo(new BytesRef(one())));
    assertThat(bytesRef, equalTo(new BytesRef(one())));
    assertThat(bytesValues.getValueScratch(2, bytesRef), equalTo(new BytesRef(three())));
    assertThat(bytesRef, equalTo(new BytesRef(three())));

    BytesValues.Iter bytesValuesIter = bytesValues.getIter(0);
    assertThat(bytesValuesIter.hasNext(), equalTo(true));
    assertThat(bytesValuesIter.next(), equalTo(new BytesRef(two())));
    assertThat(bytesValuesIter.hasNext(), equalTo(true));
    assertThat(bytesValuesIter.next(), equalTo(new BytesRef(four())));
    assertThat(bytesValuesIter.hasNext(), equalTo(false));

    BytesValues hashedBytesValues = fieldData.getBytesValues();

    assertThat(hashedBytesValues.hasValue(0), equalTo(true));
    assertThat(hashedBytesValues.hasValue(1), equalTo(true));
    assertThat(hashedBytesValues.hasValue(2), equalTo(true));

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

    BytesValues.Iter hashedBytesValuesIter = hashedBytesValues.getIter(0);
    assertThat(hashedBytesValuesIter.hasNext(), equalTo(true));
    assertThat(new HashedBytesRef(hashedBytesValuesIter.next(), hashedBytesValuesIter.hash()),
            equalTo(new HashedBytesRef(two())));
    assertThat(hashedBytesValuesIter.hasNext(), equalTo(true));
    assertThat(new HashedBytesRef(hashedBytesValuesIter.next(), hashedBytesValuesIter.hash()),
            equalTo(new HashedBytesRef(four())));
    assertThat(hashedBytesValuesIter.hasNext(), equalTo(false));

    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.test.unit.index.fielddata.AbstractNumericFieldDataTests.java

License:Apache License

@Test
public void testSingleValueAllSetNumber() throws Exception {
    fillSingleValueAllSet();/*  ww  w .j  ava 2 s  .co  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.hasValue(0), equalTo(true));
    assertThat(longValues.hasValue(1), equalTo(true));
    assertThat(longValues.hasValue(2), equalTo(true));

    assertThat(longValues.getValue(0), equalTo(2l));
    assertThat(longValues.getValue(1), equalTo(1l));
    assertThat(longValues.getValue(2), equalTo(3l));

    assertThat(longValues.getValueMissing(0, -1), equalTo(2l));
    assertThat(longValues.getValueMissing(1, -1), equalTo(1l));
    assertThat(longValues.getValueMissing(2, -1), equalTo(3l));

    LongValues.Iter longValuesIter = longValues.getIter(0);
    assertThat(longValuesIter.hasNext(), equalTo(true));
    assertThat(longValuesIter.next(), equalTo(2l));
    assertThat(longValuesIter.hasNext(), equalTo(false));

    longValuesIter = longValues.getIter(1);
    assertThat(longValuesIter.hasNext(), equalTo(true));
    assertThat(longValuesIter.next(), equalTo(1l));
    assertThat(longValuesIter.hasNext(), equalTo(false));

    longValuesIter = longValues.getIter(2);
    assertThat(longValuesIter.hasNext(), equalTo(true));
    assertThat(longValuesIter.next(), equalTo(3l));
    assertThat(longValuesIter.hasNext(), equalTo(false));

    DoubleValues doubleValues = fieldData.getDoubleValues();

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

    assertThat(doubleValues.hasValue(0), equalTo(true));
    assertThat(doubleValues.hasValue(1), equalTo(true));
    assertThat(doubleValues.hasValue(2), equalTo(true));

    assertThat(doubleValues.getValue(0), equalTo(2d));
    assertThat(doubleValues.getValue(1), equalTo(1d));
    assertThat(doubleValues.getValue(2), equalTo(3d));

    assertThat(doubleValues.getValueMissing(0, -1), equalTo(2d));
    assertThat(doubleValues.getValueMissing(1, -1), equalTo(1d));
    assertThat(doubleValues.getValueMissing(2, -1), equalTo(3d));

    DoubleValues.Iter doubleValuesIter = doubleValues.getIter(0);
    assertThat(doubleValuesIter.hasNext(), equalTo(true));
    assertThat(doubleValuesIter.next(), equalTo(2d));
    assertThat(doubleValuesIter.hasNext(), equalTo(false));

    doubleValuesIter = doubleValues.getIter(1);
    assertThat(doubleValuesIter.hasNext(), equalTo(true));
    assertThat(doubleValuesIter.next(), equalTo(1d));
    assertThat(doubleValuesIter.hasNext(), equalTo(false));

    doubleValuesIter = doubleValues.getIter(2);
    assertThat(doubleValuesIter.hasNext(), equalTo(true));
    assertThat(doubleValuesIter.next(), equalTo(3d));
    assertThat(doubleValuesIter.hasNext(), equalTo(false));

    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.test.unit.index.fielddata.AbstractNumericFieldDataTests.java

License:Apache License

@Test
public void testSingleValueWithMissingNumber() throws Exception {
    fillSingleValueWithMissing();//from w w  w .ja  v a 2  s.  co 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.hasValue(0), equalTo(true));
    assertThat(longValues.hasValue(1), equalTo(false));
    assertThat(longValues.hasValue(2), equalTo(true));

    assertThat(longValues.getValue(0), equalTo(2l));
    assertThat(longValues.getValue(2), equalTo(3l));

    assertThat(longValues.getValueMissing(0, -1), equalTo(2l));
    assertThat(longValues.getValueMissing(1, -1), equalTo(-1l));
    assertThat(longValues.getValueMissing(2, -1), equalTo(3l));

    LongValues.Iter longValuesIter = longValues.getIter(0);
    assertThat(longValuesIter.hasNext(), equalTo(true));
    assertThat(longValuesIter.next(), equalTo(2l));
    assertThat(longValuesIter.hasNext(), equalTo(false));

    longValuesIter = longValues.getIter(1);
    assertThat(longValuesIter.hasNext(), equalTo(false));

    longValuesIter = longValues.getIter(2);
    assertThat(longValuesIter.hasNext(), equalTo(true));
    assertThat(longValuesIter.next(), equalTo(3l));
    assertThat(longValuesIter.hasNext(), equalTo(false));

    DoubleValues doubleValues = fieldData.getDoubleValues();

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

    assertThat(doubleValues.hasValue(0), equalTo(true));
    assertThat(doubleValues.hasValue(1), equalTo(false));
    assertThat(doubleValues.hasValue(2), equalTo(true));

    assertThat(doubleValues.getValue(0), equalTo(2d));
    assertThat(doubleValues.getValue(2), equalTo(3d));

    assertThat(doubleValues.getValueMissing(0, -1), equalTo(2d));
    assertThat(doubleValues.getValueMissing(1, -1), equalTo(-1d));
    assertThat(doubleValues.getValueMissing(2, -1), equalTo(3d));

    DoubleValues.Iter doubleValuesIter = doubleValues.getIter(0);
    assertThat(doubleValuesIter.hasNext(), equalTo(true));
    assertThat(doubleValuesIter.next(), equalTo(2d));
    assertThat(doubleValuesIter.hasNext(), equalTo(false));

    doubleValuesIter = doubleValues.getIter(1);
    assertThat(doubleValuesIter.hasNext(), equalTo(false));

    doubleValuesIter = doubleValues.getIter(2);
    assertThat(doubleValuesIter.hasNext(), equalTo(true));
    assertThat(doubleValuesIter.next(), equalTo(3d));
    assertThat(doubleValuesIter.hasNext(), equalTo(false));

    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));
}

From source file:org.elasticsearch.test.unit.index.fielddata.AbstractStringFieldDataTests.java

License:Apache License

@Test
public void testSortMultiValuesFields() throws Exception {
    fillExtendedMvSet();/*from  ww w. j a  v a 2 s.c o  m*/
    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(1));
    assertThat(((FieldDoc) topDocs.scoreDocs[0]).fields[0], equalTo(null));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(5));
    assertThat(((FieldDoc) topDocs.scoreDocs[1]).fields[0], equalTo(null));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(7));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).utf8ToString(), equalTo("!08"));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(0));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).utf8ToString(), equalTo("02"));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(2));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).utf8ToString(), equalTo("03"));
    assertThat(topDocs.scoreDocs[5].doc, equalTo(3));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[5]).fields[0]).utf8ToString(), equalTo("04"));
    assertThat(topDocs.scoreDocs[6].doc, equalTo(4));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[6]).fields[0]).utf8ToString(), equalTo("06"));
    assertThat(topDocs.scoreDocs[7].doc, equalTo(6));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[7]).fields[0]).utf8ToString(), equalTo("08"));

    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.test.unit.index.fielddata.NumericFieldDataTests.java

License:Apache License

@Test
public void testSingleValueAllSetNumber() throws Exception {
    fillSingleValueAllSet();//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.hasValue(0), equalTo(true));
    assertThat(longValues.hasValue(1), equalTo(true));
    assertThat(longValues.hasValue(2), equalTo(true));

    assertThat(longValues.getValue(0), equalTo(2l));
    assertThat(longValues.getValue(1), equalTo(1l));
    assertThat(longValues.getValue(2), equalTo(3l));

    assertThat(longValues.getValueMissing(0, -1), equalTo(2l));
    assertThat(longValues.getValueMissing(1, -1), equalTo(1l));
    assertThat(longValues.getValueMissing(2, -1), equalTo(3l));

    LongArrayRef longArrayRef = longValues.getValues(0);
    assertThat(longArrayRef.size(), equalTo(1));
    assertThat(longArrayRef.values[longArrayRef.start], equalTo(2l));

    longArrayRef = longValues.getValues(1);
    assertThat(longArrayRef.size(), equalTo(1));
    assertThat(longArrayRef.values[longArrayRef.start], equalTo(1l));

    longArrayRef = longValues.getValues(2);
    assertThat(longArrayRef.size(), equalTo(1));
    assertThat(longArrayRef.values[longArrayRef.start], equalTo(3l));

    LongValues.Iter longValuesIter = longValues.getIter(0);
    assertThat(longValuesIter.hasNext(), equalTo(true));
    assertThat(longValuesIter.next(), equalTo(2l));
    assertThat(longValuesIter.hasNext(), equalTo(false));

    longValuesIter = longValues.getIter(1);
    assertThat(longValuesIter.hasNext(), equalTo(true));
    assertThat(longValuesIter.next(), equalTo(1l));
    assertThat(longValuesIter.hasNext(), equalTo(false));

    longValuesIter = longValues.getIter(2);
    assertThat(longValuesIter.hasNext(), equalTo(true));
    assertThat(longValuesIter.next(), equalTo(3l));
    assertThat(longValuesIter.hasNext(), equalTo(false));

    longValues.forEachValueInDoc(0, new LongValuesVerifierProc(0).addExpected(2l));
    longValues.forEachValueInDoc(1, new LongValuesVerifierProc(1).addExpected(1l));
    longValues.forEachValueInDoc(2, new LongValuesVerifierProc(2).addExpected(3l));

    DoubleValues doubleValues = fieldData.getDoubleValues();

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

    assertThat(doubleValues.hasValue(0), equalTo(true));
    assertThat(doubleValues.hasValue(1), equalTo(true));
    assertThat(doubleValues.hasValue(2), equalTo(true));

    assertThat(doubleValues.getValue(0), equalTo(2d));
    assertThat(doubleValues.getValue(1), equalTo(1d));
    assertThat(doubleValues.getValue(2), equalTo(3d));

    assertThat(doubleValues.getValueMissing(0, -1), equalTo(2d));
    assertThat(doubleValues.getValueMissing(1, -1), equalTo(1d));
    assertThat(doubleValues.getValueMissing(2, -1), equalTo(3d));

    DoubleArrayRef doubleArrayRef = doubleValues.getValues(0);
    assertThat(doubleArrayRef.size(), equalTo(1));
    assertThat(doubleArrayRef.values[doubleArrayRef.start], equalTo(2d));

    doubleArrayRef = doubleValues.getValues(1);
    assertThat(doubleArrayRef.size(), equalTo(1));
    assertThat(doubleArrayRef.values[doubleArrayRef.start], equalTo(1d));

    doubleArrayRef = doubleValues.getValues(2);
    assertThat(doubleArrayRef.size(), equalTo(1));
    assertThat(doubleArrayRef.values[doubleArrayRef.start], equalTo(3d));

    DoubleValues.Iter doubleValuesIter = doubleValues.getIter(0);
    assertThat(doubleValuesIter.hasNext(), equalTo(true));
    assertThat(doubleValuesIter.next(), equalTo(2d));
    assertThat(doubleValuesIter.hasNext(), equalTo(false));

    doubleValuesIter = doubleValues.getIter(1);
    assertThat(doubleValuesIter.hasNext(), equalTo(true));
    assertThat(doubleValuesIter.next(), equalTo(1d));
    assertThat(doubleValuesIter.hasNext(), equalTo(false));

    doubleValuesIter = doubleValues.getIter(2);
    assertThat(doubleValuesIter.hasNext(), equalTo(true));
    assertThat(doubleValuesIter.next(), equalTo(3d));
    assertThat(doubleValuesIter.hasNext(), equalTo(false));

    doubleValues.forEachValueInDoc(0, new DoubleValuesVerifierProc(0).addExpected(2d));
    doubleValues.forEachValueInDoc(1, new DoubleValuesVerifierProc(1).addExpected(1d));
    doubleValues.forEachValueInDoc(2, new DoubleValuesVerifierProc(2).addExpected(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.test.unit.index.fielddata.NumericFieldDataTests.java

License:Apache License

@Test
public void testSingleValueWithMissingNumber() throws Exception {
    fillSingleValueWithMissing();//from   w ww .ja 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.hasValue(0), equalTo(true));
    assertThat(longValues.hasValue(1), equalTo(false));
    assertThat(longValues.hasValue(2), equalTo(true));

    assertThat(longValues.getValue(0), equalTo(2l));
    assertThat(longValues.getValue(2), equalTo(3l));

    assertThat(longValues.getValueMissing(0, -1), equalTo(2l));
    assertThat(longValues.getValueMissing(1, -1), equalTo(-1l));
    assertThat(longValues.getValueMissing(2, -1), equalTo(3l));

    LongArrayRef longArrayRef = longValues.getValues(0);
    assertThat(longArrayRef.size(), equalTo(1));
    assertThat(longArrayRef.values[longArrayRef.start], equalTo(2l));

    longArrayRef = longValues.getValues(1);
    assertThat(longArrayRef.size(), equalTo(0));

    longArrayRef = longValues.getValues(2);
    assertThat(longArrayRef.size(), equalTo(1));
    assertThat(longArrayRef.values[longArrayRef.start], equalTo(3l));

    LongValues.Iter longValuesIter = longValues.getIter(0);
    assertThat(longValuesIter.hasNext(), equalTo(true));
    assertThat(longValuesIter.next(), equalTo(2l));
    assertThat(longValuesIter.hasNext(), equalTo(false));

    longValuesIter = longValues.getIter(1);
    assertThat(longValuesIter.hasNext(), equalTo(false));

    longValuesIter = longValues.getIter(2);
    assertThat(longValuesIter.hasNext(), equalTo(true));
    assertThat(longValuesIter.next(), equalTo(3l));
    assertThat(longValuesIter.hasNext(), equalTo(false));

    longValues.forEachValueInDoc(0, new LongValuesVerifierProc(0).addExpected(2l));
    longValues.forEachValueInDoc(1, new LongValuesVerifierProc(1).addMissing());
    longValues.forEachValueInDoc(2, new LongValuesVerifierProc(2).addExpected(3l));

    DoubleValues doubleValues = fieldData.getDoubleValues();

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

    assertThat(doubleValues.hasValue(0), equalTo(true));
    assertThat(doubleValues.hasValue(1), equalTo(false));
    assertThat(doubleValues.hasValue(2), equalTo(true));

    assertThat(doubleValues.getValue(0), equalTo(2d));
    assertThat(doubleValues.getValue(2), equalTo(3d));

    assertThat(doubleValues.getValueMissing(0, -1), equalTo(2d));
    assertThat(doubleValues.getValueMissing(1, -1), equalTo(-1d));
    assertThat(doubleValues.getValueMissing(2, -1), equalTo(3d));

    DoubleArrayRef doubleArrayRef = doubleValues.getValues(0);
    assertThat(doubleArrayRef.size(), equalTo(1));
    assertThat(doubleArrayRef.values[doubleArrayRef.start], equalTo(2d));

    doubleArrayRef = doubleValues.getValues(1);
    assertThat(doubleArrayRef.size(), equalTo(0));

    doubleArrayRef = doubleValues.getValues(2);
    assertThat(doubleArrayRef.size(), equalTo(1));
    assertThat(doubleArrayRef.values[doubleArrayRef.start], equalTo(3d));

    DoubleValues.Iter doubleValuesIter = doubleValues.getIter(0);
    assertThat(doubleValuesIter.hasNext(), equalTo(true));
    assertThat(doubleValuesIter.next(), equalTo(2d));
    assertThat(doubleValuesIter.hasNext(), equalTo(false));

    doubleValuesIter = doubleValues.getIter(1);
    assertThat(doubleValuesIter.hasNext(), equalTo(false));

    doubleValuesIter = doubleValues.getIter(2);
    assertThat(doubleValuesIter.hasNext(), equalTo(true));
    assertThat(doubleValuesIter.next(), equalTo(3d));
    assertThat(doubleValuesIter.hasNext(), equalTo(false));

    doubleValues.forEachValueInDoc(0, new DoubleValuesVerifierProc(0).addExpected(2d));
    doubleValues.forEachValueInDoc(1, new DoubleValuesVerifierProc(1).addMissing());
    doubleValues.forEachValueInDoc(2, new DoubleValuesVerifierProc(2).addExpected(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));
}

From source file:org.elasticsearch.test.unit.index.fielddata.StringFieldDataTests.java

License:Apache License

@Test
public void testSingleValueAllSet() throws Exception {
    fillSingleValueAllSet();// ww  w. j  a  v a 2s .c  o m
    IndexFieldData indexFieldData = getForField("value");
    AtomicReaderContext readerContext = refreshReader();
    AtomicFieldData fieldData = indexFieldData.load(readerContext);

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

    BytesValues bytesValues = fieldData.getBytesValues();

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

    assertThat(bytesValues.hasValue(0), equalTo(true));
    assertThat(bytesValues.hasValue(1), equalTo(true));
    assertThat(bytesValues.hasValue(2), equalTo(true));

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

    BytesRef bytesRef = new BytesRef();
    assertThat(bytesValues.getValueScratch(0, bytesRef), equalTo(new BytesRef(two())));
    assertThat(bytesRef, equalTo(new BytesRef(two())));
    assertThat(bytesValues.getValueScratch(1, bytesRef), equalTo(new BytesRef(one())));
    assertThat(bytesRef, equalTo(new BytesRef(one())));
    assertThat(bytesValues.getValueScratch(2, bytesRef), equalTo(new BytesRef(three())));
    assertThat(bytesRef, equalTo(new BytesRef(three())));

    BytesRefArrayRef bytesRefArrayRef = bytesValues.getValues(0);
    assertThat(bytesRefArrayRef.size(), equalTo(1));
    assertThat(bytesRefArrayRef.values[bytesRefArrayRef.start], equalTo(new BytesRef(two())));

    bytesRefArrayRef = bytesValues.getValues(1);
    assertThat(bytesRefArrayRef.size(), equalTo(1));
    assertThat(bytesRefArrayRef.values[bytesRefArrayRef.start], equalTo(new BytesRef(one())));

    bytesRefArrayRef = bytesValues.getValues(2);
    assertThat(bytesRefArrayRef.size(), equalTo(1));
    assertThat(bytesRefArrayRef.values[bytesRefArrayRef.start], equalTo(new BytesRef(three())));

    BytesValues.Iter bytesValuesIter = bytesValues.getIter(0);
    assertThat(bytesValuesIter.hasNext(), equalTo(true));
    assertThat(bytesValuesIter.next(), equalTo(new BytesRef(two())));
    assertThat(bytesValuesIter.hasNext(), equalTo(false));

    bytesValues.forEachValueInDoc(0, new BytesValuesVerifierProc(0).addExpected(two()));
    bytesValues.forEachValueInDoc(1, new BytesValuesVerifierProc(1).addExpected(one()));
    bytesValues.forEachValueInDoc(2, new BytesValuesVerifierProc(2).addExpected(three()));

    HashedBytesValues hashedBytesValues = fieldData.getHashedBytesValues();

    assertThat(hashedBytesValues.hasValue(0), equalTo(true));
    assertThat(hashedBytesValues.hasValue(1), equalTo(true));
    assertThat(hashedBytesValues.hasValue(2), equalTo(true));

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

    HashedBytesValues.Iter hashedBytesValuesIter = hashedBytesValues.getIter(0);
    assertThat(hashedBytesValuesIter.hasNext(), equalTo(true));
    assertThat(hashedBytesValuesIter.next(), equalTo(new HashedBytesRef(two())));
    assertThat(hashedBytesValuesIter.hasNext(), equalTo(false));

    hashedBytesValues.forEachValueInDoc(0, new HashedBytesValuesVerifierProc(0).addExpected(two()));
    hashedBytesValues.forEachValueInDoc(1, new HashedBytesValuesVerifierProc(1).addExpected(one()));
    hashedBytesValues.forEachValueInDoc(2, new HashedBytesValuesVerifierProc(2).addExpected(three()));

    StringValues stringValues = fieldData.getStringValues();

    assertThat(stringValues.hasValue(0), equalTo(true));
    assertThat(stringValues.hasValue(1), equalTo(true));
    assertThat(stringValues.hasValue(2), equalTo(true));

    assertThat(stringValues.getValue(0), equalTo(two()));
    assertThat(stringValues.getValue(1), equalTo(one()));
    assertThat(stringValues.getValue(2), equalTo(three()));

    StringArrayRef stringArrayRef;
    stringArrayRef = stringValues.getValues(0);
    assertThat(stringArrayRef.size(), equalTo(1));
    assertThat(stringArrayRef.values[stringArrayRef.start], equalTo(two()));

    stringArrayRef = stringValues.getValues(1);
    assertThat(stringArrayRef.size(), equalTo(1));
    assertThat(stringArrayRef.values[stringArrayRef.start], equalTo(one()));

    stringArrayRef = stringValues.getValues(2);
    assertThat(stringArrayRef.size(), equalTo(1));
    assertThat(stringArrayRef.values[stringArrayRef.start], equalTo(three()));

    StringValues.Iter stringValuesIter = stringValues.getIter(0);
    assertThat(stringValuesIter.hasNext(), equalTo(true));
    assertThat(stringValuesIter.next(), equalTo(two()));
    assertThat(stringValuesIter.hasNext(), equalTo(false));

    stringValuesIter = stringValues.getIter(1);
    assertThat(stringValuesIter.hasNext(), equalTo(true));
    assertThat(stringValuesIter.next(), equalTo(one()));
    assertThat(stringValuesIter.hasNext(), equalTo(false));

    stringValuesIter = stringValues.getIter(2);
    assertThat(stringValuesIter.hasNext(), equalTo(true));
    assertThat(stringValuesIter.next(), equalTo(three()));
    assertThat(stringValuesIter.hasNext(), equalTo(false));

    stringValues.forEachValueInDoc(0, new StringValuesVerifierProc(0).addExpected(two()));
    stringValues.forEachValueInDoc(1, new StringValuesVerifierProc(1).addExpected(one()));
    stringValues.forEachValueInDoc(2, new StringValuesVerifierProc(2).addExpected(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));
}