文章目录世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。
-侯氏工坊
重要参考参考索引实体单层再评分
以求和的形式对搜索结果再评分以求积的形式对搜索结果再评分以平均值的形式对搜索结果再评分以取最大值的形式对搜索结果再评分以取最小值的形式对搜索结果再评分java实现使用 对搜索结果多层再评分
重要参考
从当前版本以后统一使用starter:elasticsearch - java - 高级篇 - 封装类 - 3 - 关于封装的改进,使用spring-boot-starter
elasticsearch - java - 高级篇 - 封装类 - 1
elasticsearch - java - 高级篇 - 封装类 - 2
elasticsearch - java - 高级篇 - 封装类 - 3
参考elasticsearch - java - elasticsearch和kibana的安装和配置elasticsearch - java - elasticsearch对接spring bootElasticsearchClient客户端深入学习访问基础篇和高级篇所有栏目内容参考栏目预告stack - es - 官方文档 - 过滤搜索结果elasticsearch - java - 基础篇 - 搜索 - 搜索结果再评分 索引
POST /index_rescore/_bulk
{"create": {}}
{"content": "hello good me", "count": 5}
{"create": {}}
{"content": "this hello good me", "count": 3}
{"create": {}}
{"content": "this hello good me hello", "count": 2}
{"create": {}}
{"content": "hello hello good me", "count": 7}
{"create": {}}
{"content": "this is a hello good me", "count": 4}
{"create": {}}
{"content": "this good is a hello good me", "count": 6}
实体
@Data
@SearchIndex(value = "index_rescore", objName = "")
public class AdvanceRescore {
private String content;
private Integer count;
}
单层再评分
以求和的形式对搜索结果再评分
原语句
# rescore_model_total
GET /index_rescore/_search
{
"query": {
"match": {
"content": {
"operator": "or",
"query": "hello me"
}
}
},
"rescore": {
"query": {
"rescore_query": {
"match_phrase": {
"content":
{
"query": "hello good me",
"slop": 2
}
}
},
"query_weight": 0.7,
"rescore_query_weight": 1.2
},
"window_size": 10
}
}
以求积的形式对搜索结果再评分
原语句
# rescore_model_multiply
GET /index_rescore/_search
{
"query": {
"match": {
"content": {
"operator": "or",
"query": "hello me"
}
}
},
"rescore": {
"query": {
"rescore_query": {
"match_phrase": {
"content":
{
"query": "hello good me",
"slop": 2
}
}
},
"query_weight": 0.7,
"rescore_query_weight": 1.2,
"score_mode": "multiply"
},
"window_size": 10
}
}
以平均值的形式对搜索结果再评分
原语句
# rescore_model_avg
GET /index_rescore/_search
{
"query": {
"match": {
"content": {
"operator": "or",
"query": "hello me"
}
}
},
"rescore": {
"query": {
"rescore_query": {
"match_phrase": {
"content":
{
"query": "hello good me",
"slop": 2
}
}
},
"query_weight": 0.7,
"rescore_query_weight": 1.2,
"score_mode": "avg"
},
"window_size": 10
}
}
以取最大值的形式对搜索结果再评分
原语句
# rescore_model_max
GET /index_rescore/_search
{
"query": {
"match": {
"content": {
"operator": "or",
"query": "hello me"
}
}
},
"rescore": {
"query": {
"rescore_query": {
"match_phrase": {
"content":
{
"query": "hello good me",
"slop": 2
}
}
},
"query_weight": 0.7,
"rescore_query_weight": 1.2,
"score_mode": "max"
},
"window_size": 10
}
}
以取最小值的形式对搜索结果再评分
原语句
# rescore_model_min
GET /index_rescore/_search
{
"query": {
"match": {
"content": {
"operator": "or",
"query": "hello me"
}
}
},
"rescore": {
"query": {
"rescore_query": {
"match_phrase": {
"content":
{
"query": "hello good me",
"slop": 2
}
}
},
"query_weight": 0.7,
"rescore_query_weight": 1.2,
"score_mode": "min"
},
"window_size": 10
}
}
java实现
java实现
public使用SearchResponse simpleRescore( Class c, AdvanceMatchVO matchVO, AdvanceRescoreVO advanceRescoreVO ) throws Exception { return elasticsearchClient.search( new SearchRequestEntity() .index(EntityAnnotationUtils.getIndex(c)) .fields(EntityAnnotationUtils.getFields(c)) .match(matchVO) .rescore(advanceRescoreVO) .build() , c); }
simpleRescore(
AdvanceRescore.class, matchVO, rescoreVO
);
AdvanceMatchVO matchVO = new AdvanceMatchVO(
"content", FieldValue.of("hello me"),
Operator.Or);
// 传参说明:
// Total - 求和
// Multiply - 求积
// Avg - 平均值
// Max - 最大值
// Min - 最小值
String scoreMode = "Total";
AdvanceRescoreVO rescoreVO = new AdvanceRescoreVO(
10, ScoreMode.valueOf(scoreMode),
0.7, 1.2,
SearchRequestUtils.mathPhraseQuery(
new AdvanceMathPharseVO(
"content",
"hello good me",
2)));
对搜索结果多层再评分
原语句
# rescore_multi
GET /index_rescore/_search
{
"query": {
"match": {
"content": {
"operator": "or",
"query": "hello me"
}
}
},
"rescore": [
{
"query": {
"rescore_query": {
"match_phrase": {
"content":
{
"query": "hello good me",
"slop": 2
}
}
},
"query_weight": 0.7,
"rescore_query_weight": 1.2,
"score_mode": "total"
},
"window_size": 10
}, {
"query": {
"rescore_query": {
"function_score": {
"script_score": {
"script": {
"source": "Math.log10(doc.count.value + 2)"
}
}
}
},
"score_mode": "multiply"
},
"window_size": 3
}
]
}
java实现
publicSearchResponse multiRescore( Class c, AdvanceMatchVO matchVO, AdvanceRescoreVO matchPhraseRescoreVO, AdvanceRescoreVO scriptRescoreVO ) throws Exception { return elasticsearchClient.search( new SearchRequestEntity() .index(EntityAnnotationUtils.getIndex(c)) .fields(EntityAnnotationUtils.getFields(c)) .match(matchVO) .rescore(matchPhraseRescoreVO) .rescore(scriptRescoreVO) .build() , c); }
使用
multiRescore(
AdvanceRescore.class,
matchVO,
matchPhraseRescoreVO,
scriptRescoreVO
);
AdvanceMatchVO matchVO = new AdvanceMatchVO(
"content", FieldValue.of("hello me"),
Operator.Or);
AdvanceRescoreVO matchPhraseRescoreVO = new AdvanceRescoreVO(
10, ScoreMode.Total,
0.7, 1.2,
SearchRequestUtils.mathPhraseQuery(
new AdvanceMathPharseVO(
"content",
"hello good me",
2)));
AdvanceRescoreVO scriptRescoreVO = new AdvanceRescoreVO(
3, ScoreMode.Multiply,
1.0, 1.0,
SearchRequestUtils.scriptFunctionScore(
new AdvancescriptVO("Math.log10(doc.count.value + 2)")
));



