bool综合相关度分数
每个子查询都会计算一个document针对它的相关度分数,然后bool综合所有分数,合并为一个分数,当然filter是不会计算分数的
GET /website/article/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "elasticsearch"
}
}
],
"should": [
{
"match": {
"content": "elasticsearch"
}
}
],
"must_not": [
{
"match": {
"author_id": 111
}
}
]
}
}
}
响应结果
{
"took": 10,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.5408423,
"hits": [
{
"_index": "website",
"_type": "article",
"_id": "3",
"_score": 0.5408423,
"_source": {
"title": "my elasticsearch article",
"content": "elasticsearch is very bad",
"author_id": 113
}
},
{
"_index": "website",
"_type": "article",
"_id": "2",
"_score": 0.25316024,
"_source": {
"title": "my elasticsearch article",
"content": "es is very good",
"author_id": 112
}
}
]
}
}
constant_score
当我们不关心检索词频率TF(Term Frequency)对搜索结果排序的影响时,可以使用constant_score将查询语句query或者过滤语句filter包装起来。
GET /company/employee/_search
{
"query": {
"constant_score": {
"filter": {
"range": {
"age": {
"gte": 30
}
}
}
}
}
}
响应结果
{
"took": 102,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "company",
"_type": "employee",
"_id": "2",
"_score": 1,
"_source": {
"address": {
"country": "china",
"province": "jiangsu",
"city": "nanjing"
},
"name": "tom",
"age": 30,
"join_date": "2016-01-01"
}
},
{
"_index": "company",
"_type": "employee",
"_id": "3",
"_score": 1,
"_source": {
"address": {
"country": "china",
"province": "shanxi",
"city": "xian"
},
"name": "marry",
"age": 35,
"join_date": "2015-01-01"
}
}
]
}
}