use of org.apache.lucene.document.SortedNumericDocValuesField in project lucene-solr by apache.
the class TestIndexSorting method testBasicMultiValuedLong.
public void testBasicMultiValuedLong() throws Exception {
Directory dir = newDirectory();
IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
Sort indexSort = new Sort(new SortedNumericSortField("foo", SortField.Type.LONG));
iwc.setIndexSort(indexSort);
IndexWriter w = new IndexWriter(dir, iwc);
Document doc = new Document();
doc.add(new NumericDocValuesField("id", 3));
doc.add(new SortedNumericDocValuesField("foo", 18));
doc.add(new SortedNumericDocValuesField("foo", 35));
w.addDocument(doc);
// so we get more than one segment, so that forceMerge actually does merge, since we only get a sorted segment by merging:
w.commit();
doc = new Document();
doc.add(new NumericDocValuesField("id", 1));
doc.add(new SortedNumericDocValuesField("foo", -1));
w.addDocument(doc);
w.commit();
doc = new Document();
doc.add(new NumericDocValuesField("id", 2));
doc.add(new SortedNumericDocValuesField("foo", 7));
doc.add(new SortedNumericDocValuesField("foo", 22));
w.addDocument(doc);
w.forceMerge(1);
DirectoryReader r = DirectoryReader.open(w);
LeafReader leaf = getOnlyLeafReader(r);
assertEquals(3, leaf.maxDoc());
NumericDocValues values = leaf.getNumericDocValues("id");
assertEquals(0, values.nextDoc());
assertEquals(1, values.longValue());
assertEquals(1, values.nextDoc());
assertEquals(2, values.longValue());
assertEquals(2, values.nextDoc());
assertEquals(3, values.longValue());
r.close();
w.close();
dir.close();
}
use of org.apache.lucene.document.SortedNumericDocValuesField in project lucene-solr by apache.
the class TestIndexSorting method testMultiValuedNumericAlreadySorted.
public void testMultiValuedNumericAlreadySorted() throws Exception {
assertNeedsIndexSortMerge(new SortedNumericSortField("foo", SortField.Type.INT), (doc) -> {
doc.add(new SortedNumericDocValuesField("foo", Integer.MIN_VALUE));
int num = random().nextInt(5);
for (int j = 0; j < num; j++) {
doc.add(new SortedNumericDocValuesField("foo", random().nextInt()));
}
}, (doc) -> {
int num = random().nextInt(5);
for (int j = 0; j < num; j++) {
doc.add(new SortedNumericDocValuesField("foo", random().nextInt()));
}
});
}
use of org.apache.lucene.document.SortedNumericDocValuesField in project lucene-solr by apache.
the class TestJoinUtil method addLinkFields.
private void addLinkFields(final Random random, Document document, final String fieldName, String linkValue, boolean multipleValuesPerDocument, boolean globalOrdinalJoin) {
document.add(newTextField(random, fieldName, linkValue, Field.Store.NO));
final int linkInt = Integer.parseUnsignedInt(linkValue, 16);
document.add(new IntPoint(fieldName + "INT", linkInt));
document.add(new FloatPoint(fieldName + "FLOAT", linkInt));
final long linkLong = linkInt << 32 | linkInt;
document.add(new LongPoint(fieldName + "LONG", linkLong));
document.add(new DoublePoint(fieldName + "DOUBLE", linkLong));
if (multipleValuesPerDocument) {
document.add(new SortedSetDocValuesField(fieldName, new BytesRef(linkValue)));
document.add(new SortedNumericDocValuesField(fieldName + "INT", linkInt));
document.add(new SortedNumericDocValuesField(fieldName + "FLOAT", Float.floatToRawIntBits(linkInt)));
document.add(new SortedNumericDocValuesField(fieldName + "LONG", linkLong));
document.add(new SortedNumericDocValuesField(fieldName + "DOUBLE", Double.doubleToRawLongBits(linkLong)));
} else {
document.add(new SortedDocValuesField(fieldName, new BytesRef(linkValue)));
document.add(new NumericDocValuesField(fieldName + "INT", linkInt));
document.add(new FloatDocValuesField(fieldName + "FLOAT", linkInt));
document.add(new NumericDocValuesField(fieldName + "LONG", linkLong));
document.add(new DoubleDocValuesField(fieldName + "DOUBLE", linkLong));
}
if (globalOrdinalJoin) {
document.add(new SortedDocValuesField("join_field", new BytesRef(linkValue)));
}
}
use of org.apache.lucene.document.SortedNumericDocValuesField in project lucene-solr by apache.
the class TestDocValuesNumbersQuery method testDuelTermsQuery.
public void testDuelTermsQuery() throws IOException {
final int iters = atLeast(2);
for (int iter = 0; iter < iters; ++iter) {
final List<Long> allNumbers = new ArrayList<>();
final int numNumbers = TestUtil.nextInt(random(), 1, 1 << TestUtil.nextInt(random(), 1, 10));
for (int i = 0; i < numNumbers; ++i) {
allNumbers.add(random().nextLong());
}
Directory dir = newDirectory();
RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
final int numDocs = atLeast(100);
for (int i = 0; i < numDocs; ++i) {
Document doc = new Document();
final Long number = allNumbers.get(random().nextInt(allNumbers.size()));
doc.add(new StringField("text", number.toString(), Store.NO));
doc.add(new NumericDocValuesField("long", number));
doc.add(new SortedNumericDocValuesField("twolongs", number));
doc.add(new SortedNumericDocValuesField("twolongs", number * 2));
iw.addDocument(doc);
}
if (numNumbers > 1 && random().nextBoolean()) {
iw.deleteDocuments(new TermQuery(new Term("text", allNumbers.get(0).toString())));
}
iw.commit();
final IndexReader reader = iw.getReader();
final IndexSearcher searcher = newSearcher(reader);
iw.close();
if (reader.numDocs() == 0) {
// may occasionally happen if all documents got the same term
IOUtils.close(reader, dir);
continue;
}
for (int i = 0; i < 100; ++i) {
final float boost = random().nextFloat() * 10;
final int numQueryNumbers = TestUtil.nextInt(random(), 1, 1 << TestUtil.nextInt(random(), 1, 8));
Set<Long> queryNumbers = new HashSet<>();
Set<Long> queryNumbersX2 = new HashSet<>();
for (int j = 0; j < numQueryNumbers; ++j) {
Long number = allNumbers.get(random().nextInt(allNumbers.size()));
queryNumbers.add(number);
queryNumbersX2.add(2 * number);
}
final BooleanQuery.Builder bq = new BooleanQuery.Builder();
for (Long number : queryNumbers) {
bq.add(new TermQuery(new Term("text", number.toString())), Occur.SHOULD);
}
Query q1 = new BoostQuery(new ConstantScoreQuery(bq.build()), boost);
Query q2 = new BoostQuery(new DocValuesNumbersQuery("long", queryNumbers), boost);
assertSameMatches(searcher, q1, q2, true);
Query q3 = new BoostQuery(new DocValuesNumbersQuery("twolongs", queryNumbers), boost);
assertSameMatches(searcher, q1, q3, true);
Query q4 = new BoostQuery(new DocValuesNumbersQuery("twolongs", queryNumbersX2), boost);
assertSameMatches(searcher, q1, q4, true);
}
reader.close();
dir.close();
}
}
use of org.apache.lucene.document.SortedNumericDocValuesField in project lucene-solr by apache.
the class TestPointQueries method testNumericNoValuesMatch.
public void testNumericNoValuesMatch() throws Exception {
Directory dir = newDirectory();
IndexWriterConfig iwc = newIndexWriterConfig();
iwc.setCodec(getCodec());
RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc);
Document doc = new Document();
doc.add(new SortedNumericDocValuesField("value", 17));
w.addDocument(doc);
doc = new Document();
doc.add(new SortedNumericDocValuesField("value", 22));
w.addDocument(doc);
IndexReader r = w.getReader();
IndexSearcher s = new IndexSearcher(r);
assertEquals(0, s.count(LongPoint.newRangeQuery("value", 17L, 13L)));
IOUtils.close(r, w, dir);
}
Aggregations