Search in sources :

Example 71 with SortField

use of org.apache.lucene.search.SortField in project lucene-solr by apache.

the class TestIndexSorting method randomIndexSortField.

private static SortField randomIndexSortField() {
    boolean reversed = random().nextBoolean();
    SortField sortField;
    switch(random().nextInt(10)) {
        case 0:
            sortField = new SortField("int", SortField.Type.INT, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextInt());
            }
            break;
        case 1:
            sortField = new SortedNumericSortField("multi_valued_int", SortField.Type.INT, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextInt());
            }
            break;
        case 2:
            sortField = new SortField("long", SortField.Type.LONG, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextLong());
            }
            break;
        case 3:
            sortField = new SortedNumericSortField("multi_valued_long", SortField.Type.LONG, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextLong());
            }
            break;
        case 4:
            sortField = new SortField("float", SortField.Type.FLOAT, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextFloat());
            }
            break;
        case 5:
            sortField = new SortedNumericSortField("multi_valued_float", SortField.Type.FLOAT, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextFloat());
            }
            break;
        case 6:
            sortField = new SortField("double", SortField.Type.DOUBLE, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextDouble());
            }
            break;
        case 7:
            sortField = new SortedNumericSortField("multi_valued_double", SortField.Type.DOUBLE, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextDouble());
            }
            break;
        case 8:
            sortField = new SortField("bytes", SortField.Type.STRING, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(SortField.STRING_LAST);
            }
            break;
        case 9:
            sortField = new SortedSetSortField("multi_valued_bytes", reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(SortField.STRING_LAST);
            }
            break;
        default:
            sortField = null;
            fail();
    }
    return sortField;
}
Also used : SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortField(org.apache.lucene.search.SortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField)

Example 72 with SortField

use of org.apache.lucene.search.SortField in project lucene-solr by apache.

the class TestIndexSorting method testMissingMultiValuedFloatLast.

public void testMissingMultiValuedFloatLast() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
    SortField sortField = new SortedNumericSortField("foo", SortField.Type.FLOAT);
    sortField.setMissingValue(Float.POSITIVE_INFINITY);
    Sort indexSort = new Sort(sortField);
    iwc.setIndexSort(indexSort);
    IndexWriter w = new IndexWriter(dir, iwc);
    Document doc = new Document();
    doc.add(new NumericDocValuesField("id", 2));
    doc.add(new SortedNumericDocValuesField("foo", NumericUtils.floatToSortableInt(726.0f)));
    doc.add(new SortedNumericDocValuesField("foo", NumericUtils.floatToSortableInt(18.0f)));
    w.addDocument(doc);
    // so we get more than one segment, so that forceMerge actually does merge, since we only get a sorted segment by merging:
    w.commit();
    // missing
    doc = new Document();
    doc.add(new NumericDocValuesField("id", 3));
    w.addDocument(doc);
    w.commit();
    doc = new Document();
    doc.add(new NumericDocValuesField("id", 1));
    doc.add(new SortedNumericDocValuesField("foo", NumericUtils.floatToSortableInt(12.67f)));
    doc.add(new SortedNumericDocValuesField("foo", NumericUtils.floatToSortableInt(7.0f)));
    w.addDocument(doc);
    w.forceMerge(1);
    DirectoryReader r = DirectoryReader.open(w);
    LeafReader leaf = getOnlyLeafReader(r);
    assertEquals(3, leaf.maxDoc());
    NumericDocValues values = leaf.getNumericDocValues("id");
    assertEquals(0, values.nextDoc());
    assertEquals(1, values.longValue());
    assertEquals(1, values.nextDoc());
    assertEquals(2, values.longValue());
    assertEquals(2, values.nextDoc());
    assertEquals(3, values.longValue());
    r.close();
    w.close();
    dir.close();
}
Also used : SortField(org.apache.lucene.search.SortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) Document(org.apache.lucene.document.Document) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) Sort(org.apache.lucene.search.Sort) Directory(org.apache.lucene.store.Directory)

Example 73 with SortField

use of org.apache.lucene.search.SortField in project lucene-solr by apache.

the class TestIndexSorting method testAddIndexes.

public void testAddIndexes(boolean withDeletes, boolean useReaders) throws Exception {
    Directory dir = newDirectory();
    Sort indexSort = new Sort(new SortField("foo", SortField.Type.LONG));
    IndexWriterConfig iwc1 = newIndexWriterConfig();
    if (random().nextBoolean()) {
        iwc1.setIndexSort(indexSort);
    }
    RandomIndexWriter w = new RandomIndexWriter(random(), dir);
    final int numDocs = atLeast(100);
    for (int i = 0; i < numDocs; ++i) {
        Document doc = new Document();
        doc.add(new StringField("id", Integer.toString(i), Store.NO));
        doc.add(new NumericDocValuesField("foo", random().nextInt(20)));
        w.addDocument(doc);
    }
    if (withDeletes) {
        for (int i = random().nextInt(5); i < numDocs; i += TestUtil.nextInt(random(), 1, 5)) {
            w.deleteDocuments(new Term("id", Integer.toString(i)));
        }
    }
    if (random().nextBoolean()) {
        w.forceMerge(1);
    }
    final IndexReader reader = w.getReader();
    w.close();
    Directory dir2 = newDirectory();
    IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
    iwc.setIndexSort(indexSort);
    IndexWriter w2 = new IndexWriter(dir2, iwc);
    if (useReaders) {
        CodecReader[] codecReaders = new CodecReader[reader.leaves().size()];
        for (int i = 0; i < codecReaders.length; ++i) {
            codecReaders[i] = (CodecReader) reader.leaves().get(i).reader();
        }
        w2.addIndexes(codecReaders);
    } else {
        w2.addIndexes(dir);
    }
    final IndexReader reader2 = w2.getReader();
    final IndexSearcher searcher = newSearcher(reader);
    final IndexSearcher searcher2 = newSearcher(reader2);
    for (int i = 0; i < numDocs; ++i) {
        Query query = new TermQuery(new Term("id", Integer.toString(i)));
        final TopDocs topDocs = searcher.search(query, 1);
        final TopDocs topDocs2 = searcher2.search(query, 1);
        assertEquals(topDocs.totalHits, topDocs2.totalHits);
        if (topDocs.totalHits == 1) {
            NumericDocValues dvs1 = MultiDocValues.getNumericValues(reader, "foo");
            int hitDoc1 = topDocs.scoreDocs[0].doc;
            assertEquals(hitDoc1, dvs1.advance(hitDoc1));
            long value1 = dvs1.longValue();
            NumericDocValues dvs2 = MultiDocValues.getNumericValues(reader2, "foo");
            int hitDoc2 = topDocs2.scoreDocs[0].doc;
            assertEquals(hitDoc2, dvs2.advance(hitDoc2));
            long value2 = dvs2.longValue();
            assertEquals(value1, value2);
        }
    }
    IOUtils.close(reader, reader2, w2, dir, dir2);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) SortField(org.apache.lucene.search.SortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) Document(org.apache.lucene.document.Document) BinaryPoint(org.apache.lucene.document.BinaryPoint) IntPoint(org.apache.lucene.document.IntPoint) TopDocs(org.apache.lucene.search.TopDocs) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) StringField(org.apache.lucene.document.StringField) Sort(org.apache.lucene.search.Sort) Directory(org.apache.lucene.store.Directory)

Example 74 with SortField

use of org.apache.lucene.search.SortField in project lucene-solr by apache.

the class TestIndexSorting method randomSort.

private static Sort randomSort() {
    // at least 2
    int numFields = TestUtil.nextInt(random(), 2, 4);
    SortField[] sortFields = new SortField[numFields];
    for (int i = 0; i < numFields - 1; i++) {
        SortField sortField = randomIndexSortField();
        sortFields[i] = sortField;
    }
    // tie-break by id:
    sortFields[numFields - 1] = new SortField("id", SortField.Type.INT);
    return new Sort(sortFields);
}
Also used : Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) BinaryPoint(org.apache.lucene.document.BinaryPoint) IntPoint(org.apache.lucene.document.IntPoint)

Example 75 with SortField

use of org.apache.lucene.search.SortField in project lucene-solr by apache.

the class TestIndexSorting method testRandom1.

public void testRandom1() throws IOException {
    boolean withDeletes = random().nextBoolean();
    Directory dir = newDirectory();
    IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
    Sort indexSort = new Sort(new SortField("foo", SortField.Type.LONG));
    iwc.setIndexSort(indexSort);
    IndexWriter w = new IndexWriter(dir, iwc);
    final int numDocs = atLeast(1000);
    final FixedBitSet deleted = new FixedBitSet(numDocs);
    for (int i = 0; i < numDocs; ++i) {
        Document doc = new Document();
        doc.add(new NumericDocValuesField("foo", random().nextInt(20)));
        doc.add(new StringField("id", Integer.toString(i), Store.YES));
        doc.add(new NumericDocValuesField("id", i));
        w.addDocument(doc);
        if (random().nextInt(5) == 0) {
            w.getReader().close();
        } else if (random().nextInt(30) == 0) {
            w.forceMerge(2);
        } else if (random().nextInt(4) == 0) {
            final int id = TestUtil.nextInt(random(), 0, i);
            deleted.set(id);
            w.deleteDocuments(new Term("id", Integer.toString(id)));
        }
    }
    // Check that segments are sorted
    DirectoryReader reader = w.getReader();
    for (LeafReaderContext ctx : reader.leaves()) {
        final SegmentReader leaf = (SegmentReader) ctx.reader();
        SegmentInfo info = leaf.getSegmentInfo().info;
        switch(info.getDiagnostics().get(IndexWriter.SOURCE)) {
            case IndexWriter.SOURCE_FLUSH:
            case IndexWriter.SOURCE_MERGE:
                assertEquals(indexSort, info.getIndexSort());
                final NumericDocValues values = leaf.getNumericDocValues("foo");
                long previous = Long.MIN_VALUE;
                for (int i = 0; i < leaf.maxDoc(); ++i) {
                    assertEquals(i, values.nextDoc());
                    final long value = values.longValue();
                    assertTrue(value >= previous);
                    previous = value;
                }
                break;
            default:
                fail();
        }
    }
    // Now check that the index is consistent
    IndexSearcher searcher = newSearcher(reader);
    for (int i = 0; i < numDocs; ++i) {
        TermQuery termQuery = new TermQuery(new Term("id", Integer.toString(i)));
        final TopDocs topDocs = searcher.search(termQuery, 1);
        if (deleted.get(i)) {
            assertEquals(0, topDocs.totalHits);
        } else {
            assertEquals(1, topDocs.totalHits);
            NumericDocValues values = MultiDocValues.getNumericValues(reader, "id");
            assertEquals(topDocs.scoreDocs[0].doc, values.advance(topDocs.scoreDocs[0].doc));
            assertEquals(i, values.longValue());
            Document document = reader.document(topDocs.scoreDocs[0].doc);
            assertEquals(Integer.toString(i), document.get("id"));
        }
    }
    reader.close();
    w.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) SortField(org.apache.lucene.search.SortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) Document(org.apache.lucene.document.Document) BinaryPoint(org.apache.lucene.document.BinaryPoint) IntPoint(org.apache.lucene.document.IntPoint) TopDocs(org.apache.lucene.search.TopDocs) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) FixedBitSet(org.apache.lucene.util.FixedBitSet) StringField(org.apache.lucene.document.StringField) Sort(org.apache.lucene.search.Sort) Directory(org.apache.lucene.store.Directory)

Aggregations

SortField (org.apache.lucene.search.SortField)231 Sort (org.apache.lucene.search.Sort)175 Document (org.apache.lucene.document.Document)116 Directory (org.apache.lucene.store.Directory)110 IndexSearcher (org.apache.lucene.search.IndexSearcher)90 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)75 TopDocs (org.apache.lucene.search.TopDocs)74 IndexReader (org.apache.lucene.index.IndexReader)65 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)62 SortedNumericSortField (org.apache.lucene.search.SortedNumericSortField)56 SortedSetSortField (org.apache.lucene.search.SortedSetSortField)56 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)49 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)37 TermQuery (org.apache.lucene.search.TermQuery)36 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)32 Query (org.apache.lucene.search.Query)29 Term (org.apache.lucene.index.Term)25 BytesRef (org.apache.lucene.util.BytesRef)25 TopFieldDocs (org.apache.lucene.search.TopFieldDocs)24 StoredField (org.apache.lucene.document.StoredField)23