现在尝试下更复杂的搜索。同样搜索姓氏为Smith 的员工,但这次我们只需要年龄大于30 的。查询需要稍作调整,使用过滤器filter,他支持高效的执行一个结构化查询。
GET /megacorp/employee/_search{"query" : {"bool": {"must": {"match" : {"last_name" : "smith"}},"filter": {"range" : {"age" : { "gt" : 30 }}}}}}
- 这部分与我们之前使用的match 查询一样
- 这部分是一个range 过滤器,他能找到年龄大于30的文档,其中gt 表示大于(great than)
目前无需担心语法问题,后续会更详细的介绍,只需明确我们添加了一个过滤器用于执行一个范围查询,并复用之前的match 查询。现在返回结果只返回了一名员工,叫Jane Smith,32岁
{
...
"hits": {
"total": 1,
"max_score": 0.30685282,
"hits": [
{
...
"_source": {
"first_name": "Jane",
"last_name": "Smith",
"age": 32,
"about": "I like to collect rock albums",
"interests": [ "music" ]
}
}
]
}
}
