Search in sources :

Example 21 with LeafReaderContext

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

the class AbstractStringFieldDataTestCase method testGlobalOrdinals.

public void testGlobalOrdinals() throws Exception {
    fillExtendedMvSet();
    refreshReader();
    IndexOrdinalsFieldData ifd = getForField("string", "value", hasDocValues());
    IndexOrdinalsFieldData globalOrdinals = ifd.loadGlobal(topLevelReader);
    assertThat(topLevelReader.leaves().size(), equalTo(3));
    // First segment
    assertThat(globalOrdinals, instanceOf(GlobalOrdinalsIndexFieldData.class));
    LeafReaderContext leaf = topLevelReader.leaves().get(0);
    AtomicOrdinalsFieldData afd = globalOrdinals.load(leaf);
    RandomAccessOrds values = afd.getOrdinalsValues();
    assertIteratorConsistentWithRandomAccess(values, leaf.reader().maxDoc());
    values.setDocument(0);
    assertThat(values.cardinality(), equalTo(2));
    long ord = values.nextOrd();
    assertThat(ord, equalTo(3L));
    assertThat(values.lookupOrd(ord).utf8ToString(), equalTo("02"));
    ord = values.nextOrd();
    assertThat(ord, equalTo(5L));
    assertThat(values.lookupOrd(ord).utf8ToString(), equalTo("04"));
    values.setDocument(1);
    assertThat(values.cardinality(), equalTo(0));
    values.setDocument(2);
    assertThat(values.cardinality(), equalTo(1));
    ord = values.nextOrd();
    assertThat(ord, equalTo(4L));
    assertThat(values.lookupOrd(ord).utf8ToString(), equalTo("03"));
    // Second segment
    leaf = topLevelReader.leaves().get(1);
    afd = globalOrdinals.load(leaf);
    values = afd.getOrdinalsValues();
    assertIteratorConsistentWithRandomAccess(values, leaf.reader().maxDoc());
    values.setDocument(0);
    assertThat(values.cardinality(), equalTo(3));
    ord = values.nextOrd();
    assertThat(ord, equalTo(5L));
    assertThat(values.lookupOrd(ord).utf8ToString(), equalTo("04"));
    ord = values.nextOrd();
    assertThat(ord, equalTo(6L));
    assertThat(values.lookupOrd(ord).utf8ToString(), equalTo("05"));
    ord = values.nextOrd();
    assertThat(ord, equalTo(7L));
    assertThat(values.lookupOrd(ord).utf8ToString(), equalTo("06"));
    values.setDocument(1);
    assertThat(values.cardinality(), equalTo(3));
    ord = values.nextOrd();
    assertThat(ord, equalTo(7L));
    assertThat(values.lookupOrd(ord).utf8ToString(), equalTo("06"));
    ord = values.nextOrd();
    assertThat(ord, equalTo(8L));
    assertThat(values.lookupOrd(ord).utf8ToString(), equalTo("07"));
    ord = values.nextOrd();
    assertThat(ord, equalTo(9L));
    assertThat(values.lookupOrd(ord).utf8ToString(), equalTo("08"));
    values.setDocument(2);
    assertThat(values.cardinality(), equalTo(0));
    values.setDocument(3);
    assertThat(values.cardinality(), equalTo(3));
    ord = values.nextOrd();
    assertThat(ord, equalTo(9L));
    assertThat(values.lookupOrd(ord).utf8ToString(), equalTo("08"));
    ord = values.nextOrd();
    assertThat(ord, equalTo(10L));
    assertThat(values.lookupOrd(ord).utf8ToString(), equalTo("09"));
    ord = values.nextOrd();
    assertThat(ord, equalTo(11L));
    assertThat(values.lookupOrd(ord).utf8ToString(), equalTo("10"));
    // Third segment
    leaf = topLevelReader.leaves().get(2);
    afd = globalOrdinals.load(leaf);
    values = afd.getOrdinalsValues();
    assertIteratorConsistentWithRandomAccess(values, leaf.reader().maxDoc());
    values.setDocument(0);
    values.setDocument(0);
    assertThat(values.cardinality(), equalTo(3));
    ord = values.nextOrd();
    assertThat(ord, equalTo(0L));
    assertThat(values.lookupOrd(ord).utf8ToString(), equalTo("!08"));
    ord = values.nextOrd();
    assertThat(ord, equalTo(1L));
    assertThat(values.lookupOrd(ord).utf8ToString(), equalTo("!09"));
    ord = values.nextOrd();
    assertThat(ord, equalTo(2L));
    assertThat(values.lookupOrd(ord).utf8ToString(), equalTo("!10"));
}
Also used : GlobalOrdinalsIndexFieldData(org.elasticsearch.index.fielddata.ordinals.GlobalOrdinalsIndexFieldData) RandomAccessOrds(org.apache.lucene.index.RandomAccessOrds) LeafReaderContext(org.apache.lucene.index.LeafReaderContext)

Example 22 with LeafReaderContext

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

the class InternalEngineTests method getSeqNosSet.

private static FixedBitSet getSeqNosSet(final IndexReader reader, final long highestSeqNo) throws IOException {
    // _seq_no are stored as doc values for the time being, so this is how we get them
    // (as opposed to using an IndexSearcher or IndexReader)
    final FixedBitSet bitSet = new FixedBitSet((int) highestSeqNo + 1);
    final List<LeafReaderContext> leaves = reader.leaves();
    if (leaves.isEmpty()) {
        return bitSet;
    }
    for (int i = 0; i < leaves.size(); i++) {
        final LeafReader leaf = leaves.get(i).reader();
        final NumericDocValues values = leaf.getNumericDocValues(SeqNoFieldMapper.NAME);
        if (values == null) {
            continue;
        }
        final Bits bits = leaf.getLiveDocs();
        for (int docID = 0; docID < leaf.maxDoc(); docID++) {
            if (bits == null || bits.get(docID)) {
                final long seqNo = values.get(docID);
                assertFalse("should not have more than one document with the same seq_no[" + seqNo + "]", bitSet.get((int) seqNo));
                bitSet.set((int) seqNo);
            }
        }
    }
    return bitSet;
}
Also used : NumericDocValues(org.apache.lucene.index.NumericDocValues) LeafReader(org.apache.lucene.index.LeafReader) FixedBitSet(org.apache.lucene.util.FixedBitSet) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) Bits(org.apache.lucene.util.Bits) LongPoint(org.apache.lucene.document.LongPoint)

Example 23 with LeafReaderContext

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

the class ShardUtilsTests method testExtractShardId.

public void testExtractShardId() throws IOException {
    BaseDirectoryWrapper dir = newDirectory();
    IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig());
    writer.commit();
    ShardId id = new ShardId("foo", "_na_", random().nextInt());
    try (DirectoryReader reader = DirectoryReader.open(writer)) {
        ElasticsearchDirectoryReader wrap = ElasticsearchDirectoryReader.wrap(reader, id);
        assertEquals(id, ShardUtils.extractShardId(wrap));
    }
    final int numDocs = 1 + random().nextInt(5);
    for (int i = 0; i < numDocs; i++) {
        Document d = new Document();
        d.add(newField("name", "foobar", StringField.TYPE_STORED));
        writer.addDocument(d);
        if (random().nextBoolean()) {
            writer.commit();
        }
    }
    try (DirectoryReader reader = DirectoryReader.open(writer)) {
        ElasticsearchDirectoryReader wrap = ElasticsearchDirectoryReader.wrap(reader, id);
        assertEquals(id, ShardUtils.extractShardId(wrap));
        CompositeReaderContext context = wrap.getContext();
        for (LeafReaderContext leaf : context.leaves()) {
            assertEquals(id, ShardUtils.extractShardId(leaf.reader()));
        }
    }
    IOUtils.close(writer, dir);
}
Also used : ElasticsearchDirectoryReader(org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader) CompositeReaderContext(org.apache.lucene.index.CompositeReaderContext) IndexWriter(org.apache.lucene.index.IndexWriter) DirectoryReader(org.apache.lucene.index.DirectoryReader) ElasticsearchDirectoryReader(org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader) BaseDirectoryWrapper(org.apache.lucene.store.BaseDirectoryWrapper) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) Document(org.apache.lucene.document.Document)

Example 24 with LeafReaderContext

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

the class BlendedTermQuery method adjustDF.

private static TermContext adjustDF(IndexReaderContext readerContext, TermContext ctx, int newDocFreq) {
    assert ctx.wasBuiltFor(readerContext);
    // Use a value of ttf that is consistent with the doc freq (ie. gte)
    long newTTF;
    if (ctx.totalTermFreq() < 0) {
        newTTF = -1;
    } else {
        newTTF = Math.max(ctx.totalTermFreq(), newDocFreq);
    }
    List<LeafReaderContext> leaves = readerContext.leaves();
    final int len;
    if (leaves == null) {
        len = 1;
    } else {
        len = leaves.size();
    }
    TermContext newCtx = new TermContext(readerContext);
    for (int i = 0; i < len; ++i) {
        TermState termState = ctx.get(i);
        if (termState == null) {
            continue;
        }
        newCtx.register(termState, i, newDocFreq, newTTF);
        newDocFreq = 0;
        newTTF = 0;
    }
    return newCtx;
}
Also used : LeafReaderContext(org.apache.lucene.index.LeafReaderContext) TermState(org.apache.lucene.index.TermState) TermContext(org.apache.lucene.index.TermContext)

Example 25 with LeafReaderContext

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

the class MinDocQuery method createWeight.

@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
    return new ConstantScoreWeight(this) {

        @Override
        public Scorer scorer(LeafReaderContext context) throws IOException {
            final int maxDoc = context.reader().maxDoc();
            if (context.docBase + maxDoc <= minDoc) {
                return null;
            }
            final int segmentMinDoc = Math.max(0, minDoc - context.docBase);
            final DocIdSetIterator disi = new DocIdSetIterator() {

                int doc = -1;

                @Override
                public int docID() {
                    return doc;
                }

                @Override
                public int nextDoc() throws IOException {
                    return advance(doc + 1);
                }

                @Override
                public int advance(int target) throws IOException {
                    assert target > doc;
                    if (doc == -1) {
                        // skip directly to minDoc
                        doc = Math.max(target, segmentMinDoc);
                    } else {
                        doc = target;
                    }
                    if (doc >= maxDoc) {
                        doc = NO_MORE_DOCS;
                    }
                    return doc;
                }

                @Override
                public long cost() {
                    return maxDoc - segmentMinDoc;
                }
            };
            return new ConstantScoreScorer(this, score(), disi);
        }
    };
}
Also used : ConstantScoreScorer(org.apache.lucene.search.ConstantScoreScorer) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator) ConstantScoreWeight(org.apache.lucene.search.ConstantScoreWeight)

Aggregations

LeafReaderContext (org.apache.lucene.index.LeafReaderContext)335 LeafReader (org.apache.lucene.index.LeafReader)73 Document (org.apache.lucene.document.Document)71 IOException (java.io.IOException)69 BytesRef (org.apache.lucene.util.BytesRef)67 Directory (org.apache.lucene.store.Directory)61 Term (org.apache.lucene.index.Term)52 IndexSearcher (org.apache.lucene.search.IndexSearcher)49 IndexReader (org.apache.lucene.index.IndexReader)48 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)45 DirectoryReader (org.apache.lucene.index.DirectoryReader)44 Bits (org.apache.lucene.util.Bits)44 NumericDocValues (org.apache.lucene.index.NumericDocValues)43 ArrayList (java.util.ArrayList)41 Weight (org.apache.lucene.search.Weight)37 Terms (org.apache.lucene.index.Terms)36 DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)36 Scorer (org.apache.lucene.search.Scorer)36 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)34 Query (org.apache.lucene.search.Query)34