1. 复合查询 boolean query
(1) 布尔查询是一个或多个查询子句的组合,子查询的组合方式有:
must: 必须匹配每个子查询,类似 "与"
should: 选择性匹配子查询,类似 "或"
must_not: 必须不匹配,不参与算分,类似 " 非 "
filter: 必须匹配,不参与算分
get /hotel/_search
{
"query":{
"bool":{
//city必须等于南京
"must": [
{"term":{"city":"南京"}}
],
//品牌是两者中任意一个都行
"should": [
{"term":{"brand": "四季"}},
{"term":{"brand": "7天"}}
],
//必须取反
"must_not": [
{
"range": {
"price": {
"gte": 100
}
}
}
],
//必须成立
"filter": [
{
"range": {
"score": {
"gte": 5
}
}
}
]
}
}
}
2 案例
(1) 搜索名字包含"如家",价格不高于500
get /hotel/_search
{
"query":{
"bool": {
"must": [
{
"match": {
"name": "如家"
}
}
],
"must_not": [
{
"range": {
"price": {
"gt": 500
}
}
}
]
}
}
}
查询结果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.6277175,
"hits" : [
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.6277175,
"_source" : {
"address" : "柳州东路1号",
"brand" : "如家",
"business" : "弘阳商圈",
"city" : "南京市浦口区",
"id" : 1,
"location" : "33.33,131.33",
"name" : "如家",
"pic" : "http://www.bai.com/images/rujia.png",
"price" : 189,
"score" : 7,
"starName" : "二星"
}
}
]
}
}