use of org.apache.lucene.document.SortedNumericDocValuesField 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.SortedNumericDocValuesField in project lucene-solr by apache.
the class FunctionTestSetup method addDoc.
private static void addDoc(RandomIndexWriter iw, int i) throws Exception {
Document d = new Document();
Field f;
int scoreAndID = i + 1;
FieldType customType = new FieldType(TextField.TYPE_STORED);
customType.setTokenized(false);
customType.setOmitNorms(true);
// for debug purposes
f = newField(ID_FIELD, id2String(scoreAndID), customType);
d.add(f);
d.add(new SortedDocValuesField(ID_FIELD, new BytesRef(id2String(scoreAndID))));
FieldType customType2 = new FieldType(TextField.TYPE_NOT_STORED);
customType2.setOmitNorms(true);
// for regular search
f = newField(TEXT_FIELD, "text of doc" + scoreAndID + textLine(i), customType2);
d.add(f);
// for function scoring
f = new StoredField(INT_FIELD, scoreAndID);
d.add(f);
d.add(new NumericDocValuesField(INT_FIELD, scoreAndID));
// for function scoring
f = new StoredField(FLOAT_FIELD, scoreAndID);
d.add(f);
d.add(new NumericDocValuesField(FLOAT_FIELD, Float.floatToRawIntBits(scoreAndID)));
f = new StoredField(INT_FIELD_MV_MIN, scoreAndID);
d.add(f);
f = new StoredField(INT_FIELD_MV_MIN, scoreAndID + 1);
d.add(f);
d.add(new SortedNumericDocValuesField(INT_FIELD_MV_MIN, scoreAndID));
d.add(new SortedNumericDocValuesField(INT_FIELD_MV_MIN, scoreAndID + 1));
f = new StoredField(INT_FIELD_MV_MAX, scoreAndID);
d.add(f);
f = new StoredField(INT_FIELD_MV_MAX, scoreAndID - 1);
d.add(f);
d.add(new SortedNumericDocValuesField(INT_FIELD_MV_MAX, scoreAndID));
d.add(new SortedNumericDocValuesField(INT_FIELD_MV_MAX, scoreAndID - 1));
f = new StoredField(FLOAT_FIELD_MV_MIN, scoreAndID);
d.add(f);
f = new StoredField(FLOAT_FIELD_MV_MIN, scoreAndID + 1);
d.add(f);
d.add(new SortedNumericDocValuesField(FLOAT_FIELD_MV_MIN, NumericUtils.floatToSortableInt(scoreAndID)));
d.add(new SortedNumericDocValuesField(FLOAT_FIELD_MV_MIN, NumericUtils.floatToSortableInt(scoreAndID + 1)));
f = new StoredField(FLOAT_FIELD_MV_MAX, scoreAndID);
d.add(f);
f = new StoredField(FLOAT_FIELD_MV_MAX, scoreAndID - 1);
d.add(f);
d.add(new SortedNumericDocValuesField(FLOAT_FIELD_MV_MAX, NumericUtils.floatToSortableInt(scoreAndID)));
d.add(new SortedNumericDocValuesField(FLOAT_FIELD_MV_MAX, NumericUtils.floatToSortableInt(scoreAndID - 1)));
iw.addDocument(d);
log("added: " + d);
}
use of org.apache.lucene.document.SortedNumericDocValuesField in project lucene-solr by apache.
the class BaseDocValuesFormatTestCase method testOneSortedNumber.
public void testOneSortedNumber() throws IOException {
Directory directory = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), directory);
Document doc = new Document();
doc.add(new SortedNumericDocValuesField("dv", 5));
writer.addDocument(doc);
writer.close();
// Now search the index:
IndexReader reader = DirectoryReader.open(directory);
assert reader.leaves().size() == 1;
SortedNumericDocValues dv = reader.leaves().get(0).reader().getSortedNumericDocValues("dv");
assertEquals(0, dv.nextDoc());
assertEquals(1, dv.docValueCount());
assertEquals(5, dv.nextValue());
reader.close();
directory.close();
}
use of org.apache.lucene.document.SortedNumericDocValuesField in project lucene-solr by apache.
the class BaseDocValuesFormatTestCase method testTwoSortedNumberOneMissing.
public void testTwoSortedNumberOneMissing() throws IOException {
Directory directory = newDirectory();
IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig(null));
Document doc = new Document();
doc.add(new SortedNumericDocValuesField("dv", 11));
doc.add(new SortedNumericDocValuesField("dv", -5));
writer.addDocument(doc);
writer.addDocument(new Document());
writer.close();
// Now search the index:
IndexReader reader = DirectoryReader.open(directory);
assert reader.leaves().size() == 1;
SortedNumericDocValues dv = reader.leaves().get(0).reader().getSortedNumericDocValues("dv");
assertEquals(0, dv.nextDoc());
assertEquals(2, dv.docValueCount());
assertEquals(-5, dv.nextValue());
assertEquals(11, dv.nextValue());
assertEquals(NO_MORE_DOCS, dv.nextDoc());
reader.close();
directory.close();
}
use of org.apache.lucene.document.SortedNumericDocValuesField in project lucene-solr by apache.
the class BaseDocValuesFormatTestCase method addRandomFields.
@Override
protected void addRandomFields(Document doc) {
if (usually()) {
doc.add(new NumericDocValuesField("ndv", random().nextInt(1 << 12)));
doc.add(new BinaryDocValuesField("bdv", new BytesRef(TestUtil.randomSimpleString(random()))));
doc.add(new SortedDocValuesField("sdv", new BytesRef(TestUtil.randomSimpleString(random(), 2))));
}
int numValues = random().nextInt(5);
for (int i = 0; i < numValues; ++i) {
doc.add(new SortedSetDocValuesField("ssdv", new BytesRef(TestUtil.randomSimpleString(random(), 2))));
}
numValues = random().nextInt(5);
for (int i = 0; i < numValues; ++i) {
doc.add(new SortedNumericDocValuesField("sndv", TestUtil.nextLong(random(), Long.MIN_VALUE, Long.MAX_VALUE)));
}
}
Aggregations