use of org.apache.lucene.document.IntPoint in project lucene-solr by apache.
the class TestJoinUtil method testEquals_numericJoin.
public void testEquals_numericJoin() throws Exception {
final int numDocs = atLeast(random(), 50);
try (final Directory dir = newDirectory()) {
try (final RandomIndexWriter w = new RandomIndexWriter(random(), dir, newIndexWriterConfig(new MockAnalyzer(random())).setMergePolicy(newLogMergePolicy()))) {
boolean multiValued = random().nextBoolean();
String joinField = multiValued ? "mvField" : "svField";
for (int id = 0; id < numDocs; id++) {
Document doc = new Document();
doc.add(new TextField("id", "" + id, Field.Store.NO));
doc.add(new TextField("name", "name" + (id % 7), Field.Store.NO));
if (multiValued) {
int numValues = 1 + random().nextInt(2);
for (int i = 0; i < numValues; i++) {
doc.add(new IntPoint(joinField, random().nextInt(13)));
doc.add(new SortedNumericDocValuesField(joinField, random().nextInt(13)));
}
} else {
doc.add(new IntPoint(joinField, random().nextInt(13)));
doc.add(new NumericDocValuesField(joinField, random().nextInt(13)));
}
w.addDocument(doc);
}
Set<ScoreMode> scoreModes = EnumSet.allOf(ScoreMode.class);
ScoreMode scoreMode1 = scoreModes.toArray(new ScoreMode[0])[random().nextInt(scoreModes.size())];
scoreModes.remove(scoreMode1);
ScoreMode scoreMode2 = scoreModes.toArray(new ScoreMode[0])[random().nextInt(scoreModes.size())];
final Query x;
try (IndexReader r = w.getReader()) {
IndexSearcher indexSearcher = new IndexSearcher(r);
x = JoinUtil.createJoinQuery(joinField, multiValued, joinField, Integer.class, new TermQuery(new Term("name", "name5")), indexSearcher, scoreMode1);
assertEquals("identical calls to createJoinQuery", x, JoinUtil.createJoinQuery(joinField, multiValued, joinField, Integer.class, new TermQuery(new Term("name", "name5")), indexSearcher, scoreMode1));
assertFalse("score mode (" + scoreMode1 + " != " + scoreMode2 + "), but queries are equal", x.equals(JoinUtil.createJoinQuery(joinField, multiValued, joinField, Integer.class, new TermQuery(new Term("name", "name5")), indexSearcher, scoreMode2)));
assertFalse("from fields (joinField != \"other_field\") but queries equals", x.equals(JoinUtil.createJoinQuery(joinField, multiValued, "other_field", Integer.class, new TermQuery(new Term("name", "name5")), indexSearcher, scoreMode1)));
assertFalse("from fields (\"other_field\" != joinField) but queries equals", x.equals(JoinUtil.createJoinQuery("other_field", multiValued, joinField, Integer.class, new TermQuery(new Term("name", "name5")), indexSearcher, scoreMode1)));
assertFalse("fromQuery (name:name5 != name:name6) but queries equals", x.equals(JoinUtil.createJoinQuery("other_field", multiValued, joinField, Integer.class, new TermQuery(new Term("name", "name6")), indexSearcher, scoreMode1)));
}
for (int i = 14; i < 26; i++) {
Document doc = new Document();
doc.add(new TextField("id", "new_id", Field.Store.NO));
doc.add(new TextField("name", "name5", Field.Store.NO));
if (multiValued) {
int numValues = 1 + random().nextInt(2);
for (int j = 0; j < numValues; j++) {
doc.add(new SortedNumericDocValuesField(joinField, i));
doc.add(new IntPoint(joinField, i));
}
} else {
doc.add(new NumericDocValuesField(joinField, i));
doc.add(new IntPoint(joinField, i));
}
w.addDocument(doc);
}
try (IndexReader r = w.getReader()) {
IndexSearcher indexSearcher = new IndexSearcher(r);
assertFalse("Query shouldn't be equal, because new join values have been indexed", x.equals(JoinUtil.createJoinQuery(joinField, multiValued, joinField, Integer.class, new TermQuery(new Term("name", "name5")), indexSearcher, scoreMode1)));
}
}
}
}
use of org.apache.lucene.document.IntPoint in project lucene-solr by apache.
the class TestMemoryIndexAgainstRAMDir method testPointValuesMemoryIndexVsNormalIndex.
public void testPointValuesMemoryIndexVsNormalIndex() throws Exception {
int size = atLeast(12);
List<Integer> randomValues = new ArrayList<>();
Document doc = new Document();
for (Integer randomInteger : random().ints(size).toArray()) {
doc.add(new IntPoint("int", randomInteger));
randomValues.add(randomInteger);
doc.add(new LongPoint("long", randomInteger));
doc.add(new FloatPoint("float", randomInteger));
doc.add(new DoublePoint("double", randomInteger));
}
MockAnalyzer mockAnalyzer = new MockAnalyzer(random());
MemoryIndex memoryIndex = MemoryIndex.fromDocument(doc, mockAnalyzer);
IndexSearcher memoryIndexSearcher = memoryIndex.createSearcher();
Directory dir = newDirectory();
IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(random(), mockAnalyzer));
writer.addDocument(doc);
writer.close();
IndexReader controlIndexReader = DirectoryReader.open(dir);
IndexSearcher controlIndexSearcher = new IndexSearcher(controlIndexReader);
Supplier<Integer> valueSupplier = () -> randomValues.get(random().nextInt(randomValues.size()));
Query[] queries = new Query[] { IntPoint.newExactQuery("int", valueSupplier.get()), LongPoint.newExactQuery("long", valueSupplier.get()), FloatPoint.newExactQuery("float", valueSupplier.get()), DoublePoint.newExactQuery("double", valueSupplier.get()), IntPoint.newSetQuery("int", valueSupplier.get(), valueSupplier.get()), LongPoint.newSetQuery("long", valueSupplier.get(), valueSupplier.get()), FloatPoint.newSetQuery("float", valueSupplier.get(), valueSupplier.get()), DoublePoint.newSetQuery("double", valueSupplier.get(), valueSupplier.get()), IntPoint.newRangeQuery("int", valueSupplier.get(), valueSupplier.get()), LongPoint.newRangeQuery("long", valueSupplier.get(), valueSupplier.get()), FloatPoint.newRangeQuery("float", valueSupplier.get(), valueSupplier.get()), DoublePoint.newRangeQuery("double", valueSupplier.get(), valueSupplier.get()) };
for (Query query : queries) {
assertEquals(controlIndexSearcher.count(query), controlIndexSearcher.count(query));
}
memoryIndexSearcher.getIndexReader().close();
controlIndexReader.close();
dir.close();
}
use of org.apache.lucene.document.IntPoint in project lucene-solr by apache.
the class TestMemoryIndex method testPointValues.
public void testPointValues() throws Exception {
List<Function<Long, IndexableField>> fieldFunctions = Arrays.asList((t) -> new IntPoint("number", t.intValue()), (t) -> new LongPoint("number", t), (t) -> new FloatPoint("number", t.floatValue()), (t) -> new DoublePoint("number", t.doubleValue()));
List<Function<Long, Query>> exactQueryFunctions = Arrays.asList((t) -> IntPoint.newExactQuery("number", t.intValue()), (t) -> LongPoint.newExactQuery("number", t), (t) -> FloatPoint.newExactQuery("number", t.floatValue()), (t) -> DoublePoint.newExactQuery("number", t.doubleValue()));
List<Function<long[], Query>> setQueryFunctions = Arrays.asList((t) -> IntPoint.newSetQuery("number", LongStream.of(t).mapToInt(value -> (int) value).toArray()), (t) -> LongPoint.newSetQuery("number", t), (t) -> FloatPoint.newSetQuery("number", Arrays.asList(LongStream.of(t).mapToObj(value -> (float) value).toArray(Float[]::new))), (t) -> DoublePoint.newSetQuery("number", LongStream.of(t).mapToDouble(value -> (double) value).toArray()));
List<BiFunction<Long, Long, Query>> rangeQueryFunctions = Arrays.asList((t, u) -> IntPoint.newRangeQuery("number", t.intValue(), u.intValue()), (t, u) -> LongPoint.newRangeQuery("number", t, u), (t, u) -> FloatPoint.newRangeQuery("number", t.floatValue(), u.floatValue()), (t, u) -> DoublePoint.newRangeQuery("number", t.doubleValue(), u.doubleValue()));
for (int i = 0; i < fieldFunctions.size(); i++) {
Function<Long, IndexableField> fieldFunction = fieldFunctions.get(i);
Function<Long, Query> exactQueryFunction = exactQueryFunctions.get(i);
Function<long[], Query> setQueryFunction = setQueryFunctions.get(i);
BiFunction<Long, Long, Query> rangeQueryFunction = rangeQueryFunctions.get(i);
Document doc = new Document();
for (int number = 1; number < 32; number += 2) {
doc.add(fieldFunction.apply((long) number));
}
MemoryIndex mi = MemoryIndex.fromDocument(doc, analyzer);
IndexSearcher indexSearcher = mi.createSearcher();
Query query = exactQueryFunction.apply(5L);
assertEquals(1, indexSearcher.count(query));
query = exactQueryFunction.apply(4L);
assertEquals(0, indexSearcher.count(query));
query = setQueryFunction.apply(new long[] { 3L, 9L, 19L });
assertEquals(1, indexSearcher.count(query));
query = setQueryFunction.apply(new long[] { 2L, 8L, 13L });
assertEquals(1, indexSearcher.count(query));
query = setQueryFunction.apply(new long[] { 2L, 8L, 16L });
assertEquals(0, indexSearcher.count(query));
query = rangeQueryFunction.apply(2L, 16L);
assertEquals(1, indexSearcher.count(query));
query = rangeQueryFunction.apply(24L, 48L);
assertEquals(1, indexSearcher.count(query));
query = rangeQueryFunction.apply(48L, 68L);
assertEquals(0, indexSearcher.count(query));
}
}
use of org.apache.lucene.document.IntPoint in project lucene-solr by apache.
the class TestMemoryIndex method test2DPoints.
public void test2DPoints() throws Exception {
Document doc = new Document();
doc.add(new IntPoint("ints", 0, -100));
doc.add(new IntPoint("ints", 20, 20));
doc.add(new IntPoint("ints", 100, -100));
doc.add(new LongPoint("longs", 0L, -100L));
doc.add(new LongPoint("longs", 20L, 20L));
doc.add(new LongPoint("longs", 100L, -100L));
doc.add(new FloatPoint("floats", 0F, -100F));
doc.add(new FloatPoint("floats", 20F, 20F));
doc.add(new FloatPoint("floats", 100F, -100F));
doc.add(new DoublePoint("doubles", 0D, -100D));
doc.add(new DoublePoint("doubles", 20D, 20D));
doc.add(new DoublePoint("doubles", 100D, -100D));
MemoryIndex mi = MemoryIndex.fromDocument(doc, analyzer);
IndexSearcher s = mi.createSearcher();
assertEquals(1, s.count(IntPoint.newRangeQuery("ints", new int[] { 10, 10 }, new int[] { 30, 30 })));
assertEquals(1, s.count(LongPoint.newRangeQuery("longs", new long[] { 10L, 10L }, new long[] { 30L, 30L })));
assertEquals(1, s.count(FloatPoint.newRangeQuery("floats", new float[] { 10F, 10F }, new float[] { 30F, 30F })));
assertEquals(1, s.count(DoublePoint.newRangeQuery("doubles", new double[] { 10D, 10D }, new double[] { 30D, 30D })));
}
use of org.apache.lucene.document.IntPoint in project lucene-solr by apache.
the class BaseStoredFieldsFormatTestCase method testNumericField.
public void testNumericField() throws Exception {
Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir);
final int numDocs = atLeast(500);
final Number[] answers = new Number[numDocs];
final Class<?>[] typeAnswers = new Class<?>[numDocs];
for (int id = 0; id < numDocs; id++) {
Document doc = new Document();
final Field nf;
final Number answer;
final Class<?> typeAnswer;
if (random().nextBoolean()) {
// float/double
if (random().nextBoolean()) {
final float f = random().nextFloat();
answer = Float.valueOf(f);
nf = new StoredField("nf", f);
typeAnswer = Float.class;
} else {
final double d = random().nextDouble();
answer = Double.valueOf(d);
nf = new StoredField("nf", d);
typeAnswer = Double.class;
}
} else {
// int/long
if (random().nextBoolean()) {
final int i = random().nextInt();
answer = Integer.valueOf(i);
nf = new StoredField("nf", i);
typeAnswer = Integer.class;
} else {
final long l = random().nextLong();
answer = Long.valueOf(l);
nf = new StoredField("nf", l);
typeAnswer = Long.class;
}
}
doc.add(nf);
answers[id] = answer;
typeAnswers[id] = typeAnswer;
doc.add(new StoredField("id", id));
doc.add(new IntPoint("id", id));
doc.add(new NumericDocValuesField("id", id));
w.addDocument(doc);
}
final DirectoryReader r = w.getReader();
w.close();
assertEquals(numDocs, r.numDocs());
for (LeafReaderContext ctx : r.leaves()) {
final LeafReader sub = ctx.reader();
final NumericDocValues ids = DocValues.getNumeric(sub, "id");
for (int docID = 0; docID < sub.numDocs(); docID++) {
final Document doc = sub.document(docID);
final Field f = (Field) doc.getField("nf");
assertTrue("got f=" + f, f instanceof StoredField);
assertEquals(docID, ids.nextDoc());
assertEquals(answers[(int) ids.longValue()], f.numericValue());
}
}
r.close();
dir.close();
}
Aggregations