Search in sources :

Example 51 with NumericDocValues

use of org.apache.lucene.index.NumericDocValues in project lucene-solr by apache.

the class SortedNumericSelector method wrap.

/** 
   * Wraps a multi-valued SortedNumericDocValues as a single-valued view, using the specified selector 
   * and numericType.
   */
public static NumericDocValues wrap(SortedNumericDocValues sortedNumeric, Type selector, SortField.Type numericType) {
    if (numericType != SortField.Type.INT && numericType != SortField.Type.LONG && numericType != SortField.Type.FLOAT && numericType != SortField.Type.DOUBLE) {
        throw new IllegalArgumentException("numericType must be a numeric type");
    }
    final NumericDocValues view;
    NumericDocValues singleton = DocValues.unwrapSingleton(sortedNumeric);
    if (singleton != null) {
        // it's actually single-valued in practice, but indexed as multi-valued,
        // so just sort on the underlying single-valued dv directly.
        // regardless of selector type, this optimization is safe!
        view = singleton;
    } else {
        switch(selector) {
            case MIN:
                view = new MinValue(sortedNumeric);
                break;
            case MAX:
                view = new MaxValue(sortedNumeric);
                break;
            default:
                throw new AssertionError();
        }
    }
    // undo the numericutils sortability
    switch(numericType) {
        case FLOAT:
            return new FilterNumericDocValues(view) {

                @Override
                public long longValue() throws IOException {
                    return NumericUtils.sortableFloatBits((int) in.longValue());
                }
            };
        case DOUBLE:
            return new FilterNumericDocValues(view) {

                @Override
                public long longValue() throws IOException {
                    return NumericUtils.sortableDoubleBits(in.longValue());
                }
            };
        default:
            return view;
    }
}
Also used : FilterNumericDocValues(org.apache.lucene.index.FilterNumericDocValues) NumericDocValues(org.apache.lucene.index.NumericDocValues) SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) FilterNumericDocValues(org.apache.lucene.index.FilterNumericDocValues)

Example 52 with NumericDocValues

use of org.apache.lucene.index.NumericDocValues in project elasticsearch by elastic.

the class VersionFetchSubPhase method hitExecute.

@Override
public void hitExecute(SearchContext context, HitContext hitContext) {
    if (context.version() == false || (context.storedFieldsContext() != null && context.storedFieldsContext().fetchFields() == false)) {
        return;
    }
    long version = Versions.NOT_FOUND;
    try {
        NumericDocValues versions = hitContext.reader().getNumericDocValues(VersionFieldMapper.NAME);
        if (versions != null) {
            version = versions.get(hitContext.docId());
        }
    } catch (IOException e) {
        throw new ElasticsearchException("Could not retrieve version", e);
    }
    hitContext.hit().version(version < 0 ? -1 : version);
}
Also used : NumericDocValues(org.apache.lucene.index.NumericDocValues) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException)

Example 53 with NumericDocValues

use of org.apache.lucene.index.NumericDocValues in project elasticsearch by elastic.

the class FieldDataTests method testDoublesToSortableLongBits.

public void testDoublesToSortableLongBits() {
    final double value = randomDouble();
    final long valueBits = NumericUtils.doubleToSortableLong(value);
    NumericDoubleValues values = new NumericDoubleValues() {

        @Override
        public double get(int docID) {
            return value;
        }
    };
    SortedNumericDocValues asMultiLongs = FieldData.toSortableLongBits(FieldData.singleton(values, null));
    NumericDocValues asLongs = DocValues.unwrapSingleton(asMultiLongs);
    assertNotNull(asLongs);
    assertEquals(valueBits, asLongs.get(0));
    SortedNumericDoubleValues multiValues = new SortedNumericDoubleValues() {

        @Override
        public double valueAt(int index) {
            return value;
        }

        @Override
        public void setDocument(int doc) {
        }

        @Override
        public int count() {
            return 1;
        }
    };
    asMultiLongs = FieldData.toSortableLongBits(multiValues);
    assertEquals(valueBits, asMultiLongs.valueAt(0));
    assertSame(multiValues, FieldData.sortableLongBitsToDoubles(asMultiLongs));
}
Also used : NumericDocValues(org.apache.lucene.index.NumericDocValues) SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues)

Example 54 with NumericDocValues

use of org.apache.lucene.index.NumericDocValues in project elasticsearch by elastic.

the class MultiValueModeTests method testSingleValuedLongs.

public void testSingleValuedLongs() throws Exception {
    final int numDocs = scaledRandomIntBetween(1, 100);
    final long[] array = new long[numDocs];
    final FixedBitSet docsWithValue = randomBoolean() ? null : new FixedBitSet(numDocs);
    for (int i = 0; i < array.length; ++i) {
        if (randomBoolean()) {
            array[i] = randomLong();
            if (docsWithValue != null) {
                docsWithValue.set(i);
            }
        } else if (docsWithValue != null && randomBoolean()) {
            docsWithValue.set(i);
        }
    }
    final NumericDocValues singleValues = new NumericDocValues() {

        @Override
        public long get(int docID) {
            return array[docID];
        }
    };
    final SortedNumericDocValues multiValues = DocValues.singleton(singleValues, docsWithValue);
    verify(multiValues, numDocs);
    final FixedBitSet rootDocs = randomRootDocs(numDocs);
    final FixedBitSet innerDocs = randomInnerDocs(rootDocs);
    verify(multiValues, numDocs, rootDocs, innerDocs);
}
Also used : NumericDocValues(org.apache.lucene.index.NumericDocValues) SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) FixedBitSet(org.apache.lucene.util.FixedBitSet)

Example 55 with NumericDocValues

use of org.apache.lucene.index.NumericDocValues in project elasticsearch by elastic.

the class MultiValueModeTests method verify.

private void verify(SortedNumericDocValues values, int maxDoc) {
    for (long missingValue : new long[] { 0, randomLong() }) {
        for (MultiValueMode mode : MultiValueMode.values()) {
            final NumericDocValues selected = mode.select(values, missingValue);
            for (int i = 0; i < maxDoc; ++i) {
                final long actual = selected.get(i);
                long expected = 0;
                values.setDocument(i);
                int numValues = values.count();
                if (numValues == 0) {
                    expected = missingValue;
                } else {
                    if (mode == MultiValueMode.MAX) {
                        expected = Long.MIN_VALUE;
                    } else if (mode == MultiValueMode.MIN) {
                        expected = Long.MAX_VALUE;
                    }
                    for (int j = 0; j < numValues; ++j) {
                        if (mode == MultiValueMode.SUM || mode == MultiValueMode.AVG) {
                            expected += values.valueAt(j);
                        } else if (mode == MultiValueMode.MIN) {
                            expected = Math.min(expected, values.valueAt(j));
                        } else if (mode == MultiValueMode.MAX) {
                            expected = Math.max(expected, values.valueAt(j));
                        }
                    }
                    if (mode == MultiValueMode.AVG) {
                        expected = numValues > 1 ? Math.round((double) expected / (double) numValues) : expected;
                    } else if (mode == MultiValueMode.MEDIAN) {
                        int value = numValues / 2;
                        if (numValues % 2 == 0) {
                            expected = Math.round((values.valueAt(value - 1) + values.valueAt(value)) / 2.0);
                        } else {
                            expected = values.valueAt(value);
                        }
                    }
                }
                assertEquals(mode.toString() + " docId=" + i, expected, actual);
            }
        }
    }
}
Also used : NumericDocValues(org.apache.lucene.index.NumericDocValues) SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues)

Aggregations

NumericDocValues (org.apache.lucene.index.NumericDocValues)81 Document (org.apache.lucene.document.Document)30 Directory (org.apache.lucene.store.Directory)29 LeafReader (org.apache.lucene.index.LeafReader)25 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)25 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)23 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)22 SortedNumericDocValues (org.apache.lucene.index.SortedNumericDocValues)22 IOException (java.io.IOException)20 BytesRef (org.apache.lucene.util.BytesRef)19 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)17 HashSet (java.util.HashSet)16 Bits (org.apache.lucene.util.Bits)16 DirectoryReader (org.apache.lucene.index.DirectoryReader)15 SortedDocValues (org.apache.lucene.index.SortedDocValues)15 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)14 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)13 BinaryDocValuesField (org.apache.lucene.document.BinaryDocValuesField)12 IndexReader (org.apache.lucene.index.IndexReader)12 Term (org.apache.lucene.index.Term)12