Search in sources :

Example 71 with BinaryDocValuesField

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

the class TestBinaryDocValuesUpdates method testUpdateSegmentWithPostingButNoDocValues.

public void testUpdateSegmentWithPostingButNoDocValues() 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 StringField("bdv", "mock-value", Store.NO));
    doc.add(new BinaryDocValuesField("bdv", toBytes(5L)));
    writer.addDocument(doc);
    writer.commit();
    // second segment with no BDV
    doc = new Document();
    doc.add(new StringField("id", "doc1", Store.NO));
    doc.add(new StringField("bdv", "mock-value", Store.NO));
    writer.addDocument(doc);
    writer.commit();
    // update document in the second segment
    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");
        for (int i = 0; i < r.maxDoc(); i++) {
            assertEquals(i, bdv.nextDoc());
            assertEquals(5L, getValue(bdv));
        }
    }
    reader.close();
    dir.close();
}
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)

Example 72 with BinaryDocValuesField

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

the class AllGroupHeadsCollectorTest method addGroupField.

private void addGroupField(Document doc, String groupField, String value, DocValuesType valueType) {
    Field valuesField = null;
    switch(valueType) {
        case BINARY:
            valuesField = new BinaryDocValuesField(groupField, new BytesRef(value));
            break;
        case SORTED:
            valuesField = new SortedDocValuesField(groupField, new BytesRef(value));
            break;
        default:
            fail("unhandled type");
    }
    doc.add(valuesField);
}
Also used : SortField(org.apache.lucene.search.SortField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) Field(org.apache.lucene.document.Field) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) BytesRef(org.apache.lucene.util.BytesRef)

Example 73 with BinaryDocValuesField

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

the class TestDocValuesFieldSources method test.

@SuppressWarnings("fallthrough")
public void test(DocValuesType type) throws IOException {
    Directory d = newDirectory();
    IndexWriterConfig iwConfig = newIndexWriterConfig(new MockAnalyzer(random()));
    final int nDocs = atLeast(50);
    final Field id = new NumericDocValuesField("id", 0);
    final Field f;
    switch(type) {
        case BINARY:
            f = new BinaryDocValuesField("dv", new BytesRef());
            break;
        case SORTED:
            f = new SortedDocValuesField("dv", new BytesRef());
            break;
        case NUMERIC:
            f = new NumericDocValuesField("dv", 0);
            break;
        case SORTED_NUMERIC:
            f = new SortedNumericDocValuesField("dv", 0);
            break;
        default:
            throw new AssertionError();
    }
    Document document = new Document();
    document.add(id);
    document.add(f);
    final Object[] vals = new Object[nDocs];
    RandomIndexWriter iw = new RandomIndexWriter(random(), d, iwConfig);
    for (int i = 0; i < nDocs; ++i) {
        id.setLongValue(i);
        switch(type) {
            case SORTED:
            case BINARY:
                do {
                    vals[i] = TestUtil.randomSimpleString(random(), 20);
                } while (((String) vals[i]).isEmpty());
                f.setBytesValue(new BytesRef((String) vals[i]));
                break;
            case NUMERIC:
            case SORTED_NUMERIC:
                // keep it an int
                final int bitsPerValue = RandomNumbers.randomIntBetween(random(), 1, 31);
                vals[i] = (long) random().nextInt((int) PackedInts.maxValue(bitsPerValue));
                f.setLongValue((Long) vals[i]);
                break;
        }
        iw.addDocument(document);
        if (random().nextBoolean() && i % 10 == 9) {
            iw.commit();
        }
    }
    iw.close();
    DirectoryReader rd = DirectoryReader.open(d);
    for (LeafReaderContext leave : rd.leaves()) {
        final FunctionValues ids = new LongFieldSource("id").getValues(null, leave);
        final ValueSource vs;
        switch(type) {
            case BINARY:
            case SORTED:
                vs = new BytesRefFieldSource("dv");
                break;
            case NUMERIC:
                vs = new LongFieldSource("dv");
                break;
            case SORTED_NUMERIC:
                // Since we are not indexing multiple values, MIN and MAX should work the same way
                vs = random().nextBoolean() ? new MultiValuedLongFieldSource("dv", Type.MIN) : new MultiValuedLongFieldSource("dv", Type.MAX);
                break;
            default:
                throw new AssertionError();
        }
        final FunctionValues values = vs.getValues(null, leave);
        BytesRefBuilder bytes = new BytesRefBuilder();
        for (int i = 0; i < leave.reader().maxDoc(); ++i) {
            assertTrue(values.exists(i));
            if (vs instanceof BytesRefFieldSource) {
                assertTrue(values.objectVal(i) instanceof String);
            } else if (vs instanceof LongFieldSource) {
                assertTrue(values.objectVal(i) instanceof Long);
                assertTrue(values.bytesVal(i, bytes));
            } else {
                throw new AssertionError();
            }
            Object expected = vals[ids.intVal(i)];
            switch(type) {
                case SORTED:
                    // no exception
                    values.ordVal(i);
                    assertTrue(values.numOrd() >= 1);
                // fall-through
                case BINARY:
                    assertEquals(expected, values.objectVal(i));
                    assertEquals(expected, values.strVal(i));
                    assertEquals(expected, values.objectVal(i));
                    assertEquals(expected, values.strVal(i));
                    assertTrue(values.bytesVal(i, bytes));
                    assertEquals(new BytesRef((String) expected), bytes.get());
                    break;
                case NUMERIC:
                case SORTED_NUMERIC:
                    assertEquals(((Number) expected).longValue(), values.longVal(i));
                    break;
            }
        }
    }
    rd.close();
    d.close();
}
Also used : BytesRefFieldSource(org.apache.lucene.queries.function.valuesource.BytesRefFieldSource) Document(org.apache.lucene.document.Document) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) Field(org.apache.lucene.document.Field) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory) MultiValuedLongFieldSource(org.apache.lucene.queries.function.valuesource.MultiValuedLongFieldSource) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) DirectoryReader(org.apache.lucene.index.DirectoryReader) LongFieldSource(org.apache.lucene.queries.function.valuesource.LongFieldSource) MultiValuedLongFieldSource(org.apache.lucene.queries.function.valuesource.MultiValuedLongFieldSource) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 74 with BinaryDocValuesField

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

the class BaseDocValuesFormatTestCase method testTwoBinaryValues.

public void testTwoBinaryValues() throws IOException {
    Directory directory = newDirectory();
    RandomIndexWriter iwriter = new RandomIndexWriter(random(), directory);
    Document doc = new Document();
    String longTerm = "longtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongterm";
    String text = "This is the text to be indexed. " + longTerm;
    doc.add(newTextField("fieldname", text, Field.Store.YES));
    doc.add(new BinaryDocValuesField("dv1", new BytesRef(longTerm)));
    doc.add(new BinaryDocValuesField("dv2", new BytesRef(text)));
    iwriter.addDocument(doc);
    iwriter.close();
    // Now search the index:
    // read-only=true
    IndexReader ireader = DirectoryReader.open(directory);
    IndexSearcher isearcher = new IndexSearcher(ireader);
    assertEquals(1, isearcher.search(new TermQuery(new Term("fieldname", longTerm)), 1).totalHits);
    Query query = new TermQuery(new Term("fieldname", "text"));
    TopDocs hits = isearcher.search(query, 1);
    assertEquals(1, hits.totalHits);
    // Iterate through the results:
    for (int i = 0; i < hits.scoreDocs.length; i++) {
        int hitDocID = hits.scoreDocs[i].doc;
        Document hitDoc = isearcher.doc(hitDocID);
        assertEquals(text, hitDoc.get("fieldname"));
        assert ireader.leaves().size() == 1;
        BinaryDocValues dv = ireader.leaves().get(0).reader().getBinaryDocValues("dv1");
        assertEquals(hitDocID, dv.advance(hitDocID));
        BytesRef scratch = dv.binaryValue();
        assertEquals(new BytesRef(longTerm), scratch);
        dv = ireader.leaves().get(0).reader().getBinaryDocValues("dv2");
        assertEquals(hitDocID, dv.advance(hitDocID));
        scratch = dv.binaryValue();
        assertEquals(new BytesRef(text), scratch);
    }
    ireader.close();
    directory.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Document(org.apache.lucene.document.Document) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) TopDocs(org.apache.lucene.search.TopDocs) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

Example 75 with BinaryDocValuesField

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

the class BaseDocValuesFormatTestCase method testBytesTwoDocumentsMerged.

public void testBytesTwoDocumentsMerged() throws IOException {
    Analyzer analyzer = new MockAnalyzer(random());
    Directory directory = newDirectory();
    IndexWriterConfig conf = newIndexWriterConfig(analyzer);
    conf.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter iwriter = new RandomIndexWriter(random(), directory, conf);
    Document doc = new Document();
    doc.add(newField("id", "0", StringField.TYPE_STORED));
    doc.add(new BinaryDocValuesField("dv", new BytesRef("hello world 1")));
    iwriter.addDocument(doc);
    iwriter.commit();
    doc = new Document();
    doc.add(newField("id", "1", StringField.TYPE_STORED));
    doc.add(new BinaryDocValuesField("dv", new BytesRef("hello 2")));
    iwriter.addDocument(doc);
    iwriter.forceMerge(1);
    iwriter.close();
    // Now search the index:
    // read-only=true
    IndexReader ireader = DirectoryReader.open(directory);
    assert ireader.leaves().size() == 1;
    BinaryDocValues dv = ireader.leaves().get(0).reader().getBinaryDocValues("dv");
    for (int i = 0; i < 2; i++) {
        Document doc2 = ireader.leaves().get(0).reader().document(i);
        String expected;
        if (doc2.get("id").equals("0")) {
            expected = "hello world 1";
        } else {
            expected = "hello 2";
        }
        assertEquals(i, dv.nextDoc());
        assertEquals(expected, dv.binaryValue().utf8ToString());
    }
    ireader.close();
    directory.close();
}
Also used : MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) Document(org.apache.lucene.document.Document) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

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