Search in sources :

Example 1 with DocIdAndVersion

use of org.elasticsearch.common.lucene.uid.Versions.DocIdAndVersion in project elasticsearch by elastic.

the class VersionLookupTests method testSimple.

/** 
     * test version lookup actually works
     */
public void testSimple() throws Exception {
    Directory dir = newDirectory();
    IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(Lucene.STANDARD_ANALYZER));
    Document doc = new Document();
    doc.add(new Field(UidFieldMapper.NAME, "6", UidFieldMapper.Defaults.FIELD_TYPE));
    doc.add(new NumericDocValuesField(VersionFieldMapper.NAME, 87));
    writer.addDocument(doc);
    DirectoryReader reader = DirectoryReader.open(writer);
    LeafReaderContext segment = reader.leaves().get(0);
    PerThreadIDAndVersionLookup lookup = new PerThreadIDAndVersionLookup(segment.reader());
    // found doc
    DocIdAndVersion result = lookup.lookup(new BytesRef("6"), null, segment);
    assertNotNull(result);
    assertEquals(87, result.version);
    assertEquals(0, result.docId);
    // not found doc
    assertNull(lookup.lookup(new BytesRef("7"), null, segment));
    // deleted doc
    assertNull(lookup.lookup(new BytesRef("6"), new Bits.MatchNoBits(1), segment));
    reader.close();
    writer.close();
    dir.close();
}
Also used : NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) Field(org.apache.lucene.document.Field) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) IndexWriter(org.apache.lucene.index.IndexWriter) DirectoryReader(org.apache.lucene.index.DirectoryReader) DocIdAndVersion(org.elasticsearch.common.lucene.uid.Versions.DocIdAndVersion) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) Document(org.apache.lucene.document.Document) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 2 with DocIdAndVersion

use of org.elasticsearch.common.lucene.uid.Versions.DocIdAndVersion in project elasticsearch by elastic.

the class VersionLookupTests method testTwoDocuments.

/** 
     * test version lookup with two documents matching the ID
     */
public void testTwoDocuments() throws Exception {
    Directory dir = newDirectory();
    IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(Lucene.STANDARD_ANALYZER));
    Document doc = new Document();
    doc.add(new Field(UidFieldMapper.NAME, "6", UidFieldMapper.Defaults.FIELD_TYPE));
    doc.add(new NumericDocValuesField(VersionFieldMapper.NAME, 87));
    writer.addDocument(doc);
    writer.addDocument(doc);
    DirectoryReader reader = DirectoryReader.open(writer);
    LeafReaderContext segment = reader.leaves().get(0);
    PerThreadIDAndVersionLookup lookup = new PerThreadIDAndVersionLookup(segment.reader());
    // return the last doc when there are duplicates
    DocIdAndVersion result = lookup.lookup(new BytesRef("6"), null, segment);
    assertNotNull(result);
    assertEquals(87, result.version);
    assertEquals(1, result.docId);
    // delete the first doc only
    FixedBitSet live = new FixedBitSet(2);
    live.set(1);
    result = lookup.lookup(new BytesRef("6"), live, segment);
    assertNotNull(result);
    assertEquals(87, result.version);
    assertEquals(1, result.docId);
    // delete the second doc only
    live.clear(1);
    live.set(0);
    result = lookup.lookup(new BytesRef("6"), live, segment);
    assertNotNull(result);
    assertEquals(87, result.version);
    assertEquals(0, result.docId);
    // delete both docs
    assertNull(lookup.lookup(new BytesRef("6"), new Bits.MatchNoBits(2), segment));
    reader.close();
    writer.close();
    dir.close();
}
Also used : DirectoryReader(org.apache.lucene.index.DirectoryReader) Document(org.apache.lucene.document.Document) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) Field(org.apache.lucene.document.Field) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) IndexWriter(org.apache.lucene.index.IndexWriter) FixedBitSet(org.apache.lucene.util.FixedBitSet) DocIdAndVersion(org.elasticsearch.common.lucene.uid.Versions.DocIdAndVersion) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Aggregations

Document (org.apache.lucene.document.Document)2 Field (org.apache.lucene.document.Field)2 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)2 DirectoryReader (org.apache.lucene.index.DirectoryReader)2 IndexWriter (org.apache.lucene.index.IndexWriter)2 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)2 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)2 Directory (org.apache.lucene.store.Directory)2 BytesRef (org.apache.lucene.util.BytesRef)2 DocIdAndVersion (org.elasticsearch.common.lucene.uid.Versions.DocIdAndVersion)2 FixedBitSet (org.apache.lucene.util.FixedBitSet)1