Search in sources :

Example 1 with SumFloatFunction

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);
}
Also used : SumTotalTermFreqValueSource(org.apache.lucene.queries.function.valuesource.SumTotalTermFreqValueSource) DoubleConstValueSource(org.apache.lucene.queries.function.valuesource.DoubleConstValueSource) ConstValueSource(org.apache.lucene.queries.function.valuesource.ConstValueSource) QueryValueSource(org.apache.lucene.queries.function.valuesource.QueryValueSource) DocFreqValueSource(org.apache.lucene.queries.function.valuesource.DocFreqValueSource) NormValueSource(org.apache.lucene.queries.function.valuesource.NormValueSource) NumDocsValueSource(org.apache.lucene.queries.function.valuesource.NumDocsValueSource) MaxDocValueSource(org.apache.lucene.queries.function.valuesource.MaxDocValueSource) JoinDocFreqValueSource(org.apache.lucene.queries.function.valuesource.JoinDocFreqValueSource) LiteralValueSource(org.apache.lucene.queries.function.valuesource.LiteralValueSource) TotalTermFreqValueSource(org.apache.lucene.queries.function.valuesource.TotalTermFreqValueSource) IDFValueSource(org.apache.lucene.queries.function.valuesource.IDFValueSource) TermFreqValueSource(org.apache.lucene.queries.function.valuesource.TermFreqValueSource) TFValueSource(org.apache.lucene.queries.function.valuesource.TFValueSource) SumFloatFunction(org.apache.lucene.queries.function.valuesource.SumFloatFunction) DoubleConstValueSource(org.apache.lucene.queries.function.valuesource.DoubleConstValueSource) ConstValueSource(org.apache.lucene.queries.function.valuesource.ConstValueSource)

Example 2 with SumFloatFunction

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)) ?
}
Also used : RangeMapFloatFunction(org.apache.lucene.queries.function.valuesource.RangeMapFloatFunction) FloatFieldSource(org.apache.lucene.queries.function.valuesource.FloatFieldSource) MultiValuedFloatFieldSource(org.apache.lucene.queries.function.valuesource.MultiValuedFloatFieldSource) SumTotalTermFreqValueSource(org.apache.lucene.queries.function.valuesource.SumTotalTermFreqValueSource) DoubleConstValueSource(org.apache.lucene.queries.function.valuesource.DoubleConstValueSource) ConstValueSource(org.apache.lucene.queries.function.valuesource.ConstValueSource) QueryValueSource(org.apache.lucene.queries.function.valuesource.QueryValueSource) DocFreqValueSource(org.apache.lucene.queries.function.valuesource.DocFreqValueSource) NormValueSource(org.apache.lucene.queries.function.valuesource.NormValueSource) NumDocsValueSource(org.apache.lucene.queries.function.valuesource.NumDocsValueSource) MaxDocValueSource(org.apache.lucene.queries.function.valuesource.MaxDocValueSource) JoinDocFreqValueSource(org.apache.lucene.queries.function.valuesource.JoinDocFreqValueSource) LiteralValueSource(org.apache.lucene.queries.function.valuesource.LiteralValueSource) TotalTermFreqValueSource(org.apache.lucene.queries.function.valuesource.TotalTermFreqValueSource) IDFValueSource(org.apache.lucene.queries.function.valuesource.IDFValueSource) TermFreqValueSource(org.apache.lucene.queries.function.valuesource.TermFreqValueSource) TFValueSource(org.apache.lucene.queries.function.valuesource.TFValueSource) SumFloatFunction(org.apache.lucene.queries.function.valuesource.SumFloatFunction) DoubleConstValueSource(org.apache.lucene.queries.function.valuesource.DoubleConstValueSource) ConstValueSource(org.apache.lucene.queries.function.valuesource.ConstValueSource)

Example 3 with SumFloatFunction

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();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) FieldDoc(org.apache.lucene.search.FieldDoc) SortField(org.apache.lucene.search.SortField) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) ScoreDoc(org.apache.lucene.search.ScoreDoc) DoubleConstValueSource(org.apache.lucene.queries.function.valuesource.DoubleConstValueSource) TopDocs(org.apache.lucene.search.TopDocs) StoredField(org.apache.lucene.document.StoredField) SortField(org.apache.lucene.search.SortField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) Field(org.apache.lucene.document.Field) StoredField(org.apache.lucene.document.StoredField) IntFieldSource(org.apache.lucene.queries.function.valuesource.IntFieldSource) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) DoubleConstValueSource(org.apache.lucene.queries.function.valuesource.DoubleConstValueSource) IndexReader(org.apache.lucene.index.IndexReader) SumFloatFunction(org.apache.lucene.queries.function.valuesource.SumFloatFunction) Sort(org.apache.lucene.search.Sort) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Aggregations

DoubleConstValueSource (org.apache.lucene.queries.function.valuesource.DoubleConstValueSource)3 SumFloatFunction (org.apache.lucene.queries.function.valuesource.SumFloatFunction)3 ConstValueSource (org.apache.lucene.queries.function.valuesource.ConstValueSource)2 DocFreqValueSource (org.apache.lucene.queries.function.valuesource.DocFreqValueSource)2 IDFValueSource (org.apache.lucene.queries.function.valuesource.IDFValueSource)2 JoinDocFreqValueSource (org.apache.lucene.queries.function.valuesource.JoinDocFreqValueSource)2 LiteralValueSource (org.apache.lucene.queries.function.valuesource.LiteralValueSource)2 MaxDocValueSource (org.apache.lucene.queries.function.valuesource.MaxDocValueSource)2 NormValueSource (org.apache.lucene.queries.function.valuesource.NormValueSource)2 NumDocsValueSource (org.apache.lucene.queries.function.valuesource.NumDocsValueSource)2 QueryValueSource (org.apache.lucene.queries.function.valuesource.QueryValueSource)2 SumTotalTermFreqValueSource (org.apache.lucene.queries.function.valuesource.SumTotalTermFreqValueSource)2 TFValueSource (org.apache.lucene.queries.function.valuesource.TFValueSource)2 TermFreqValueSource (org.apache.lucene.queries.function.valuesource.TermFreqValueSource)2 TotalTermFreqValueSource (org.apache.lucene.queries.function.valuesource.TotalTermFreqValueSource)2 Document (org.apache.lucene.document.Document)1 Field (org.apache.lucene.document.Field)1 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)1 StoredField (org.apache.lucene.document.StoredField)1 IndexReader (org.apache.lucene.index.IndexReader)1