Search in sources :

Example 1 with FunctionScoreQuery

use of org.apache.lucene.queries.function.FunctionScoreQuery in project zuliasearch by zuliaio.

the class ZuliaIndex method handleScoreFunction.

private FunctionScoreQuery handleScoreFunction(String scoreFunction, Query query) throws java.text.ParseException {
    SimpleBindings bindings = new SimpleBindings();
    Expression expr = JavascriptCompiler.compile(scoreFunction);
    bindings.add("score", DoubleValuesSource.SCORES);
    for (String var : expr.variables) {
        if (!"score".equals(var)) {
            FieldConfig.FieldType fieldType = indexConfig.getFieldTypeForSortField(var);
            if (fieldType == null) {
                throw new IllegalArgumentException("Score Function references unknown sort field <" + var + ">");
            }
            if (fieldType == FieldConfig.FieldType.NUMERIC_INT) {
                bindings.add(var, DoubleValuesSource.fromIntField(var));
            } else if (fieldType == FieldConfig.FieldType.NUMERIC_LONG) {
                bindings.add(var, DoubleValuesSource.fromLongField(var));
            } else if (fieldType == FieldConfig.FieldType.NUMERIC_FLOAT) {
                bindings.add(var, DoubleValuesSource.fromFloatField(var));
            } else if (fieldType == FieldConfig.FieldType.NUMERIC_DOUBLE) {
                bindings.add(var, DoubleValuesSource.fromDoubleField(var));
            } else if (fieldType == FieldConfig.FieldType.DATE) {
                bindings.add(var, DoubleValuesSource.fromLongField(var));
            } else {
                throw new IllegalArgumentException("Score Function references sort field with non numeric or date type <" + var + ">");
            }
        }
    // 
    }
    return new FunctionScoreQuery(query, expr.getDoubleValuesSource(bindings));
}
Also used : Expression(org.apache.lucene.expressions.Expression) FunctionScoreQuery(org.apache.lucene.queries.function.FunctionScoreQuery) SimpleBindings(org.apache.lucene.expressions.SimpleBindings) FieldConfig(io.zulia.message.ZuliaIndex.FieldConfig)

Example 2 with FunctionScoreQuery

use of org.apache.lucene.queries.function.FunctionScoreQuery in project java-basic by tzuyichao.

the class TestFunctionScoreQuery method main.

public static void main(String[] args) {
    try {
        Directory directory = DataConstructor.constructSimpleDataset("/tmp/lucene");
        IndexReader indexReader = DirectoryReader.open(directory);
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        FunctionScoreQuery functionScoreQuery = FunctionScoreQuery.boostByQuery(new TermQuery(new Term(FIELD_CONTENT, "humpty")), new TermQuery(new Term(FIELD_CONTENT, "together")), 100f);
        TopDocs topDocs = indexSearcher.search(functionScoreQuery, 100);
        SimpleDatasetUtil.printQueryResult(indexReader, topDocs);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) TermQuery(org.apache.lucene.search.TermQuery) FunctionScoreQuery(org.apache.lucene.queries.function.FunctionScoreQuery) IndexReader(org.apache.lucene.index.IndexReader) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) Directory(org.apache.lucene.store.Directory)

Aggregations

FunctionScoreQuery (org.apache.lucene.queries.function.FunctionScoreQuery)2 FieldConfig (io.zulia.message.ZuliaIndex.FieldConfig)1 IOException (java.io.IOException)1 Expression (org.apache.lucene.expressions.Expression)1 SimpleBindings (org.apache.lucene.expressions.SimpleBindings)1 IndexReader (org.apache.lucene.index.IndexReader)1 Term (org.apache.lucene.index.Term)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 TermQuery (org.apache.lucene.search.TermQuery)1 TopDocs (org.apache.lucene.search.TopDocs)1 Directory (org.apache.lucene.store.Directory)1