1.简介
该查询将其内部的查询结果文档得分都设定为固定值,默认为1,多用于结合bool查询实现自定义得分,其基本语法如下。
POST /index_name/_search
{
"query": {
"constant_score": {
"filter": {
"match": {
"field_name": "query_clause"
}
},
"boost": score
}
}
}
- const_score:关键字
- filter:只能有一个
- field_name:字段名
- query_clause:待查询的语句
- boost:自定义得分
2.query
查询名为steven的文档,并将得分设置为1.2。
POST /people/_search
POST /people/_search
{
"query": {
"constant_score": {
"filter": {
"match": {
"name": "steven"
}
},
"boost": 1.2
}
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.2,
"hits" : [
{
"_index" : "people",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.2,
"_source" : {
"name" : "Mike Steven",
"country" : "China",
"age" : 26,
"birthday" : "1995-01-01",
"description" : "I am Mike Steven."
}
}
]
}
}