Search in sources :

Example 26 with TopFieldDocs

use of org.apache.lucene.search.TopFieldDocs in project elasticsearch by elastic.

the class AbstractNumberNestedSortingTestCase method testNestedSorting.

public void testNestedSorting() throws Exception {
    List<Document> docs = new ArrayList<>();
    Document document = new Document();
    document.add(createField("field2", 3));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 3));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 3));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(createField("field1", 1));
    docs.add(document);
    writer.addDocuments(docs);
    writer.commit();
    docs.clear();
    document = new Document();
    document.add(createField("field2", 3));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 3));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 2));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(createField("field1", 2));
    docs.add(document);
    writer.addDocuments(docs);
    docs.clear();
    document = new Document();
    document.add(createField("field2", 3));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 3));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 1));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(createField("field1", 3));
    docs.add(document);
    writer.addDocuments(docs);
    docs.clear();
    document = new Document();
    document.add(createField("field2", 3));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 3));
    document.add(new StringField("filter_1", "F", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 4));
    document.add(new StringField("filter_1", "F", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(createField("field1", 4));
    docs.add(document);
    writer.addDocuments(docs);
    writer.commit();
    docs.clear();
    document = new Document();
    document.add(createField("field2", 3));
    document.add(new StringField("filter_1", "F", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 3));
    document.add(new StringField("filter_1", "F", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 5));
    document.add(new StringField("filter_1", "F", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(createField("field1", 5));
    docs.add(document);
    writer.addDocuments(docs);
    writer.commit();
    docs.clear();
    document = new Document();
    document.add(createField("field2", 3));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 3));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 6));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(createField("field1", 6));
    docs.add(document);
    writer.addDocuments(docs);
    writer.commit();
    // This doc will not be included, because it doesn't have nested docs
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(createField("field1", 7));
    writer.addDocument(document);
    writer.commit();
    docs.clear();
    document = new Document();
    document.add(createField("field2", 3));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 3));
    document.add(new StringField("filter_1", "F", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 7));
    document.add(new StringField("filter_1", "F", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(createField("field1", 8));
    docs.add(document);
    writer.addDocuments(docs);
    writer.commit();
    // Some garbage docs, just to check if the NestedFieldComparator can deal with this.
    document = new Document();
    document.add(new StringField("fieldXXX", "x", Field.Store.NO));
    writer.addDocument(document);
    document = new Document();
    document.add(new StringField("fieldXXX", "x", Field.Store.NO));
    writer.addDocument(document);
    document = new Document();
    document.add(new StringField("fieldXXX", "x", Field.Store.NO));
    writer.addDocument(document);
    MultiValueMode sortMode = MultiValueMode.SUM;
    DirectoryReader directoryReader = DirectoryReader.open(writer);
    directoryReader = ElasticsearchDirectoryReader.wrap(directoryReader, new ShardId(indexService.index(), 0));
    IndexSearcher searcher = new IndexSearcher(directoryReader);
    Query parentFilter = new TermQuery(new Term("__type", "parent"));
    Query childFilter = Queries.not(parentFilter);
    XFieldComparatorSource nestedComparatorSource = createFieldComparator("field2", sortMode, null, createNested(searcher, parentFilter, childFilter));
    ToParentBlockJoinQuery query = new ToParentBlockJoinQuery(new ConstantScoreQuery(childFilter), new QueryBitSetProducer(parentFilter), ScoreMode.None);
    Sort sort = new Sort(new SortField("field2", nestedComparatorSource));
    TopFieldDocs topDocs = searcher.search(query, 5, sort);
    assertThat(topDocs.totalHits, equalTo(7));
    assertThat(topDocs.scoreDocs.length, equalTo(5));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(11));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(7));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(7));
    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(9));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(15));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(10));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(19));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(11));
    sort = new Sort(new SortField("field2", nestedComparatorSource, true));
    topDocs = searcher.search(query, 5, sort);
    assertThat(topDocs.totalHits, equalTo(7));
    assertThat(topDocs.scoreDocs.length, equalTo(5));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(28));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(13));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(23));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(12));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(19));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(11));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(15));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(10));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(3));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(9));
    childFilter = new TermQuery(new Term("filter_1", "T"));
    nestedComparatorSource = createFieldComparator("field2", sortMode, null, createNested(searcher, parentFilter, childFilter));
    query = new ToParentBlockJoinQuery(new ConstantScoreQuery(childFilter), new QueryBitSetProducer(parentFilter), ScoreMode.None);
    sort = new Sort(new SortField("field2", nestedComparatorSource, true));
    topDocs = searcher.search(query, 5, sort);
    assertThat(topDocs.totalHits, equalTo(6));
    assertThat(topDocs.scoreDocs.length, equalTo(5));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(23));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(12));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(3));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(9));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(7));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(8));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(11));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(7));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(15));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(3));
    sort = new Sort(new SortField("field2", nestedComparatorSource));
    topDocs = searcher.search(query, 5, sort);
    assertThat(topDocs.totalHits, equalTo(6));
    assertThat(topDocs.scoreDocs.length, equalTo(5));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(15));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(28));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(11));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(7));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(7));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(8));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(3));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(9));
    nestedComparatorSource = createFieldComparator("field2", sortMode, 127, createNested(searcher, parentFilter, childFilter));
    sort = new Sort(new SortField("field2", nestedComparatorSource, true));
    topDocs = searcher.search(new TermQuery(new Term("__type", "parent")), 5, sort);
    assertThat(topDocs.totalHits, equalTo(8));
    assertThat(topDocs.scoreDocs.length, equalTo(5));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(19));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(127));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(24));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(127));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(23));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(12));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(3));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(9));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(7));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(8));
    nestedComparatorSource = createFieldComparator("field2", sortMode, -127, createNested(searcher, parentFilter, childFilter));
    sort = new Sort(new SortField("field2", nestedComparatorSource));
    topDocs = searcher.search(new TermQuery(new Term("__type", "parent")), 5, sort);
    assertThat(topDocs.totalHits, equalTo(8));
    assertThat(topDocs.scoreDocs.length, equalTo(5));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(19));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(-127));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(24));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(-127));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(15));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(28));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(11));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(7));
    // Moved to method, because floating point based XFieldComparatorSource have different outcome for SortMode avg,
    // than integral number based implementations...
    assertAvgScoreMode(parentFilter, searcher);
    searcher.getIndexReader().close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) TermQuery(org.apache.lucene.search.TermQuery) ToParentBlockJoinQuery(org.apache.lucene.search.join.ToParentBlockJoinQuery) ElasticsearchDirectoryReader(org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader) DirectoryReader(org.apache.lucene.index.DirectoryReader) ArrayList(java.util.ArrayList) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) SortField(org.apache.lucene.search.SortField) Term(org.apache.lucene.index.Term) XFieldComparatorSource(org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource) Document(org.apache.lucene.document.Document) MultiValueMode(org.elasticsearch.search.MultiValueMode) ShardId(org.elasticsearch.index.shard.ShardId) ToParentBlockJoinQuery(org.apache.lucene.search.join.ToParentBlockJoinQuery) StringField(org.apache.lucene.document.StringField) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) QueryBitSetProducer(org.apache.lucene.search.join.QueryBitSetProducer) Sort(org.apache.lucene.search.Sort)

Example 27 with TopFieldDocs

use of org.apache.lucene.search.TopFieldDocs in project elasticsearch by elastic.

the class InternalTopHitsTests method createTestInstance.

@Override
protected InternalTopHits createTestInstance(String name, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
    int from = 0;
    int requestedSize = between(1, 40);
    int actualSize = between(0, requestedSize);
    float maxScore = Float.MIN_VALUE;
    ScoreDoc[] scoreDocs = new ScoreDoc[actualSize];
    SearchHit[] hits = new SearchHit[actualSize];
    Set<Integer> usedDocIds = new HashSet<>();
    for (int i = 0; i < actualSize; i++) {
        float score = randomFloat();
        maxScore = max(maxScore, score);
        int docId = randomValueOtherThanMany(usedDocIds::contains, () -> between(0, IndexWriter.MAX_DOCS));
        usedDocIds.add(docId);
        Map<String, SearchHitField> searchHitFields = new HashMap<>();
        if (testInstancesLookSortedByField) {
            Object[] fields = new Object[testInstancesSortFields.length];
            for (int f = 0; f < testInstancesSortFields.length; f++) {
                fields[f] = randomOfType(testInstancesSortFields[f].getType());
            }
            scoreDocs[i] = new FieldDoc(docId, score, fields);
        } else {
            scoreDocs[i] = new ScoreDoc(docId, score);
        }
        hits[i] = new SearchHit(docId, Integer.toString(i), new Text("test"), searchHitFields);
        hits[i].score(score);
    }
    int totalHits = between(actualSize, 500000);
    SearchHits searchHits = new SearchHits(hits, totalHits, maxScore);
    TopDocs topDocs;
    Arrays.sort(scoreDocs, scoreDocComparator());
    if (testInstancesLookSortedByField) {
        topDocs = new TopFieldDocs(totalHits, scoreDocs, testInstancesSortFields, maxScore);
    } else {
        topDocs = new TopDocs(totalHits, scoreDocs, maxScore);
    }
    return new InternalTopHits(name, from, requestedSize, topDocs, searchHits, pipelineAggregators, metaData);
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) FieldDoc(org.apache.lucene.search.FieldDoc) HashMap(java.util.HashMap) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) Text(org.elasticsearch.common.text.Text) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) SearchHitField(org.elasticsearch.search.SearchHitField) SearchHits(org.elasticsearch.search.SearchHits) HashSet(java.util.HashSet)

Example 28 with TopFieldDocs

use of org.apache.lucene.search.TopFieldDocs in project elasticsearch by elastic.

the class SimpleLuceneTests method testSortValues.

public void testSortValues() throws Exception {
    Directory dir = new RAMDirectory();
    IndexWriter indexWriter = new IndexWriter(dir, new IndexWriterConfig(Lucene.STANDARD_ANALYZER));
    for (int i = 0; i < 10; i++) {
        Document document = new Document();
        String text = new String(new char[] { (char) (97 + i), (char) (97 + i) });
        document.add(new TextField("str", text, Field.Store.YES));
        document.add(new SortedDocValuesField("str", new BytesRef(text)));
        indexWriter.addDocument(document);
    }
    IndexReader reader = SlowCompositeReaderWrapper.wrap(DirectoryReader.open(indexWriter));
    IndexSearcher searcher = new IndexSearcher(reader);
    TopFieldDocs docs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(new SortField("str", SortField.Type.STRING)));
    for (int i = 0; i < 10; i++) {
        FieldDoc fieldDoc = (FieldDoc) docs.scoreDocs[i];
        assertThat((BytesRef) fieldDoc.fields[0], equalTo(new BytesRef(new String(new char[] { (char) (97 + i), (char) (97 + i) }))));
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) FieldDoc(org.apache.lucene.search.FieldDoc) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) SortField(org.apache.lucene.search.SortField) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RAMDirectory(org.apache.lucene.store.RAMDirectory) IndexWriter(org.apache.lucene.index.IndexWriter) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) IndexReader(org.apache.lucene.index.IndexReader) TextField(org.apache.lucene.document.TextField) Sort(org.apache.lucene.search.Sort) BytesRef(org.apache.lucene.util.BytesRef) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 29 with TopFieldDocs

use of org.apache.lucene.search.TopFieldDocs in project elasticsearch by elastic.

the class AbstractFieldDataImplTestCase method testMultiValueAllSet.

public void testMultiValueAllSet() throws Exception {
    fillMultiValueAllSet();
    // the segments are force merged to a single segment so that the sorted binary doc values can be asserted within a single segment.
    // Previously we used the SlowCompositeReaderWrapper but this is an unideal solution so force merging is a better idea.
    writer.forceMerge(1);
    IndexFieldData indexFieldData = getForField("value");
    List<LeafReaderContext> readerContexts = refreshReader();
    for (LeafReaderContext readerContext : readerContexts) {
        AtomicFieldData fieldData = indexFieldData.load(readerContext);
        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));
        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));
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Example 30 with TopFieldDocs

use of org.apache.lucene.search.TopFieldDocs in project elasticsearch by elastic.

the class AbstractFieldDataImplTestCase method testSingleValueAllSet.

public void testSingleValueAllSet() throws Exception {
    fillSingleValueAllSet();
    IndexFieldData indexFieldData = getForField("value");
    List<LeafReaderContext> readerContexts = refreshReader();
    for (LeafReaderContext readerContext : readerContexts) {
        AtomicFieldData fieldData = indexFieldData.load(readerContext);
        assertThat(fieldData.ramBytesUsed(), greaterThanOrEqualTo(minRamBytesUsed()));
        SortedBinaryDocValues bytesValues = fieldData.getBytesValues();
        bytesValues.setDocument(0);
        assertThat(bytesValues.count(), equalTo(1));
        assertThat(bytesValues.valueAt(0), equalTo(new BytesRef(two())));
        bytesValues.setDocument(1);
        assertThat(bytesValues.count(), equalTo(1));
        assertThat(bytesValues.valueAt(0), equalTo(new BytesRef(one())));
        bytesValues.setDocument(2);
        assertThat(bytesValues.count(), equalTo(1));
        assertThat(bytesValues.valueAt(0), equalTo(new BytesRef(three())));
        assertValues(bytesValues, 0, two());
        assertValues(bytesValues, 1, one());
        assertValues(bytesValues, 2, three());
        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(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, 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));
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

TopFieldDocs (org.apache.lucene.search.TopFieldDocs)41 Sort (org.apache.lucene.search.Sort)30 SortField (org.apache.lucene.search.SortField)24 FieldDoc (org.apache.lucene.search.FieldDoc)23 IndexSearcher (org.apache.lucene.search.IndexSearcher)19 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)18 TermQuery (org.apache.lucene.search.TermQuery)15 Document (org.apache.lucene.document.Document)14 ScoreDoc (org.apache.lucene.search.ScoreDoc)12 ArrayList (java.util.ArrayList)11 Term (org.apache.lucene.index.Term)11 Query (org.apache.lucene.search.Query)11 BytesRef (org.apache.lucene.util.BytesRef)9 TopDocs (org.apache.lucene.search.TopDocs)8 Directory (org.apache.lucene.store.Directory)8 HashMap (java.util.HashMap)6 StringField (org.apache.lucene.document.StringField)6 CollapseTopFieldDocs (org.apache.lucene.search.grouping.CollapseTopFieldDocs)5 IOException (java.io.IOException)4 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)4