1.下面写的第二种方法是我以前使用的,后来觉得太冗余了就一直寻找更好的方式,最后发现了dis_max方法,能更好的实现取最大得分为得分:
{
"query": {
"dis_max": {
"queries": [
{
"constant_score": {
"boost": 700,
"filter": {
"term": {
"field.keyword": "dongzijing1"
}
}
}
},
{
"constant_score": {
"boost": 600,
"filter": {
"prefix": {
"field.keyword": "dongzijing2"
}
}
}
}
]
}
}
}
2.使用function_score+weight+score_mode
其中:
score_mode是functions中的得分策略
boost_mode是query和functions组合为最后得分,replace为最后得分为functions中的得分
参考:https://blog.csdn.net/weixin_40341116/article/details/80913045
{
"query": {
"function_score": {
"score_mode": "max",
"boost_mode": "replace",
"functions": [
{
"filter": {
"term": {
"field.keyword": "dongzijing1"
}
},
"weight": 700
},
{
"filter": {
"prefix": {
"field.keyword": "dongzijing2"
}
},
"weight": 600
}
],
"query": {
"bool": {
"should": [
{
"term": {
"field.keyword": "dongzijing1"
}
},
{
"term": {
"field.keyword": "dongzijing2"
}
}
]
}
}
}
}
}