Search in sources :

Example 6 with SortedNumericSortField

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

the class TestIndexSorting method testMissingMultiValuedFloatFirst.

public void testMissingMultiValuedFloatFirst() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
    SortField sortField = new SortedNumericSortField("foo", SortField.Type.FLOAT);
    sortField.setMissingValue(Float.NEGATIVE_INFINITY);
    Sort indexSort = new Sort(sortField);
    iwc.setIndexSort(indexSort);
    IndexWriter w = new IndexWriter(dir, iwc);
    Document doc = new Document();
    doc.add(new NumericDocValuesField("id", 3));
    doc.add(new SortedNumericDocValuesField("foo", NumericUtils.floatToSortableInt(18.0f)));
    doc.add(new SortedNumericDocValuesField("foo", NumericUtils.floatToSortableInt(726.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", 1));
    w.addDocument(doc);
    w.commit();
    doc = new Document();
    doc.add(new NumericDocValuesField("id", 2));
    doc.add(new SortedNumericDocValuesField("foo", NumericUtils.floatToSortableInt(7.0f)));
    doc.add(new SortedNumericDocValuesField("foo", NumericUtils.floatToSortableInt(18.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 7 with SortedNumericSortField

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

the class TestIndexSorting method testMissingMultiValuedLongFirst.

public void testMissingMultiValuedLongFirst() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
    SortField sortField = new SortedNumericSortField("foo", SortField.Type.LONG);
    sortField.setMissingValue(Long.valueOf(Long.MIN_VALUE));
    Sort indexSort = new Sort(sortField);
    iwc.setIndexSort(indexSort);
    IndexWriter w = new IndexWriter(dir, iwc);
    Document doc = new Document();
    doc.add(new NumericDocValuesField("id", 3));
    doc.add(new SortedNumericDocValuesField("foo", 18));
    doc.add(new SortedNumericDocValuesField("foo", 27));
    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", 1));
    w.addDocument(doc);
    w.commit();
    doc = new Document();
    doc.add(new NumericDocValuesField("id", 2));
    doc.add(new SortedNumericDocValuesField("foo", 7));
    doc.add(new SortedNumericDocValuesField("foo", 24));
    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 8 with SortedNumericSortField

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

the class TestIndexSorting method testBasicMultiValuedDouble.

public void testBasicMultiValuedDouble() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
    Sort indexSort = new Sort(new SortedNumericSortField("foo", SortField.Type.DOUBLE));
    iwc.setIndexSort(indexSort);
    IndexWriter w = new IndexWriter(dir, iwc);
    Document doc = new Document();
    doc.add(new NumericDocValuesField("id", 3));
    doc.add(new SortedNumericDocValuesField("foo", NumericUtils.doubleToSortableLong(7.54)));
    doc.add(new SortedNumericDocValuesField("foo", NumericUtils.doubleToSortableLong(27.0)));
    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();
    doc = new Document();
    doc.add(new NumericDocValuesField("id", 1));
    doc.add(new SortedNumericDocValuesField("foo", NumericUtils.doubleToSortableLong(-1.0)));
    doc.add(new SortedNumericDocValuesField("foo", NumericUtils.doubleToSortableLong(0.0)));
    w.addDocument(doc);
    w.commit();
    doc = new Document();
    doc.add(new NumericDocValuesField("id", 2));
    doc.add(new SortedNumericDocValuesField("foo", NumericUtils.doubleToSortableLong(7.0)));
    doc.add(new SortedNumericDocValuesField("foo", NumericUtils.doubleToSortableLong(7.67)));
    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 : 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) Document(org.apache.lucene.document.Document) Directory(org.apache.lucene.store.Directory)

Example 9 with SortedNumericSortField

use of org.apache.lucene.search.SortedNumericSortField 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 10 with SortedNumericSortField

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

Aggregations

SortedNumericSortField (org.apache.lucene.search.SortedNumericSortField)33 SortField (org.apache.lucene.search.SortField)25 Sort (org.apache.lucene.search.Sort)24 SortedSetSortField (org.apache.lucene.search.SortedSetSortField)22 Document (org.apache.lucene.document.Document)18 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)18 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)18 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)13 Directory (org.apache.lucene.store.Directory)13 Version (org.apache.lucene.util.Version)5 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)3 SegmentInfo (org.apache.lucene.index.SegmentInfo)3 SortedNumericSelector (org.apache.lucene.search.SortedNumericSelector)3 SortedSetSelector (org.apache.lucene.search.SortedSetSelector)3 ChecksumIndexInput (org.apache.lucene.store.ChecksumIndexInput)3 OrderBy (io.crate.analyze.OrderBy)2 LongType (io.crate.types.LongType)2 IndexSearcher (org.apache.lucene.search.IndexSearcher)2 IndexOutput (org.apache.lucene.store.IndexOutput)2 Path (com.querydsl.core.types.Path)1