Search in sources :

Example 1 with SortedDocValues

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

the class ParentFieldSubFetchPhase method getParentId.

public static String getParentId(ParentFieldMapper fieldMapper, LeafReader reader, int docId) {
    try {
        SortedDocValues docValues = reader.getSortedDocValues(fieldMapper.name());
        if (docValues == null) {
            // hit has no _parent field.
            return null;
        }
        BytesRef parentId = docValues.get(docId);
        return parentId.length > 0 ? parentId.utf8ToString() : null;
    } catch (IOException e) {
        throw ExceptionsHelper.convertToElastic(e);
    }
}
Also used : IOException(java.io.IOException) SortedDocValues(org.apache.lucene.index.SortedDocValues) BytesRef(org.apache.lucene.util.BytesRef)

Example 2 with SortedDocValues

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

the class GlobalOrdinalsStringTermsAggregator method newCollector.

protected LeafBucketCollector newCollector(final RandomAccessOrds ords, final LeafBucketCollector sub) {
    grow(ords.getValueCount());
    final SortedDocValues singleValues = DocValues.unwrapSingleton(ords);
    if (singleValues != null) {
        return new LeafBucketCollectorBase(sub, ords) {

            @Override
            public void collect(int doc, long bucket) throws IOException {
                assert bucket == 0;
                final int ord = singleValues.getOrd(doc);
                if (ord >= 0) {
                    collectExistingBucket(sub, doc, ord);
                }
            }
        };
    } else {
        return new LeafBucketCollectorBase(sub, ords) {

            @Override
            public void collect(int doc, long bucket) throws IOException {
                assert bucket == 0;
                ords.setDocument(doc);
                final int numOrds = ords.cardinality();
                for (int i = 0; i < numOrds; i++) {
                    final long globalOrd = ords.ordAt(i);
                    collectExistingBucket(sub, doc, globalOrd);
                }
            }
        };
    }
}
Also used : LeafBucketCollectorBase(org.elasticsearch.search.aggregations.LeafBucketCollectorBase) SortedDocValues(org.apache.lucene.index.SortedDocValues)

Example 3 with SortedDocValues

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

the class ParentToChildrenAggregator method getLeafCollector.

@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, final LeafBucketCollector sub) throws IOException {
    if (valuesSource == null) {
        return LeafBucketCollector.NO_OP_COLLECTOR;
    }
    final SortedDocValues globalOrdinals = valuesSource.globalOrdinalsValues(parentType, ctx);
    assert globalOrdinals != null;
    Scorer parentScorer = parentFilter.scorer(ctx);
    final Bits parentDocs = Lucene.asSequentialAccessBits(ctx.reader().maxDoc(), parentScorer);
    return new LeafBucketCollector() {

        @Override
        public void collect(int docId, long bucket) throws IOException {
            if (parentDocs.get(docId)) {
                long globalOrdinal = globalOrdinals.getOrd(docId);
                if (globalOrdinal != -1) {
                    if (parentOrdToBuckets.get(globalOrdinal) == -1) {
                        parentOrdToBuckets.set(globalOrdinal, bucket);
                    } else {
                        long[] bucketOrds = parentOrdToOtherBuckets.get(globalOrdinal);
                        if (bucketOrds != null) {
                            bucketOrds = Arrays.copyOf(bucketOrds, bucketOrds.length + 1);
                            bucketOrds[bucketOrds.length - 1] = bucket;
                            parentOrdToOtherBuckets.put(globalOrdinal, bucketOrds);
                        } else {
                            parentOrdToOtherBuckets.put(globalOrdinal, new long[] { bucket });
                        }
                        multipleBucketsPerParentOrd = true;
                    }
                }
            }
        }
    };
}
Also used : LeafBucketCollector(org.elasticsearch.search.aggregations.LeafBucketCollector) ConstantScoreScorer(org.apache.lucene.search.ConstantScoreScorer) Scorer(org.apache.lucene.search.Scorer) Bits(org.apache.lucene.util.Bits) SortedDocValues(org.apache.lucene.index.SortedDocValues)

Example 4 with SortedDocValues

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

the class ParentChildFieldDataTests method testThreads.

public void testThreads() throws Exception {
    final ParentChildIndexFieldData indexFieldData = getForField(childType);
    final DirectoryReader reader = ElasticsearchDirectoryReader.wrap(DirectoryReader.open(writer), new ShardId(new Index("test", ""), 0));
    final IndexParentChildFieldData global = indexFieldData.loadGlobal(reader);
    final AtomicReference<Exception> error = new AtomicReference<>();
    final int numThreads = scaledRandomIntBetween(3, 8);
    final Thread[] threads = new Thread[numThreads];
    final CountDownLatch latch = new CountDownLatch(1);
    final Map<Object, BytesRef[]> expected = new HashMap<>();
    for (LeafReaderContext context : reader.leaves()) {
        AtomicParentChildFieldData leafData = global.load(context);
        SortedDocValues parentIds = leafData.getOrdinalsValues(parentType);
        final BytesRef[] ids = new BytesRef[parentIds.getValueCount()];
        for (int j = 0; j < parentIds.getValueCount(); ++j) {
            final BytesRef id = parentIds.lookupOrd(j);
            if (id != null) {
                ids[j] = BytesRef.deepCopyOf(id);
            }
        }
        expected.put(context.reader().getCoreCacheKey(), ids);
    }
    for (int i = 0; i < numThreads; ++i) {
        threads[i] = new Thread() {

            @Override
            public void run() {
                try {
                    latch.await();
                    for (int i = 0; i < 100000; ++i) {
                        for (LeafReaderContext context : reader.leaves()) {
                            AtomicParentChildFieldData leafData = global.load(context);
                            SortedDocValues parentIds = leafData.getOrdinalsValues(parentType);
                            final BytesRef[] expectedIds = expected.get(context.reader().getCoreCacheKey());
                            for (int j = 0; j < parentIds.getValueCount(); ++j) {
                                final BytesRef id = parentIds.lookupOrd(j);
                                assertEquals(expectedIds[j], id);
                            }
                        }
                    }
                } catch (Exception e) {
                    error.compareAndSet(null, e);
                }
            }
        };
        threads[i].start();
    }
    latch.countDown();
    for (Thread thread : threads) {
        thread.join();
    }
    if (error.get() != null) {
        throw error.get();
    }
}
Also used : ElasticsearchDirectoryReader(org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader) DirectoryReader(org.apache.lucene.index.DirectoryReader) HashMap(java.util.HashMap) Index(org.elasticsearch.index.Index) AtomicReference(java.util.concurrent.atomic.AtomicReference) ParentChildIndexFieldData(org.elasticsearch.index.fielddata.plain.ParentChildIndexFieldData) CountDownLatch(java.util.concurrent.CountDownLatch) SortedDocValues(org.apache.lucene.index.SortedDocValues) ShardId(org.elasticsearch.index.shard.ShardId) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) BytesRef(org.apache.lucene.util.BytesRef)

Example 5 with SortedDocValues

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

the class SingleOrdinalsTests method testSvValues.

public void testSvValues() throws IOException {
    int numDocs = 1000000;
    int numOrdinals = numDocs / 4;
    Map<Integer, Long> controlDocToOrdinal = new HashMap<>();
    OrdinalsBuilder builder = new OrdinalsBuilder(numDocs);
    long ordinal = builder.currentOrdinal();
    for (int doc = 0; doc < numDocs; doc++) {
        if (doc % numOrdinals == 0) {
            ordinal = builder.nextOrdinal();
        }
        controlDocToOrdinal.put(doc, ordinal);
        builder.addDoc(doc);
    }
    Ordinals ords = builder.build();
    assertThat(ords, instanceOf(SinglePackedOrdinals.class));
    RandomAccessOrds docs = ords.ordinals();
    final SortedDocValues singleOrds = DocValues.unwrapSingleton(docs);
    assertNotNull(singleOrds);
    for (Map.Entry<Integer, Long> entry : controlDocToOrdinal.entrySet()) {
        assertThat(entry.getValue(), equalTo((long) singleOrds.getOrd(entry.getKey())));
    }
}
Also used : HashMap(java.util.HashMap) RandomAccessOrds(org.apache.lucene.index.RandomAccessOrds) SortedDocValues(org.apache.lucene.index.SortedDocValues) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

SortedDocValues (org.apache.lucene.index.SortedDocValues)66 BytesRef (org.apache.lucene.util.BytesRef)32 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)27 SortedSetDocValues (org.apache.lucene.index.SortedSetDocValues)23 LeafReader (org.apache.lucene.index.LeafReader)22 Document (org.apache.lucene.document.Document)21 NumericDocValues (org.apache.lucene.index.NumericDocValues)15 Directory (org.apache.lucene.store.Directory)15 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)14 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)13 IOException (java.io.IOException)12 BinaryDocValues (org.apache.lucene.index.BinaryDocValues)12 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)10 MultiDocValues (org.apache.lucene.index.MultiDocValues)10 ArrayList (java.util.ArrayList)9 IndexReader (org.apache.lucene.index.IndexReader)9 OrdinalMap (org.apache.lucene.index.MultiDocValues.OrdinalMap)9 DoublePoint (org.apache.lucene.document.DoublePoint)8 FloatPoint (org.apache.lucene.document.FloatPoint)8 IntPoint (org.apache.lucene.document.IntPoint)8