Search in sources :

Example 61 with BinaryDocValuesField

use of org.apache.lucene.document.BinaryDocValuesField in project lucene-solr by apache.

the class TestBinaryDocValuesUpdates method testDifferentDVFormatPerField.

public void testDifferentDVFormatPerField() throws Exception {
    // test relies on separate instances of "same thing"
    assert TestUtil.getDefaultDocValuesFormat() != TestUtil.getDefaultDocValuesFormat();
    Directory dir = newDirectory();
    IndexWriterConfig conf = newIndexWriterConfig(new MockAnalyzer(random()));
    conf.setCodec(new AssertingCodec() {

        @Override
        public DocValuesFormat getDocValuesFormatForField(String field) {
            return TestUtil.getDefaultDocValuesFormat();
        }
    });
    IndexWriter writer = new IndexWriter(dir, conf);
    Document doc = new Document();
    doc.add(new StringField("key", "doc", Store.NO));
    doc.add(new BinaryDocValuesField("bdv", toBytes(5L)));
    doc.add(new SortedDocValuesField("sorted", new BytesRef("value")));
    // flushed document
    writer.addDocument(doc);
    writer.commit();
    // in-memory document
    writer.addDocument(doc);
    writer.updateBinaryDocValue(new Term("key", "doc"), "bdv", toBytes(17L));
    writer.close();
    final DirectoryReader reader = DirectoryReader.open(dir);
    BinaryDocValues bdv = MultiDocValues.getBinaryValues(reader, "bdv");
    SortedDocValues sdv = MultiDocValues.getSortedValues(reader, "sorted");
    for (int i = 0; i < reader.maxDoc(); i++) {
        assertEquals(i, bdv.nextDoc());
        assertEquals(17, getValue(bdv));
        assertEquals(i, sdv.nextDoc());
        BytesRef term = sdv.binaryValue();
        assertEquals(new BytesRef("value"), term);
    }
    reader.close();
    dir.close();
}
Also used : AssertingCodec(org.apache.lucene.codecs.asserting.AssertingCodec) Document(org.apache.lucene.document.Document) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) DocValuesFormat(org.apache.lucene.codecs.DocValuesFormat) AssertingDocValuesFormat(org.apache.lucene.codecs.asserting.AssertingDocValuesFormat) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) StringField(org.apache.lucene.document.StringField) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory) NRTCachingDirectory(org.apache.lucene.store.NRTCachingDirectory)

Example 62 with BinaryDocValuesField

use of org.apache.lucene.document.BinaryDocValuesField in project lucene-solr by apache.

the class TestBinaryDocValuesUpdates method testUpdateSegmentWithNoDocValues.

public void testUpdateSegmentWithNoDocValues() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig conf = newIndexWriterConfig(new MockAnalyzer(random()));
    // prevent merges, otherwise by the time updates are applied
    // (writer.close()), the segments might have merged and that update becomes
    // legit.
    conf.setMergePolicy(NoMergePolicy.INSTANCE);
    IndexWriter writer = new IndexWriter(dir, conf);
    // first segment with BDV
    Document doc = new Document();
    doc.add(new StringField("id", "doc0", Store.NO));
    doc.add(new BinaryDocValuesField("bdv", toBytes(3L)));
    writer.addDocument(doc);
    doc = new Document();
    // document without 'bdv' field
    doc.add(new StringField("id", "doc4", Store.NO));
    writer.addDocument(doc);
    writer.commit();
    // second segment with no BDV
    doc = new Document();
    doc.add(new StringField("id", "doc1", Store.NO));
    writer.addDocument(doc);
    doc = new Document();
    // document that isn't updated
    doc.add(new StringField("id", "doc2", Store.NO));
    writer.addDocument(doc);
    writer.commit();
    // update document in the first segment - should not affect docsWithField of
    // the document without BDV field
    writer.updateBinaryDocValue(new Term("id", "doc0"), "bdv", toBytes(5L));
    // update document in the second segment - field should be added and we should
    // be able to handle the other document correctly (e.g. no NPE)
    writer.updateBinaryDocValue(new Term("id", "doc1"), "bdv", toBytes(5L));
    writer.close();
    DirectoryReader reader = DirectoryReader.open(dir);
    for (LeafReaderContext context : reader.leaves()) {
        LeafReader r = context.reader();
        BinaryDocValues bdv = r.getBinaryDocValues("bdv");
        assertEquals(0, bdv.nextDoc());
        assertEquals(5L, getValue(bdv));
        assertEquals(NO_MORE_DOCS, bdv.nextDoc());
    }
    reader.close();
    dir.close();
}
Also used : MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) StringField(org.apache.lucene.document.StringField) Document(org.apache.lucene.document.Document) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) Directory(org.apache.lucene.store.Directory) NRTCachingDirectory(org.apache.lucene.store.NRTCachingDirectory)

Example 63 with BinaryDocValuesField

use of org.apache.lucene.document.BinaryDocValuesField in project lucene-solr by apache.

the class TestBinaryDocValuesUpdates method testAddIndexes.

public void testAddIndexes() throws Exception {
    Directory dir1 = newDirectory();
    IndexWriterConfig conf = newIndexWriterConfig(new MockAnalyzer(random()));
    IndexWriter writer = new IndexWriter(dir1, conf);
    final int numDocs = atLeast(50);
    final int numTerms = TestUtil.nextInt(random(), 1, numDocs / 5);
    Set<String> randomTerms = new HashSet<>();
    while (randomTerms.size() < numTerms) {
        randomTerms.add(TestUtil.randomSimpleString(random()));
    }
    // create first index
    for (int i = 0; i < numDocs; i++) {
        Document doc = new Document();
        doc.add(new StringField("id", RandomPicks.randomFrom(random(), randomTerms), Store.NO));
        doc.add(new BinaryDocValuesField("bdv", toBytes(4L)));
        doc.add(new BinaryDocValuesField("control", toBytes(8L)));
        writer.addDocument(doc);
    }
    if (random().nextBoolean()) {
        writer.commit();
    }
    // update some docs to a random value
    long value = random().nextInt();
    Term term = new Term("id", RandomPicks.randomFrom(random(), randomTerms));
    writer.updateDocValues(term, new BinaryDocValuesField("bdv", toBytes(value)), new BinaryDocValuesField("control", toBytes(value * 2)));
    writer.close();
    Directory dir2 = newDirectory();
    conf = newIndexWriterConfig(new MockAnalyzer(random()));
    writer = new IndexWriter(dir2, conf);
    if (random().nextBoolean()) {
        writer.addIndexes(dir1);
    } else {
        DirectoryReader reader = DirectoryReader.open(dir1);
        TestUtil.addIndexesSlowly(writer, reader);
        reader.close();
    }
    writer.close();
    DirectoryReader reader = DirectoryReader.open(dir2);
    for (LeafReaderContext context : reader.leaves()) {
        LeafReader r = context.reader();
        BinaryDocValues bdv = r.getBinaryDocValues("bdv");
        BinaryDocValues control = r.getBinaryDocValues("control");
        for (int i = 0; i < r.maxDoc(); i++) {
            assertEquals(i, bdv.nextDoc());
            assertEquals(i, control.nextDoc());
            assertEquals(getValue(bdv) * 2, getValue(control));
        }
    }
    reader.close();
    IOUtils.close(dir1, dir2);
}
Also used : Document(org.apache.lucene.document.Document) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) StringField(org.apache.lucene.document.StringField) Directory(org.apache.lucene.store.Directory) NRTCachingDirectory(org.apache.lucene.store.NRTCachingDirectory) HashSet(java.util.HashSet)

Example 64 with BinaryDocValuesField

use of org.apache.lucene.document.BinaryDocValuesField in project lucene-solr by apache.

the class TestBinaryDocValuesUpdates method testDeleteUnusedUpdatesFiles.

public void testDeleteUnusedUpdatesFiles() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig conf = newIndexWriterConfig(new MockAnalyzer(random()));
    IndexWriter writer = new IndexWriter(dir, conf);
    Document doc = new Document();
    doc.add(new StringField("id", "d0", Store.NO));
    doc.add(new BinaryDocValuesField("f1", toBytes(1L)));
    doc.add(new BinaryDocValuesField("f2", toBytes(1L)));
    writer.addDocument(doc);
    // update each field twice to make sure all unneeded files are deleted
    for (String f : new String[] { "f1", "f2" }) {
        writer.updateBinaryDocValue(new Term("id", "d0"), f, toBytes(2L));
        writer.commit();
        int numFiles = dir.listAll().length;
        // update again, number of files shouldn't change (old field's gen is
        // removed) 
        writer.updateBinaryDocValue(new Term("id", "d0"), f, toBytes(3L));
        writer.commit();
        assertEquals(numFiles, dir.listAll().length);
    }
    writer.close();
    dir.close();
}
Also used : MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) StringField(org.apache.lucene.document.StringField) Document(org.apache.lucene.document.Document) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) Directory(org.apache.lucene.store.Directory) NRTCachingDirectory(org.apache.lucene.store.NRTCachingDirectory)

Example 65 with BinaryDocValuesField

use of org.apache.lucene.document.BinaryDocValuesField in project lucene-solr by apache.

the class TestBinaryDocValuesUpdates method testDocumentWithNoValue.

public void testDocumentWithNoValue() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig conf = newIndexWriterConfig(new MockAnalyzer(random()));
    IndexWriter writer = new IndexWriter(dir, conf);
    for (int i = 0; i < 2; i++) {
        Document doc = new Document();
        doc.add(new StringField("dvUpdateKey", "dv", Store.NO));
        if (i == 0) {
            // index only one document with value
            doc.add(new BinaryDocValuesField("bdv", toBytes(5L)));
        }
        writer.addDocument(doc);
    }
    writer.commit();
    // update all docs' bdv field
    writer.updateBinaryDocValue(new Term("dvUpdateKey", "dv"), "bdv", toBytes(17L));
    writer.close();
    final DirectoryReader reader = DirectoryReader.open(dir);
    LeafReader r = reader.leaves().get(0).reader();
    BinaryDocValues bdv = r.getBinaryDocValues("bdv");
    for (int i = 0; i < r.maxDoc(); i++) {
        assertEquals(i, bdv.nextDoc());
        assertEquals(17, getValue(bdv));
    }
    reader.close();
    dir.close();
}
Also used : MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) StringField(org.apache.lucene.document.StringField) Document(org.apache.lucene.document.Document) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) Directory(org.apache.lucene.store.Directory) NRTCachingDirectory(org.apache.lucene.store.NRTCachingDirectory)

Aggregations

BinaryDocValuesField (org.apache.lucene.document.BinaryDocValuesField)90 Document (org.apache.lucene.document.Document)84 Directory (org.apache.lucene.store.Directory)71 BytesRef (org.apache.lucene.util.BytesRef)65 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)57 StringField (org.apache.lucene.document.StringField)50 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)40 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)29 SortedSetDocValuesField (org.apache.lucene.document.SortedSetDocValuesField)24 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)23 NRTCachingDirectory (org.apache.lucene.store.NRTCachingDirectory)21 Field (org.apache.lucene.document.Field)16 Analyzer (org.apache.lucene.analysis.Analyzer)15 Random (java.util.Random)12 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)12 StoredField (org.apache.lucene.document.StoredField)11 TextField (org.apache.lucene.document.TextField)11 IOException (java.io.IOException)9 BinaryDocValues (org.apache.lucene.index.BinaryDocValues)9 LeafReader (org.apache.lucene.index.LeafReader)9