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));
}
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();
}
}
Aggregations