Search in sources :

Example 6 with RandomAccessOrds

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

the class CardinalityAggregator method pickCollector.

private Collector pickCollector(LeafReaderContext ctx) throws IOException {
    if (valuesSource == null) {
        return new EmptyCollector();
    }
    if (valuesSource instanceof ValuesSource.Numeric) {
        ValuesSource.Numeric source = (ValuesSource.Numeric) valuesSource;
        MurmurHash3Values hashValues = source.isFloatingPoint() ? MurmurHash3Values.hash(source.doubleValues(ctx)) : MurmurHash3Values.hash(source.longValues(ctx));
        return new DirectCollector(counts, hashValues);
    }
    if (valuesSource instanceof ValuesSource.Bytes.WithOrdinals) {
        ValuesSource.Bytes.WithOrdinals source = (ValuesSource.Bytes.WithOrdinals) valuesSource;
        final RandomAccessOrds ordinalValues = source.ordinalsValues(ctx);
        final long maxOrd = ordinalValues.getValueCount();
        if (maxOrd == 0) {
            return new EmptyCollector();
        }
        final long ordinalsMemoryUsage = OrdinalsCollector.memoryOverhead(maxOrd);
        final long countsMemoryUsage = HyperLogLogPlusPlus.memoryUsage(precision);
        // only use ordinals if they don't increase memory usage by more than 25%
        if (ordinalsMemoryUsage < countsMemoryUsage / 4) {
            return new OrdinalsCollector(counts, ordinalValues, context.bigArrays());
        }
    }
    return new DirectCollector(counts, MurmurHash3Values.hash(valuesSource.bytesValues(ctx)));
}
Also used : RandomAccessOrds(org.apache.lucene.index.RandomAccessOrds) ValuesSource(org.elasticsearch.search.aggregations.support.ValuesSource)

Example 7 with RandomAccessOrds

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

the class BytesRefFieldComparatorSource method newComparator.

@Override
public FieldComparator<?> newComparator(String fieldname, int numHits, int sortPos, boolean reversed) {
    assert indexFieldData == null || fieldname.equals(indexFieldData.getFieldName());
    final boolean sortMissingLast = sortMissingLast(missingValue) ^ reversed;
    final BytesRef missingBytes = (BytesRef) missingObject(missingValue, reversed);
    if (indexFieldData instanceof IndexOrdinalsFieldData) {
        return new FieldComparator.TermOrdValComparator(numHits, null, sortMissingLast) {

            @Override
            protected SortedDocValues getSortedDocValues(LeafReaderContext context, String field) throws IOException {
                final RandomAccessOrds values = ((IndexOrdinalsFieldData) indexFieldData).load(context).getOrdinalsValues();
                final SortedDocValues selectedValues;
                if (nested == null) {
                    selectedValues = sortMode.select(values);
                } else {
                    final BitSet rootDocs = nested.rootDocs(context);
                    final DocIdSetIterator innerDocs = nested.innerDocs(context);
                    selectedValues = sortMode.select(values, rootDocs, innerDocs);
                }
                if (sortMissingFirst(missingValue) || sortMissingLast(missingValue)) {
                    return selectedValues;
                } else {
                    return new ReplaceMissing(selectedValues, missingBytes);
                }
            }

            @Override
            public void setScorer(Scorer scorer) {
                BytesRefFieldComparatorSource.this.setScorer(scorer);
            }
        };
    }
    final BytesRef nullPlaceHolder = new BytesRef();
    final BytesRef nonNullMissingBytes = missingBytes == null ? nullPlaceHolder : missingBytes;
    return new FieldComparator.TermValComparator(numHits, null, sortMissingLast) {

        @Override
        protected BinaryDocValues getBinaryDocValues(LeafReaderContext context, String field) throws IOException {
            final SortedBinaryDocValues values = getValues(context);
            final BinaryDocValues selectedValues;
            if (nested == null) {
                selectedValues = sortMode.select(values, nonNullMissingBytes);
            } else {
                final BitSet rootDocs = nested.rootDocs(context);
                final DocIdSetIterator innerDocs = nested.innerDocs(context);
                selectedValues = sortMode.select(values, nonNullMissingBytes, rootDocs, innerDocs, context.reader().maxDoc());
            }
            return selectedValues;
        }

        @Override
        protected Bits getDocsWithField(LeafReaderContext context, String field) throws IOException {
            return new Bits.MatchAllBits(context.reader().maxDoc());
        }

        @Override
        protected boolean isNull(int doc, BytesRef term) {
            return term == nullPlaceHolder;
        }

        @Override
        public void setScorer(Scorer scorer) {
            BytesRefFieldComparatorSource.this.setScorer(scorer);
        }
    };
}
Also used : RandomAccessOrds(org.apache.lucene.index.RandomAccessOrds) BitSet(org.apache.lucene.util.BitSet) Scorer(org.apache.lucene.search.Scorer) SortedDocValues(org.apache.lucene.index.SortedDocValues) SortedBinaryDocValues(org.elasticsearch.index.fielddata.SortedBinaryDocValues) BinaryDocValues(org.apache.lucene.index.BinaryDocValues) SortedBinaryDocValues(org.elasticsearch.index.fielddata.SortedBinaryDocValues) IndexOrdinalsFieldData(org.elasticsearch.index.fielddata.IndexOrdinalsFieldData) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator) BytesRef(org.apache.lucene.util.BytesRef)

Example 8 with RandomAccessOrds

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

the class MultiValueModeTests method testSingleValuedOrds.

public void testSingleValuedOrds() throws Exception {
    final int numDocs = scaledRandomIntBetween(1, 100);
    final int[] array = new int[numDocs];
    for (int i = 0; i < array.length; ++i) {
        if (randomBoolean()) {
            array[i] = randomInt(1000);
        } else {
            array[i] = -1;
        }
    }
    final SortedDocValues singleValues = new SortedDocValues() {

        @Override
        public int getOrd(int docID) {
            return array[docID];
        }

        @Override
        public BytesRef lookupOrd(int ord) {
            throw new UnsupportedOperationException();
        }

        @Override
        public int getValueCount() {
            return 1 << 20;
        }
    };
    final RandomAccessOrds multiValues = (RandomAccessOrds) DocValues.singleton(singleValues);
    verify(multiValues, numDocs);
    final FixedBitSet rootDocs = randomRootDocs(numDocs);
    final FixedBitSet innerDocs = randomInnerDocs(rootDocs);
    verify(multiValues, numDocs, rootDocs, innerDocs);
}
Also used : RandomAccessOrds(org.apache.lucene.index.RandomAccessOrds) FixedBitSet(org.apache.lucene.util.FixedBitSet) SortedDocValues(org.apache.lucene.index.SortedDocValues)

Example 9 with RandomAccessOrds

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

the class IncludeExcludeTests method testSingleTermWithOrds.

public void testSingleTermWithOrds() throws IOException {
    RandomAccessOrds ords = new RandomAccessOrds() {

        boolean consumed = true;

        @Override
        public void setDocument(int docID) {
            consumed = false;
        }

        @Override
        public long nextOrd() {
            if (consumed) {
                return SortedSetDocValues.NO_MORE_ORDS;
            } else {
                consumed = true;
                return 0;
            }
        }

        @Override
        public BytesRef lookupOrd(long ord) {
            assertEquals(0, ord);
            return new BytesRef("foo");
        }

        @Override
        public long getValueCount() {
            return 1;
        }

        @Override
        public long ordAt(int index) {
            return 0;
        }

        @Override
        public int cardinality() {
            return 1;
        }
    };
    IncludeExclude inexcl = new IncludeExclude(new TreeSet<>(Collections.singleton(new BytesRef("foo"))), null);
    OrdinalsFilter filter = inexcl.convertToOrdinalsFilter(DocValueFormat.RAW);
    LongBitSet acceptedOrds = filter.acceptedGlobalOrdinals(ords);
    assertEquals(1, acceptedOrds.length());
    assertTrue(acceptedOrds.get(0));
    inexcl = new IncludeExclude(new TreeSet<>(Collections.singleton(new BytesRef("bar"))), null);
    filter = inexcl.convertToOrdinalsFilter(DocValueFormat.RAW);
    acceptedOrds = filter.acceptedGlobalOrdinals(ords);
    assertEquals(1, acceptedOrds.length());
    assertFalse(acceptedOrds.get(0));
    inexcl = new IncludeExclude(new TreeSet<>(Collections.singleton(new BytesRef("foo"))), new TreeSet<>(Collections.singleton(new BytesRef("foo"))));
    filter = inexcl.convertToOrdinalsFilter(DocValueFormat.RAW);
    acceptedOrds = filter.acceptedGlobalOrdinals(ords);
    assertEquals(1, acceptedOrds.length());
    assertFalse(acceptedOrds.get(0));
    inexcl = new IncludeExclude(// means everything included
    null, new TreeSet<>(Collections.singleton(new BytesRef("foo"))));
    filter = inexcl.convertToOrdinalsFilter(DocValueFormat.RAW);
    acceptedOrds = filter.acceptedGlobalOrdinals(ords);
    assertEquals(1, acceptedOrds.length());
    assertFalse(acceptedOrds.get(0));
}
Also used : RandomAccessOrds(org.apache.lucene.index.RandomAccessOrds) TreeSet(java.util.TreeSet) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude) OrdinalsFilter(org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude.OrdinalsFilter) LongBitSet(org.apache.lucene.util.LongBitSet) BytesRef(org.apache.lucene.util.BytesRef)

Example 10 with RandomAccessOrds

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

the class MultiOrdinalsTests method testMultiValuesDocsWithOverlappingStorageArrays.

public void testMultiValuesDocsWithOverlappingStorageArrays() throws Exception {
    int maxDoc = 7;
    long maxOrds = 15;
    OrdinalsBuilder builder = new OrdinalsBuilder(maxDoc);
    for (int i = 0; i < maxOrds; i++) {
        builder.nextOrdinal();
        if (i < 10) {
            builder.addDoc(0);
        }
        builder.addDoc(1);
        if (i == 0) {
            builder.addDoc(2);
        }
        if (i < 5) {
            builder.addDoc(3);
        }
        if (i < 6) {
            builder.addDoc(4);
        }
        if (i == 1) {
            builder.addDoc(5);
        }
        if (i < 10) {
            builder.addDoc(6);
        }
    }
    long[][] ordinalPlan = new long[][] { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }, { 0 }, { 0, 1, 2, 3, 4 }, { 0, 1, 2, 3, 4, 5 }, { 1 }, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } };
    Ordinals ordinals = new MultiOrdinals(builder, PackedInts.FASTEST);
    RandomAccessOrds docs = ordinals.ordinals();
    assertEquals(docs, ordinalPlan);
}
Also used : RandomAccessOrds(org.apache.lucene.index.RandomAccessOrds)

Aggregations

RandomAccessOrds (org.apache.lucene.index.RandomAccessOrds)12 SortedDocValues (org.apache.lucene.index.SortedDocValues)4 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)3 BytesRef (org.apache.lucene.util.BytesRef)3 HashSet (java.util.HashSet)2 Random (java.util.Random)2 FixedBitSet (org.apache.lucene.util.FixedBitSet)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1 Document (org.apache.lucene.document.Document)1 StringField (org.apache.lucene.document.StringField)1 BinaryDocValues (org.apache.lucene.index.BinaryDocValues)1 DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)1 Scorer (org.apache.lucene.search.Scorer)1 BitSet (org.apache.lucene.util.BitSet)1 LongBitSet (org.apache.lucene.util.LongBitSet)1 GeoPoint (org.elasticsearch.common.geo.GeoPoint)1 AbstractRandomAccessOrds (org.elasticsearch.index.fielddata.AbstractRandomAccessOrds)1