Search in sources :

Example 1 with SortedNumericSortField

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

the class CollapsingTopDocsCollectorTests method testCollapseDouble.

public void testCollapseDouble() throws Exception {
    CollapsingDocValuesProducer producer = new CollapsingDocValuesProducer<Double>() {

        @Override
        public Double randomGroup(int maxGroup) {
            return new Double(randomIntBetween(0, maxGroup - 1));
        }

        @Override
        public void add(Document doc, Double value, boolean multivalued) {
            if (multivalued) {
                doc.add(new SortedNumericDocValuesField("field", NumericUtils.doubleToSortableLong(value)));
            } else {
                doc.add(new NumericDocValuesField("field", Double.doubleToLongBits(value)));
            }
        }

        @Override
        public SortField sortField(boolean multivalued) {
            if (multivalued) {
                return new SortedNumericSortField("field", SortField.Type.DOUBLE);
            } else {
                return new SortField("field", SortField.Type.DOUBLE);
            }
        }
    };
    assertSearchCollapse(producer, true);
}
Also used : SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) SortField(org.apache.lucene.search.SortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) Document(org.apache.lucene.document.Document)

Example 2 with SortedNumericSortField

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

the class CollapsingTopDocsCollectorTests method testCollapseInt.

public void testCollapseInt() throws Exception {
    CollapsingDocValuesProducer producer = new CollapsingDocValuesProducer<Integer>() {

        @Override
        public Integer randomGroup(int maxGroup) {
            return randomIntBetween(0, maxGroup - 1);
        }

        @Override
        public void add(Document doc, Integer value, boolean multivalued) {
            if (multivalued) {
                doc.add(new SortedNumericDocValuesField("field", value));
            } else {
                doc.add(new NumericDocValuesField("field", value));
            }
        }

        @Override
        public SortField sortField(boolean multivalued) {
            if (multivalued) {
                return new SortedNumericSortField("field", SortField.Type.INT);
            } else {
                return new SortField("field", SortField.Type.INT);
            }
        }
    };
    assertSearchCollapse(producer, true);
}
Also used : SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) SortField(org.apache.lucene.search.SortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) Document(org.apache.lucene.document.Document)

Example 3 with SortedNumericSortField

use of org.apache.lucene.search.SortedNumericSortField in project neo4j by neo4j.

the class QueryContext method sortNumeric.

/**
     * Sort the results of a numeric range query if the query in this context
     * is a {@link NumericRangeQuery}, see {@link #numericRange(String, Number, Number)},
     * Otherwise an {@link IllegalStateException} will be thrown.
     *
     * @param key the key to sort on.
     * @param reversed if the sort order should be reversed or not. {@code true}
     * for lowest first (ascending), {@code false} for highest first (descending)
     * @return a QueryContext with sorting by numeric value.
     */
public QueryContext sortNumeric(String key, boolean reversed) {
    if (!(queryOrQueryObject instanceof NumericRangeQuery)) {
        throw new IllegalStateException("Not a numeric range query");
    }
    Number number = ((NumericRangeQuery) queryOrQueryObject).getMin();
    number = number != null ? number : ((NumericRangeQuery) queryOrQueryObject).getMax();
    SortField.Type fieldType = SortField.Type.INT;
    if (number instanceof Long) {
        fieldType = SortField.Type.LONG;
    } else if (number instanceof Float) {
        fieldType = SortField.Type.FLOAT;
    } else if (number instanceof Double) {
        fieldType = SortField.Type.DOUBLE;
    }
    sort(new Sort(new SortedNumericSortField(key, fieldType, reversed)));
    return this;
}
Also used : SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) NumericRangeQuery(org.apache.lucene.search.NumericRangeQuery) Sort(org.apache.lucene.search.Sort) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortField(org.apache.lucene.search.SortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField)

Example 4 with SortedNumericSortField

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

the class BaseSegmentInfoFormatTestCase method randomIndexSortField.

private SortField randomIndexSortField() {
    boolean reversed = random().nextBoolean();
    SortField sortField;
    switch(random().nextInt(10)) {
        case 0:
            sortField = new SortField(TestUtil.randomSimpleString(random()), SortField.Type.INT, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextInt());
            }
            break;
        case 1:
            sortField = new SortedNumericSortField(TestUtil.randomSimpleString(random()), SortField.Type.INT, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextInt());
            }
            break;
        case 2:
            sortField = new SortField(TestUtil.randomSimpleString(random()), SortField.Type.LONG, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextLong());
            }
            break;
        case 3:
            sortField = new SortedNumericSortField(TestUtil.randomSimpleString(random()), SortField.Type.LONG, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextLong());
            }
            break;
        case 4:
            sortField = new SortField(TestUtil.randomSimpleString(random()), SortField.Type.FLOAT, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextFloat());
            }
            break;
        case 5:
            sortField = new SortedNumericSortField(TestUtil.randomSimpleString(random()), SortField.Type.FLOAT, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextFloat());
            }
            break;
        case 6:
            sortField = new SortField(TestUtil.randomSimpleString(random()), SortField.Type.DOUBLE, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextDouble());
            }
            break;
        case 7:
            sortField = new SortedNumericSortField(TestUtil.randomSimpleString(random()), SortField.Type.DOUBLE, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextDouble());
            }
            break;
        case 8:
            sortField = new SortField(TestUtil.randomSimpleString(random()), SortField.Type.STRING, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(SortField.STRING_LAST);
            }
            break;
        case 9:
            sortField = new SortedSetSortField(TestUtil.randomSimpleString(random()), 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) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortField(org.apache.lucene.search.SortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField)

Example 5 with SortedNumericSortField

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

the class TestIndexSorting method testMissingMultiValuedIntFirst.

public void testMissingMultiValuedIntFirst() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
    SortField sortField = new SortedNumericSortField("foo", SortField.Type.INT);
    sortField.setMissingValue(Integer.valueOf(Integer.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", 187667));
    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", 34));
    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