use of org.apache.lucene.queries.function.valuesource.SumFloatFunction in project lucene-solr by apache.
the class TestValueSources method testSumFloat.
public void testSumFloat() throws Exception {
ValueSource vs = new SumFloatFunction(new ValueSource[] { new ConstValueSource(1f), new ConstValueSource(2f) });
assertHits(new FunctionQuery(vs), new float[] { 3f, 3f });
assertAllExist(vs);
vs = new SumFloatFunction(new ValueSource[] { BOGUS_FLOAT_VS, new ConstValueSource(2f) });
assertNoneExist(vs);
}
use of org.apache.lucene.queries.function.valuesource.SumFloatFunction in project lucene-solr by apache.
the class TestValueSources method testRangeMap.
public void testRangeMap() throws Exception {
assertHits(new FunctionQuery(new RangeMapFloatFunction(new FloatFieldSource("float"), 5, 6, 1, 0f)), new float[] { 1f, 0f });
assertHits(new FunctionQuery(new RangeMapFloatFunction(new FloatFieldSource("float"), 5, 6, new SumFloatFunction(new ValueSource[] { new ConstValueSource(1f), new ConstValueSource(2f) }), new ConstValueSource(11f))), new float[] { 3f, 11f });
// TODO: what *should* the rules be for exist() ?
// ((source exists && source in range && target exists) OR (source not in range && default exists)) ?
}
use of org.apache.lucene.queries.function.valuesource.SumFloatFunction in project lucene-solr by apache.
the class TestFunctionQuerySort method testSearchAfterWhenSortingByFunctionValues.
public void testSearchAfterWhenSortingByFunctionValues() throws IOException {
Directory dir = newDirectory();
IndexWriterConfig iwc = newIndexWriterConfig(null);
// depends on docid order
iwc.setMergePolicy(newLogMergePolicy());
RandomIndexWriter writer = new RandomIndexWriter(random(), dir, iwc);
Document doc = new Document();
Field field = new StoredField("value", 0);
Field dvField = new NumericDocValuesField("value", 0);
doc.add(field);
doc.add(dvField);
// Save docs unsorted (decreasing value n, n-1, ...)
final int NUM_VALS = 5;
for (int val = NUM_VALS; val > 0; val--) {
field.setIntValue(val);
dvField.setLongValue(val);
writer.addDocument(doc);
}
// Open index
IndexReader reader = writer.getReader();
writer.close();
IndexSearcher searcher = newSearcher(reader);
// Trivial ValueSource function that bypasses single field ValueSource sort optimization
ValueSource src = new SumFloatFunction(new ValueSource[] { new IntFieldSource("value"), new DoubleConstValueSource(1.0D) });
// ...and make it a sort criterion
SortField sf = src.getSortField(false).rewrite(searcher);
Sort orderBy = new Sort(sf);
// Get hits sorted by our FunctionValues (ascending values)
Query q = new MatchAllDocsQuery();
TopDocs hits = searcher.search(q, reader.maxDoc(), orderBy);
assertEquals(NUM_VALS, hits.scoreDocs.length);
// Verify that sorting works in general
int i = 0;
for (ScoreDoc hit : hits.scoreDocs) {
int valueFromDoc = Integer.parseInt(reader.document(hit.doc).get("value"));
assertEquals(++i, valueFromDoc);
}
// Now get hits after hit #2 using IS.searchAfter()
int afterIdx = 1;
FieldDoc afterHit = (FieldDoc) hits.scoreDocs[afterIdx];
hits = searcher.searchAfter(afterHit, q, reader.maxDoc(), orderBy);
// Expected # of hits: NUM_VALS - 2
assertEquals(NUM_VALS - (afterIdx + 1), hits.scoreDocs.length);
// Verify that hits are actually "after"
int afterValue = ((Double) afterHit.fields[0]).intValue();
for (ScoreDoc hit : hits.scoreDocs) {
int val = Integer.parseInt(reader.document(hit.doc).get("value"));
assertTrue(afterValue <= val);
assertFalse(hit.doc == afterHit.doc);
}
reader.close();
dir.close();
}
Aggregations