Search in sources :

Example 6 with SortedNumericDoubleValues

use of org.elasticsearch.index.fielddata.SortedNumericDoubleValues in project elasticsearch by elastic.

the class HistogramAggregator method getLeafCollector.

@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, final LeafBucketCollector sub) throws IOException {
    if (valuesSource == null) {
        return LeafBucketCollector.NO_OP_COLLECTOR;
    }
    final SortedNumericDoubleValues values = valuesSource.doubleValues(ctx);
    return new LeafBucketCollectorBase(sub, values) {

        @Override
        public void collect(int doc, long bucket) throws IOException {
            assert bucket == 0;
            values.setDocument(doc);
            final int valuesCount = values.count();
            double previousKey = Double.NEGATIVE_INFINITY;
            for (int i = 0; i < valuesCount; ++i) {
                double value = values.valueAt(i);
                double key = Math.floor((value - offset) / interval);
                assert key >= previousKey;
                if (key == previousKey) {
                    continue;
                }
                long bucketOrd = bucketOrds.add(Double.doubleToLongBits(key));
                if (bucketOrd < 0) {
                    // already seen
                    bucketOrd = -1 - bucketOrd;
                    collectExistingBucket(sub, doc, bucketOrd);
                } else {
                    collectBucket(sub, doc, bucketOrd);
                }
                previousKey = key;
            }
        }
    };
}
Also used : LeafBucketCollectorBase(org.elasticsearch.search.aggregations.LeafBucketCollectorBase) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues)

Example 7 with SortedNumericDoubleValues

use of org.elasticsearch.index.fielddata.SortedNumericDoubleValues in project elasticsearch by elastic.

the class CountMethodValueSource method getValues.

@Override
// ValueSource uses a rawtype
@SuppressWarnings("rawtypes")
public FunctionValues getValues(Map context, LeafReaderContext leaf) throws IOException {
    AtomicNumericFieldData leafData = (AtomicNumericFieldData) fieldData.load(leaf);
    final SortedNumericDoubleValues values = leafData.getDoubleValues();
    return new DoubleDocValues(this) {

        @Override
        public double doubleVal(int doc) {
            values.setDocument(doc);
            return values.count();
        }
    };
}
Also used : DoubleDocValues(org.apache.lucene.queries.function.docvalues.DoubleDocValues) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues) AtomicNumericFieldData(org.elasticsearch.index.fielddata.AtomicNumericFieldData)

Example 8 with SortedNumericDoubleValues

use of org.elasticsearch.index.fielddata.SortedNumericDoubleValues in project elasticsearch by elastic.

the class EmptyMemberValueSource method getValues.

@Override
// ValueSource uses a rawtype
@SuppressWarnings("rawtypes")
public FunctionValues getValues(Map context, LeafReaderContext leaf) throws IOException {
    AtomicNumericFieldData leafData = (AtomicNumericFieldData) fieldData.load(leaf);
    final SortedNumericDoubleValues values = leafData.getDoubleValues();
    return new DoubleDocValues(this) {

        @Override
        public double doubleVal(int doc) {
            values.setDocument(doc);
            if (values.count() == 0) {
                return 1;
            } else {
                return 0;
            }
        }
    };
}
Also used : DoubleDocValues(org.apache.lucene.queries.function.docvalues.DoubleDocValues) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues) AtomicNumericFieldData(org.elasticsearch.index.fielddata.AtomicNumericFieldData)

Example 9 with SortedNumericDoubleValues

use of org.elasticsearch.index.fielddata.SortedNumericDoubleValues in project elasticsearch by elastic.

the class HalfFloatFielddataTests method testSingleValued.

public void testSingleValued() throws IOException {
    Directory dir = newDirectory();
    // we need the default codec to check for singletons
    IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(null).setCodec(TestUtil.getDefaultCodec()));
    Document doc = new Document();
    for (IndexableField f : NumberFieldMapper.NumberType.HALF_FLOAT.createFields("half_float", 3f, false, true, false)) {
        doc.add(f);
    }
    w.addDocument(doc);
    final DirectoryReader dirReader = DirectoryReader.open(w);
    LeafReader reader = getOnlyLeafReader(dirReader);
    SortedNumericDoubleValues values = new SortedNumericDVIndexFieldData.SortedNumericHalfFloatFieldData(reader, "half_float").getDoubleValues();
    assertNotNull(FieldData.unwrapSingleton(values));
    values.setDocument(0);
    assertEquals(1, values.count());
    assertEquals(3f, values.valueAt(0), 0f);
    IOUtils.close(dirReader, w, dir);
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) LeafReader(org.apache.lucene.index.LeafReader) IndexWriter(org.apache.lucene.index.IndexWriter) DirectoryReader(org.apache.lucene.index.DirectoryReader) Document(org.apache.lucene.document.Document) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 10 with SortedNumericDoubleValues

use of org.elasticsearch.index.fielddata.SortedNumericDoubleValues in project elasticsearch by elastic.

the class ScaledFloatFieldTypeTests method testFieldData.

public void testFieldData() throws IOException {
    ScaledFloatFieldMapper.ScaledFloatFieldType ft = new ScaledFloatFieldMapper.ScaledFloatFieldType();
    ft.setScalingFactor(0.1 + randomDouble() * 100);
    Directory dir = newDirectory();
    IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(null));
    Document doc = new Document();
    doc.add(new SortedNumericDocValuesField("scaled_float1", 10));
    doc.add(new SortedNumericDocValuesField("scaled_float2", 5));
    doc.add(new SortedNumericDocValuesField("scaled_float2", 12));
    w.addDocument(doc);
    try (DirectoryReader reader = DirectoryReader.open(w)) {
        IndexMetaData indexMetadata = new IndexMetaData.Builder("index").settings(Settings.builder().put("index.version.created", Version.CURRENT).put("index.number_of_shards", 1).put("index.number_of_replicas", 0).build()).build();
        IndexSettings indexSettings = new IndexSettings(indexMetadata, Settings.EMPTY);
        // single-valued
        ft.setName("scaled_float1");
        IndexNumericFieldData fielddata = (IndexNumericFieldData) ft.fielddataBuilder().build(indexSettings, ft, null, null, null);
        assertEquals(fielddata.getNumericType(), IndexNumericFieldData.NumericType.DOUBLE);
        AtomicNumericFieldData leafFieldData = fielddata.load(reader.leaves().get(0));
        SortedNumericDoubleValues values = leafFieldData.getDoubleValues();
        values.setDocument(0);
        assertEquals(1, values.count());
        assertEquals(10 / ft.getScalingFactor(), values.valueAt(0), 10e-5);
        // multi-valued
        ft.setName("scaled_float2");
        fielddata = (IndexNumericFieldData) ft.fielddataBuilder().build(indexSettings, ft, null, null, null);
        leafFieldData = fielddata.load(reader.leaves().get(0));
        values = leafFieldData.getDoubleValues();
        values.setDocument(0);
        assertEquals(2, values.count());
        assertEquals(5 / ft.getScalingFactor(), values.valueAt(0), 10e-5);
        assertEquals(12 / ft.getScalingFactor(), values.valueAt(1), 10e-5);
    }
    IOUtils.close(w, dir);
}
Also used : DirectoryReader(org.apache.lucene.index.DirectoryReader) IndexSettings(org.elasticsearch.index.IndexSettings) IndexNumericFieldData(org.elasticsearch.index.fielddata.IndexNumericFieldData) Document(org.apache.lucene.document.Document) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) IndexWriter(org.apache.lucene.index.IndexWriter) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues) AtomicNumericFieldData(org.elasticsearch.index.fielddata.AtomicNumericFieldData) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Aggregations

SortedNumericDoubleValues (org.elasticsearch.index.fielddata.SortedNumericDoubleValues)27 NumericDoubleValues (org.elasticsearch.index.fielddata.NumericDoubleValues)11 LeafBucketCollectorBase (org.elasticsearch.search.aggregations.LeafBucketCollectorBase)10 BigArrays (org.elasticsearch.common.util.BigArrays)8 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)4 Document (org.apache.lucene.document.Document)3 DirectoryReader (org.apache.lucene.index.DirectoryReader)3 IndexWriter (org.apache.lucene.index.IndexWriter)3 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)3 DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)3 Directory (org.apache.lucene.store.Directory)3 BitSet (org.apache.lucene.util.BitSet)3 FixedBitSet (org.apache.lucene.util.FixedBitSet)3 AtomicNumericFieldData (org.elasticsearch.index.fielddata.AtomicNumericFieldData)3 IndexableField (org.apache.lucene.index.IndexableField)2 LeafReader (org.apache.lucene.index.LeafReader)2 DoubleDocValues (org.apache.lucene.queries.function.docvalues.DoubleDocValues)2 Scorer (org.apache.lucene.search.Scorer)2 SortField (org.apache.lucene.search.SortField)2 GeoPoint (org.elasticsearch.common.geo.GeoPoint)2