导言
上文介绍了DSL语言查询,本文介绍进一步的查询。
更复杂搜索
让搜索更加复杂。我们仍然想找到所有姓史密斯的员工,但我们想要的是年龄超过30岁的员工。我们的查询将小改变来适应一个过滤器,它允许我们有效地执行结构化搜索:
GET /megacorp/employee/_search?pretty
{
"query" : {
"filtered" : {
"filter" : {
"range" : {
"age" : { "gt" : 30 }# ---(1)
}
},
"query" : {
"match" : {
"last_name" : "smith" # ---(2)
}
}
}
}
}
不要担心现在的语法太多,我们将讨论在稍后详细。现在我们已经意识到,添加了一个执行范围搜索的过滤器,和重用之前相同的match
查询。现在我们的结果显示只有一个员工,年龄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" ]
}
}
]
}
}
实践六
在sence中,输入如下的curl请求代码
curl -XGET 'localhost:9200/megacorp/employee/_search?pretty' -d'
{
"query" : {
"filtered" : {
"filter" : {
"range" : {
"age" : { "gt" : 30 }
}
},
"query" : {
"match" : {
"last_name" : "smith"
}
}
}
}
}'
结果如图所示:
完整返回信息如下:
{
"took": 47,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.30685282,
"hits": [
{
"_index": "megacorp",
"_type": "employee",
"_id": "2",
"_score": 0.30685282,
"_source": {
"first_name": "Jane",
"last_name": "Smith",
"age": 32,
"about": "I like to collect rock albums",
"interests": [
"music"
]
}
}
]
}
}
结语
本文介绍了更为复杂的查询,是在要求的匹配的情况下,加了过滤器,在返回的搜索结果中,要求更进一步,这个在应用场景中很常见。