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