如何在 Solrj 中正确使用评分函数
How to properly use a scoring function in Solrj
我正在尝试根据文档中浮点数字段的值来提高文档的分数。
例如,用户可能搜索 "Oliphaunts named ron, with height between 6 and 10 meters",而我想以类似于 DecayFunction 的方式查询 height
字段。
在下面的简化查询中,我希望检索按名称和身高评分的 oliphaunts,其中身高越接近 8 米越好 -
q=name:"ron" _val_:"div(1,abs(sub(height,8)))"
我使用 math operands and regular functions -
的组合起草了我的指数衰减评分函数
exp(sub(0,div(pow(max(0,sub(abs(sub(value,origin)),offset)),2),mul(2,sub(0,div(pow(scale,2),mul(2,ln(decay))))))))
现在我想使用 _val_
magic field.
将此函数合并到查询分数中
如何在 Solrj 中完成此操作?
还有什么其他方法(而不是 _val_
)来做到这一点?
{p.s。 - 我在 Solr 5.3.1 中使用标准查询解析器}
我最终通过实施自定义 lucene.search.Query
来完成此任务。以下是 class 及其用法的摘要 -
package org.example.ronvisbord.solr.queries.custom;
import org.apache.lucene.search.Query;
public class HeightFunctionQuery extends Query {
private final String queryTemplate = "(_val_:\"%s\")^%f";
private final String functionTemplate = "div(1,abs(sub(%s,%d)))";
private double boost;
@Override
public String toString(String field) {
return String.format(queryTemplate, createFunction(field), boost);
}
public HeightFunctionQuery(double boost, int targetHeight) {
this.boost = boost;
this.targetHeight = targetHeight;
}
private String createFunction(String field) {
return String.format(functionTemplate, field, targetHeight);
}
}
我通过将 toString(field)
放在 solrj.SolrQuery
-
的 "q" 参数中来使用 class
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.SolrClient;
import org.example.ronvisbord.solr.queries.custom.HeightFunctionQuery;
import org.apache.lucene.search.Query;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.response.QueryResponse;
...
double boost = 10.0;
double targetHeight = 8;
String heightField = "height";
Query heightQuery = new HeightFunctionQuery(targetHeight, boost);
SolrQuery solrQuery = new SolrQuery();
solrQuery.set("q", heightQuery.toString(heightField));
// ... further configure the solrQuery ...
SolrClient client = new HttpSolrClient("http://solr_host:8983/solr/core")
QueryResponse response = client.query(query, SolrRequest.METHOD.POST)
// ... process results ...
我正在尝试根据文档中浮点数字段的值来提高文档的分数。
例如,用户可能搜索 "Oliphaunts named ron, with height between 6 and 10 meters",而我想以类似于 DecayFunction 的方式查询 height
字段。
在下面的简化查询中,我希望检索按名称和身高评分的 oliphaunts,其中身高越接近 8 米越好 -
q=name:"ron" _val_:"div(1,abs(sub(height,8)))"
我使用 math operands and regular functions -
的组合起草了我的指数衰减评分函数exp(sub(0,div(pow(max(0,sub(abs(sub(value,origin)),offset)),2),mul(2,sub(0,div(pow(scale,2),mul(2,ln(decay))))))))
现在我想使用 _val_
magic field.
如何在 Solrj 中完成此操作?
还有什么其他方法(而不是 _val_
)来做到这一点?
{p.s。 - 我在 Solr 5.3.1 中使用标准查询解析器}
我最终通过实施自定义 lucene.search.Query
来完成此任务。以下是 class 及其用法的摘要 -
package org.example.ronvisbord.solr.queries.custom;
import org.apache.lucene.search.Query;
public class HeightFunctionQuery extends Query {
private final String queryTemplate = "(_val_:\"%s\")^%f";
private final String functionTemplate = "div(1,abs(sub(%s,%d)))";
private double boost;
@Override
public String toString(String field) {
return String.format(queryTemplate, createFunction(field), boost);
}
public HeightFunctionQuery(double boost, int targetHeight) {
this.boost = boost;
this.targetHeight = targetHeight;
}
private String createFunction(String field) {
return String.format(functionTemplate, field, targetHeight);
}
}
我通过将 toString(field)
放在 solrj.SolrQuery
-
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.SolrClient;
import org.example.ronvisbord.solr.queries.custom.HeightFunctionQuery;
import org.apache.lucene.search.Query;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.response.QueryResponse;
...
double boost = 10.0;
double targetHeight = 8;
String heightField = "height";
Query heightQuery = new HeightFunctionQuery(targetHeight, boost);
SolrQuery solrQuery = new SolrQuery();
solrQuery.set("q", heightQuery.toString(heightField));
// ... further configure the solrQuery ...
SolrClient client = new HttpSolrClient("http://solr_host:8983/solr/core")
QueryResponse response = client.query(query, SolrRequest.METHOD.POST)
// ... process results ...