elasticsearch FunctionScore Java API sample
程序员文章站
2022-07-14 12:40:15
...
public Response<List<Book>> fieldValueFactor(String query) {
// query
MultiMatchQueryBuilder multiMatchQueryBuilder = new MultiMatchQueryBuilder(query)
.field(Constants.TITLE).field(Constants.SUMMARY);
// fieldValueFactor
FieldValueFactorFunctionBuilder fieldValueFactor = ScoreFunctionBuilders.fieldValueFactorFunction(Constants.NUM_REVIEWS)
.factor(2).modifier(FieldValueFactorFunction.Modifier.LOG1P);
// functionscore
FunctionScoreQueryBuilder queryBuilder = QueryBuilders.functionScoreQuery(multiMatchQueryBuilder, fieldValueFactor);
SearchRequestBuilder requestBuilder = client.prepareSearch(bookIndex).setTypes(bookType)
.setQuery(queryBuilder).setFetchSource(Constants.fetchFieldsTSPN, null);
}
script_score,通过脚本进行自定义打分
// 参数
Map<String, Object> params = new HashMap<>();
params.put("threshold", threshold);
//painless 脚本,注意不能换行
String scriptStr = "publish_date = doc['publish_date'].value; num_reviews = doc['num_reviews'].value; if (publish_date > Date.parse('yyyy-MM-dd', threshold).getTime()) { return log(2.5 + num_reviews) }; return log(1 + num_reviews);";
Script script = new Script(ScriptType.INLINE, "painless", scriptStr, params);
//构造脚本查询对象
ScriptScoreFunctionBuilder scriptScoreFunctionBuilder = ScoreFunctionBuilders.scriptFunction(script);
//组合成完整查询
FunctionScoreQueryBuilder queryBuilder = QueryBuilders.functionScoreQuery(multiMatchQueryBuilder, scriptScoreFunctionBuilder);
推荐阅读
-
Elasticsearch Java Rest Client API 整理总结 (一)
-
Elasticsearch Java Rest Client API 整理总结 (二) —— SearchAPI
-
elasticsearch FunctionScore Java API sample
-
【Spark Java API】Transformation(2)—sample、randomSplit
-
Elasticsearch Java Rest Client API 整理总结 (三)——Building Queries
-
Elasticsearch Java API 的使用(14)—优化索引创建之setting设置、写入优化
-
Elasticsearch 5.X Java API 创建客户端
-
Elasticsearch Java REST高级客户端 MultiSearch Api
-
elasticsearch java客户端api使用(一)
-
elasticsearch Java API 客户端(Node Client)