如何在文本字段中使用FunctionScoreQuery?

时间:2018-06-20 16:11:20

标签: lucene scoring lucene-boosting

在Lucene 6.6.0中,不建议使用索引时间提升。而且,不建议使用CustomQueryScore。因此,Lucene开发人员的建议是使用FunctionScoreQuery。但是,我不明白如何使用FunctionScoreQuery来增强文本字段,因为它以DoubleValuesSource作为输入,这有助于增强数字字段(fromIntField,fromDoubleField等...)

3 个答案:

答案 0 :(得分:0)

我相信您误会了他们在那儿给的指导。您在docValues字段而不是文本中索引 boost 。将提升视为元领域。因此,您最终得到两个字段,例如:

  • 一个文本字段myField
  • 数字DocValues字段myFieldBoost

然后您将查询myField的内容,并将myFieldBoost的值分解为FunctionScoreQuery。

答案 1 :(得分:0)

搜索时提升费用是一种更好的解决方案,可以使用:

要基本了解评分方式,请锁定documentation

答案 2 :(得分:0)

可能为时已晚,但请查看此处的讨论:http://lucene.472066.n3.nabble.com/any-example-on-FunctionScoreQuery-since-Field-setBoost-is-deprecated-with-Lucene-6-6-0-td4400355.html,以及有关如何使用FunctionScoreQuery类的一些副本:

基于表达式排序的示例:

// compile an expression:
Expression expr = JavascriptCompiler.compile("sqrt(_score) + ln(popularity)");

// SimpleBindings just maps variables to SortField instances
SimpleBindings bindings = new SimpleBindings();
bindings.add(new SortField("_score", SortField.Type.SCORE));
bindings.add(new SortField("popularity", SortField.Type.INT));

// create a sort field and sort by it (reverse order)
Sort sort = new Sort(expr.getSortField(bindings, true));
Query query = new TermQuery(new Term("body", "contents"));
searcher.search(query, 10, sort);

修改查询所产生分数的示例:

// compile an expression:
Expression expr = JavascriptCompiler.compile("sqrt(_score) + ln(popularity)");

// SimpleBindings just maps variables to SortField instances
SimpleBindings bindings = new SimpleBindings();
bindings.add(new SortField("_score", SortField.Type.SCORE));
bindings.add(new SortField("popularity", SortField.Type.INT));

// create a query that matches based on body:contents but
// scores using expr
Query query = new FunctionScoreQuery(
    new TermQuery(new Term("body", "contents")),
    expr.getDoubleValuesSource(bindings));
searcher.search(query, 10);