1

在 Lucene 6.6.0 中,不推荐使用索引时间提升。此外,CustomQueryScore 也已被弃用。所以,Lucene 开发者的建议是使用 FunctionScoreQuery。但是,我不明白如何使用 FunctionScoreQuery 提升文本字段,因为它需要 DoubleValuesSource 作为输入,这有助于提升数字字段(fromIntField、fromDoubleField 等......)

4

3 回答 3

0

我相信您误解了他们在那里提供的指导。您在 docValues 字段中索引提升,而不是文本。将提升视为一种元字段。所以你最终有两个字段,比如:

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

您将查询 myField 的内容,并将 myFieldBoost 的值纳入您的 FunctionScoreQuery。

于 2018-06-20T22:54:22.457 回答
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);

Lucene Expression 类接受 Javascript 表达式:(https://lucene.apache.org/core/7_7_2/expressions/org/apache/lucene/expressions/js/package-summary.html

“Javascript 表达式是使用基于 JavaScript 表达式的表达式语法指定的数字表达式。您可以使用以下方法构造表达式:

  • 整数、浮点、十六进制和八进制文字
  • 算术运算符:+ - * / %
  • 位运算符:| & ^ ~ << >> >>>
  • 布尔运算符(包括三元运算符):&& || ! ?:
  • 比较运算符:< <= == >= >
  • 常用数学函数:abs ceil exp floor ln log10 logn max min sqrt pow
  • 三角函数库函数:acosh acos asinh asin atanh atan atan2 cosh cos sinh sin tanh tan
  • 距离函数:haversin
  • 其他功能:min, max
  • 任意外部变量” - 参见上面的 Bindings 示例
于 2019-05-30T15:33:32.077 回答
0

搜索时提升是一个更好的解决方案,可以通过以下方式完成:

要基本了解评分的工作原理,请锁定文档

于 2018-06-21T11:19:20.107 回答