public Response> 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,通过脚本进行自定义打分
// 参数 Mapparams = 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);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)