Search in sources :

Example 1 with SortedNumericDocValues

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

the class MultiValueModeTests method verify.

private void verify(SortedNumericDocValues values, int maxDoc, FixedBitSet rootDocs, FixedBitSet innerDocs) throws IOException {
    for (long missingValue : new long[] { 0, randomLong() }) {
        for (MultiValueMode mode : new MultiValueMode[] { MultiValueMode.MIN, MultiValueMode.MAX, MultiValueMode.SUM, MultiValueMode.AVG }) {
            final NumericDocValues selected = mode.select(values, missingValue, rootDocs, new BitSetIterator(innerDocs, 0L), maxDoc);
            int prevRoot = -1;
            for (int root = rootDocs.nextSetBit(0); root != -1; root = root + 1 < maxDoc ? rootDocs.nextSetBit(root + 1) : -1) {
                final long actual = selected.get(root);
                long expected = 0;
                if (mode == MultiValueMode.MAX) {
                    expected = Long.MIN_VALUE;
                } else if (mode == MultiValueMode.MIN) {
                    expected = Long.MAX_VALUE;
                }
                int numValues = 0;
                for (int child = innerDocs.nextSetBit(prevRoot + 1); child != -1 && child < root; child = innerDocs.nextSetBit(child + 1)) {
                    values.setDocument(child);
                    for (int j = 0; j < values.count(); ++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));
                        }
                        ++numValues;
                    }
                }
                if (numValues == 0) {
                    expected = missingValue;
                } else if (mode == MultiValueMode.AVG) {
                    expected = numValues > 1 ? Math.round((double) expected / (double) numValues) : expected;
                }
                assertEquals(mode.toString() + " docId=" + root, expected, actual);
                prevRoot = root;
            }
        }
    }
}
Also used : NumericDocValues(org.apache.lucene.index.NumericDocValues) SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) BitSetIterator(org.apache.lucene.util.BitSetIterator)

Example 2 with SortedNumericDocValues

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

the class MultiValueModeTests method testMultiValuedLongs.

public void testMultiValuedLongs() throws Exception {
    final int numDocs = scaledRandomIntBetween(1, 100);
    final long[][] array = new long[numDocs][];
    for (int i = 0; i < numDocs; ++i) {
        final long[] values = new long[randomInt(4)];
        for (int j = 0; j < values.length; ++j) {
            values[j] = randomLong();
        }
        Arrays.sort(values);
        array[i] = values;
    }
    final SortedNumericDocValues multiValues = new SortedNumericDocValues() {

        int doc;

        @Override
        public long valueAt(int index) {
            return array[doc][index];
        }

        @Override
        public void setDocument(int doc) {
            this.doc = doc;
        }

        @Override
        public int count() {
            return array[doc].length;
        }
    };
    verify(multiValues, numDocs);
    final FixedBitSet rootDocs = randomRootDocs(numDocs);
    final FixedBitSet innerDocs = randomInnerDocs(rootDocs);
    verify(multiValues, numDocs, rootDocs, innerDocs);
}
Also used : SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) FixedBitSet(org.apache.lucene.util.FixedBitSet)

Example 3 with SortedNumericDocValues

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

the class ValuesSourceConfigTests method testEmptyLong.

public void testEmptyLong() throws Exception {
    IndexService indexService = createIndex("index", Settings.EMPTY, "type", "long", "type=long");
    client().prepareIndex("index", "type", "1").setSource().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get();
    try (Searcher searcher = indexService.getShard(0).acquireSearcher("test")) {
        QueryShardContext context = indexService.newQueryShardContext(0, searcher.reader(), () -> 42L);
        ValuesSourceConfig<ValuesSource.Numeric> config = ValuesSourceConfig.resolve(context, null, "long", null, null, null, null);
        ValuesSource.Numeric valuesSource = config.toValuesSource(context);
        LeafReaderContext ctx = searcher.reader().leaves().get(0);
        SortedNumericDocValues values = valuesSource.longValues(ctx);
        values.setDocument(0);
        assertEquals(0, values.count());
        config = ValuesSourceConfig.resolve(context, null, "long", null, 42, null, null);
        valuesSource = config.toValuesSource(context);
        values = valuesSource.longValues(ctx);
        values.setDocument(0);
        assertEquals(1, values.count());
        assertEquals(42, values.valueAt(0));
    }
}
Also used : SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) IndexService(org.elasticsearch.index.IndexService) Searcher(org.elasticsearch.index.engine.Engine.Searcher) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) LeafReaderContext(org.apache.lucene.index.LeafReaderContext)

Example 4 with SortedNumericDocValues

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

the class ValuesSourceConfigTests method testUnmappedLong.

public void testUnmappedLong() throws Exception {
    IndexService indexService = createIndex("index", Settings.EMPTY, "type");
    client().prepareIndex("index", "type", "1").setSource().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get();
    try (Searcher searcher = indexService.getShard(0).acquireSearcher("test")) {
        QueryShardContext context = indexService.newQueryShardContext(0, searcher.reader(), () -> 42L);
        ValuesSourceConfig<ValuesSource.Numeric> config = ValuesSourceConfig.resolve(context, ValueType.NUMBER, "long", null, null, null, null);
        ValuesSource.Numeric valuesSource = config.toValuesSource(context);
        assertNull(valuesSource);
        config = ValuesSourceConfig.resolve(context, ValueType.NUMBER, "long", null, 42, null, null);
        valuesSource = config.toValuesSource(context);
        LeafReaderContext ctx = searcher.reader().leaves().get(0);
        SortedNumericDocValues values = valuesSource.longValues(ctx);
        values.setDocument(0);
        assertEquals(1, values.count());
        assertEquals(42, values.valueAt(0));
    }
}
Also used : SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) IndexService(org.elasticsearch.index.IndexService) Searcher(org.elasticsearch.index.engine.Engine.Searcher) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) LeafReaderContext(org.apache.lucene.index.LeafReaderContext)

Example 5 with SortedNumericDocValues

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

the class ValuesSourceConfigTests method testBoolean.

public void testBoolean() throws Exception {
    IndexService indexService = createIndex("index", Settings.EMPTY, "type", "bool", "type=boolean");
    client().prepareIndex("index", "type", "1").setSource("bool", true).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get();
    try (Searcher searcher = indexService.getShard(0).acquireSearcher("test")) {
        QueryShardContext context = indexService.newQueryShardContext(0, searcher.reader(), () -> 42L);
        ValuesSourceConfig<ValuesSource.Numeric> config = ValuesSourceConfig.resolve(context, null, "bool", null, null, null, null);
        ValuesSource.Numeric valuesSource = config.toValuesSource(context);
        LeafReaderContext ctx = searcher.reader().leaves().get(0);
        SortedNumericDocValues values = valuesSource.longValues(ctx);
        values.setDocument(0);
        assertEquals(1, values.count());
        assertEquals(1, values.valueAt(0));
    }
}
Also used : SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) IndexService(org.elasticsearch.index.IndexService) Searcher(org.elasticsearch.index.engine.Engine.Searcher) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) LeafReaderContext(org.apache.lucene.index.LeafReaderContext)

Aggregations

SortedNumericDocValues (org.apache.lucene.index.SortedNumericDocValues)44 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)23 NumericDocValues (org.apache.lucene.index.NumericDocValues)13 LeafReader (org.apache.lucene.index.LeafReader)10 BytesRef (org.apache.lucene.util.BytesRef)7 Document (org.apache.lucene.document.Document)6 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)6 Directory (org.apache.lucene.store.Directory)6 IndexService (org.elasticsearch.index.IndexService)6 Searcher (org.elasticsearch.index.engine.Engine.Searcher)6 QueryShardContext (org.elasticsearch.index.query.QueryShardContext)6 IOException (java.io.IOException)5 BinaryDocValues (org.apache.lucene.index.BinaryDocValues)5 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)5 SortedDocValues (org.apache.lucene.index.SortedDocValues)5 SortedSetDocValues (org.apache.lucene.index.SortedSetDocValues)5 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)4 DirectoryReader (org.apache.lucene.index.DirectoryReader)4 IndexWriter (org.apache.lucene.index.IndexWriter)4 BitSet (org.apache.lucene.util.BitSet)4