Search in sources :

Example 31 with FieldDoc

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

the class SearchAfterBuilder method buildFieldDoc.

public static FieldDoc buildFieldDoc(SortAndFormats sort, Object[] values) {
    if (sort == null || sort.sort.getSort() == null || sort.sort.getSort().length == 0) {
        throw new IllegalArgumentException("Sort must contain at least one field.");
    }
    SortField[] sortFields = sort.sort.getSort();
    if (sortFields.length != values.length) {
        throw new IllegalArgumentException(SEARCH_AFTER.getPreferredName() + " has " + values.length + " value(s) but sort has " + sort.sort.getSort().length + ".");
    }
    Object[] fieldValues = new Object[sortFields.length];
    for (int i = 0; i < sortFields.length; i++) {
        SortField sortField = sortFields[i];
        DocValueFormat format = sort.formats[i];
        if (values[i] != null) {
            fieldValues[i] = convertValueFromSortField(values[i], sortField, format);
        } else {
            fieldValues[i] = null;
        }
    }
    /*
         * We set the doc id to Integer.MAX_VALUE in order to make sure that the search starts "after" the first document that is equal to
         * the field values.
         */
    return new FieldDoc(Integer.MAX_VALUE, 0, fieldValues);
}
Also used : FieldDoc(org.apache.lucene.search.FieldDoc) DocValueFormat(org.elasticsearch.search.DocValueFormat) SortField(org.apache.lucene.search.SortField)

Example 32 with FieldDoc

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

the class Lucene method writeTopDocs.

public static void writeTopDocs(StreamOutput out, TopDocs topDocs) throws IOException {
    if (topDocs instanceof CollapseTopFieldDocs) {
        out.writeByte((byte) 2);
        CollapseTopFieldDocs collapseDocs = (CollapseTopFieldDocs) topDocs;
        out.writeVInt(topDocs.totalHits);
        out.writeFloat(topDocs.getMaxScore());
        out.writeString(collapseDocs.field);
        out.writeVInt(collapseDocs.fields.length);
        for (SortField sortField : collapseDocs.fields) {
            writeSortField(out, sortField);
        }
        out.writeVInt(topDocs.scoreDocs.length);
        for (int i = 0; i < topDocs.scoreDocs.length; i++) {
            ScoreDoc doc = collapseDocs.scoreDocs[i];
            writeFieldDoc(out, (FieldDoc) doc);
            writeSortValue(out, collapseDocs.collapseValues[i]);
        }
    } else if (topDocs instanceof TopFieldDocs) {
        out.writeByte((byte) 1);
        TopFieldDocs topFieldDocs = (TopFieldDocs) topDocs;
        out.writeVInt(topDocs.totalHits);
        out.writeFloat(topDocs.getMaxScore());
        out.writeVInt(topFieldDocs.fields.length);
        for (SortField sortField : topFieldDocs.fields) {
            writeSortField(out, sortField);
        }
        out.writeVInt(topDocs.scoreDocs.length);
        for (ScoreDoc doc : topFieldDocs.scoreDocs) {
            writeFieldDoc(out, (FieldDoc) doc);
        }
    } else {
        out.writeByte((byte) 0);
        out.writeVInt(topDocs.totalHits);
        out.writeFloat(topDocs.getMaxScore());
        out.writeVInt(topDocs.scoreDocs.length);
        for (ScoreDoc doc : topDocs.scoreDocs) {
            writeScoreDoc(out, doc);
        }
    }
}
Also used : FieldDoc(org.apache.lucene.search.FieldDoc) CollapseTopFieldDocs(org.apache.lucene.search.grouping.CollapseTopFieldDocs) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) SortField(org.apache.lucene.search.SortField) CollapseTopFieldDocs(org.apache.lucene.search.grouping.CollapseTopFieldDocs) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 33 with FieldDoc

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

the class Lucene method readTopDocs.

public static TopDocs readTopDocs(StreamInput in) throws IOException {
    byte type = in.readByte();
    if (type == 0) {
        int totalHits = in.readVInt();
        float maxScore = in.readFloat();
        ScoreDoc[] scoreDocs = new ScoreDoc[in.readVInt()];
        for (int i = 0; i < scoreDocs.length; i++) {
            scoreDocs[i] = new ScoreDoc(in.readVInt(), in.readFloat());
        }
        return new TopDocs(totalHits, scoreDocs, maxScore);
    } else if (type == 1) {
        int totalHits = in.readVInt();
        float maxScore = in.readFloat();
        SortField[] fields = new SortField[in.readVInt()];
        for (int i = 0; i < fields.length; i++) {
            fields[i] = readSortField(in);
        }
        FieldDoc[] fieldDocs = new FieldDoc[in.readVInt()];
        for (int i = 0; i < fieldDocs.length; i++) {
            fieldDocs[i] = readFieldDoc(in);
        }
        return new TopFieldDocs(totalHits, fieldDocs, fields, maxScore);
    } else if (type == 2) {
        int totalHits = in.readVInt();
        float maxScore = in.readFloat();
        String field = in.readString();
        SortField[] fields = new SortField[in.readVInt()];
        for (int i = 0; i < fields.length; i++) {
            fields[i] = readSortField(in);
        }
        int size = in.readVInt();
        Object[] collapseValues = new Object[size];
        FieldDoc[] fieldDocs = new FieldDoc[size];
        for (int i = 0; i < fieldDocs.length; i++) {
            fieldDocs[i] = readFieldDoc(in);
            collapseValues[i] = readSortValue(in);
        }
        return new CollapseTopFieldDocs(field, totalHits, fieldDocs, fields, collapseValues, maxScore);
    } else {
        throw new IllegalStateException("Unknown type " + type);
    }
}
Also used : FieldDoc(org.apache.lucene.search.FieldDoc) CollapseTopFieldDocs(org.apache.lucene.search.grouping.CollapseTopFieldDocs) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) SortField(org.apache.lucene.search.SortField) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) CollapseTopFieldDocs(org.apache.lucene.search.grouping.CollapseTopFieldDocs)

Example 34 with FieldDoc

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

the class ParentChildFieldDataTests method testSorting.

public void testSorting() throws Exception {
    IndexFieldData indexFieldData = getForField(parentType);
    IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(writer));
    IndexFieldData.XFieldComparatorSource comparator = indexFieldData.comparatorSource("_last", MultiValueMode.MIN, null);
    TopFieldDocs topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(new SortField(ParentFieldMapper.joinField(parentType), comparator, false)));
    assertThat(topDocs.totalHits, equalTo(8));
    assertThat(topDocs.scoreDocs.length, equalTo(8));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(0));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).utf8ToString(), equalTo("1"));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(1));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).utf8ToString(), equalTo("1"));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(2));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).utf8ToString(), equalTo("1"));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(5));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).utf8ToString(), equalTo("1"));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(3));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).utf8ToString(), equalTo("2"));
    assertThat(topDocs.scoreDocs[5].doc, equalTo(4));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[5]).fields[0]).utf8ToString(), equalTo("2"));
    assertThat(topDocs.scoreDocs[6].doc, equalTo(6));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[6]).fields[0]).utf8ToString(), equalTo("2"));
    assertThat(topDocs.scoreDocs[7].doc, equalTo(7));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[7]).fields[0]), equalTo(null));
    topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(new SortField(ParentFieldMapper.joinField(parentType), comparator, true)));
    assertThat(topDocs.totalHits, equalTo(8));
    assertThat(topDocs.scoreDocs.length, equalTo(8));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(3));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).utf8ToString(), equalTo("2"));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(4));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).utf8ToString(), equalTo("2"));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(6));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).utf8ToString(), equalTo("2"));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(0));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).utf8ToString(), equalTo("1"));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(1));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).utf8ToString(), equalTo("1"));
    assertThat(topDocs.scoreDocs[5].doc, equalTo(2));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[5]).fields[0]).utf8ToString(), equalTo("1"));
    assertThat(topDocs.scoreDocs[6].doc, equalTo(5));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[6]).fields[0]).utf8ToString(), equalTo("1"));
    assertThat(topDocs.scoreDocs[7].doc, equalTo(7));
    assertThat(((FieldDoc) topDocs.scoreDocs[7]).fields[0], nullValue());
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) FieldDoc(org.apache.lucene.search.FieldDoc) ParentChildIndexFieldData(org.elasticsearch.index.fielddata.plain.ParentChildIndexFieldData) 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)

Example 35 with FieldDoc

use of org.apache.lucene.search.FieldDoc 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)

Aggregations

FieldDoc (org.apache.lucene.search.FieldDoc)50 Sort (org.apache.lucene.search.Sort)26 TopFieldDocs (org.apache.lucene.search.TopFieldDocs)23 SortField (org.apache.lucene.search.SortField)21 ScoreDoc (org.apache.lucene.search.ScoreDoc)20 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)18 IndexSearcher (org.apache.lucene.search.IndexSearcher)17 ArrayList (java.util.ArrayList)14 Directory (org.apache.lucene.store.Directory)14 TopDocs (org.apache.lucene.search.TopDocs)12 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)10 TermQuery (org.apache.lucene.search.TermQuery)10 Document (org.apache.lucene.document.Document)9 Term (org.apache.lucene.index.Term)9 BytesRef (org.apache.lucene.util.BytesRef)9 HashMap (java.util.HashMap)8 Query (org.apache.lucene.search.Query)8 IndexReader (org.apache.lucene.index.IndexReader)7 HashSet (java.util.HashSet)5 DirectoryReader (org.apache.lucene.index.DirectoryReader)5