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"));
}
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;
}
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);
}
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;
}
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);
}
};
}
Aggregations