1、constant_score查询
内部包装了过滤查询,故而不会计算相似度分,该查询返回的相似度分与字段上指定boost参数值相同
//请求参数
GET bank/_search
{
"query": {
"constant_score": {
"filter": {
"term": {
"city.keyword": "Fredericktown"
}
},
"boost": 1.3
}
}
}
//结果返回
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.3,
"hits" : [
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "97",
"_score" : 1.3,
"_source" : {
"account_number" : 97,
"balance" : 49671,
"firstname" : "Karen",
"lastname" : "Trujillo",
"age" : 40,
"gender" : "F",
"address" : "512 Cumberland Walk",
"employer" : "Tsunamia",
"email" : "[email protected]",
"city" : "Fredericktown",
"state" : "MO"
}
}
]
}
}
constant_score查询的顶层参数
序号 | 参数 | 描述 |
---|---|---|
1 | filter | 必须,查询对象,指定希望执行的过滤查询,任何返回的文档都必须匹配这个查询;过滤查询不会计算相似度分,为提升性能,ES会对使用频率高的过滤查询的结果进行缓存 |
2 | boost | 可选,浮点数,该值作为匹配了以上filter的文档的相似度分,默认为1.0 |
2、dis_max查询
最大析取(disjunction max)
返回的文档必须要满足多个查询子句中的一项条件;
若一个文档能匹配多个查询子句时,则dis_max查询将为能匹配上查询子句条件的项增加额外分,即针对多个子句文档有一项满足就针对满足的那一项分配更高分,这也能打破在多个文档都匹配某一个或多个条件时分数相同的情况;
//查询city为Brogan与gender为M的银行职员,能匹配任一项在返回结果中就存在
//tie_breaker计算最高分数因子
//在dis_max查询时针对多个查询字段指定不同的boost因子,此处指定city与gender不同的因子,分数也会因此不同
//请求参数
GET bank/_search
{
"size": 5,
"query": {
"dis_max": {
"tie_breaker": 0.7,
"queries": [
{
"term": {
"city.keyword": {
"boost": 2,
"value": "Brogan"
}
}
},
{
"term": {
"gender.keyword": {
"boost": 1.0,
"value": "M"
}
}
}
]
}
}
}
//返回结果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 507,
"relation" : "eq"
},
"max_score" : 13.48206,
"hits" : [
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "1",
"_score" : 13.48206,
"_source" : {
"account_number" : 1,
"balance" : 39225,
"firstname" : "Amber",
"lastname" : "Duke",
"age" : 32,
"gender" : "M",
"address" : "880 Holmes Lane",
"employer" : "Pyrami",
"email" : "[email protected]",
"city" : "Brogan",
"state" : "IL"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "6",
"_score" : 0.679258,
"_source" : {
"account_number" : 6,
"balance" : 5686,
"firstname" : "Hattie",
"lastname" : "Bond",
"age" : 36,
"gender" : "M",
"address" : "671 Bristol Street",
"employer" : "Netagy",
"email" : "[email protected]",
"city" : "Dante",
"state" : "TN"
}
},
{
"_index" : "bank"