和其他关系型数据库一样,我们经常操作数据库是对数据库的查询操作,那么查询在ES中是非常重要的,下面将针对查询做以下操作。
接:ElasticSearch系列之基础入门篇
接下来我们多插入几条数据,通过筛选条件查询出数据。
数据准备
get请求:http://192.168.254.128:9200/my_index_one/employee/2
{
"first_name" : "Michael",
"last_name" : "jack",
"age" : 20,
"about" : "I like reading",
"interests": [ "movie", "book" ]
}
get请求:http://192.168.254.128:9200/my_index_one/employee/3
{
"first_name" : "Tom",
"last_name" : "Smith",
"age" : 21,
"about" : "I like running",
"interests": [ "movie", "book" ]
}
数据插入完之后我们开始使用条件查询。
搜索
搜索查询所有数据
ES默认情况下只返回10条数据。
get请求:http://192.168.254.128:9200/my_index_one/employee/_search
搜索结果:
{
"took": 446,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "my_index_one",
"_type": "employee",
"_id": "1",
"_score": 1.0,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "my_index_one",
"_type": "employee",
"_id": "3",
"_score": 1.0,
"_source": {
"first_name": "Tom",
"last_name": "Smith",
"age": 21,
"about": "I like running",
"interests": [
"movie",
"book"
]
}
},
{
"_index": "my_index_one",
"_type": "employee",
"_id": "2",
"_score": 1.0,
"_source": {
"first_name": "Michael",
"last_name": "jack",
"age": 20,
"about": "I like reading",
"interests": [
"movie",
"book"
]
}
}
]
}
}
条件查询
我们通过一个URL参数来传递查询信息给搜索接口
Get请求:http://192.168.254.128:9200/my_index_one/employee/_search?q=last_name:Smith
查询结果:返回结果给出了所有的 Smith
{
"took": 783,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 0.53899646,
"hits": [
{
"_index": "my_index_one",
"_type": "employee",
"_id": "1",
"_score": 0.53899646,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "my_index_one",
"_type": "employee",
"_id": "3",
"_score": 0.53899646,
"_source": {
"first_name": "Tom",
"last_name": "Smith",
"age": 21,
"about": "I like running",
"interests": [
"movie",
"book"
]
}
}
]
}
}
查询条件可以放在url中,当然也可以放在body体中查询。
使用查询表达式搜索
get请求:http://192.168.254.128:9200/my_index_one/employee/_search
请求体:
{ "query" : { "match" : { "last_name" : "Smith" } } }
返回结果同上。
使用过滤器 filter搜索
get请求:http://192.168.254.128:9200/my_index_one/employee/_search
请求体:
{
"query" : {
"bool": {
"must": {
"match" : {
"last_name" : "smith"
}
},
"filter": {
"range" : {
"age" : { "gt" : 20 }
}
}
}
}
}
查询出年龄大于20岁的数据(不包含20)
查询结果:
{
"took": 27,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 0.53899646,
"hits": [
{
"_index": "my_index_one",
"_type": "employee",
"_id": "1",
"_score": 0.53899646,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "my_index_one",
"_type": "employee",
"_id": "3",
"_score": 0.53899646,
"_source": {
"first_name": "Tom",
"last_name": "Smith",
"age": 21,
"about": "I like running",
"interests": [
"movie",
"book"
]
}
}
]
}
}
全文搜索
get请求:http://192.168.254.128:9200/my_index_one/employee/_search
请求体:
{
"query" : {
"match" : {
"about" : "I like"
}
}
}
搜索about中带有I like的数据
查询结果:
{
"took": 9,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 0.40211007,
"hits": [
{
"_index": "my_index_one",
"_type": "employee",
"_id": "3",
"_score": 0.40211007,
"_source": {
"first_name": "Tom",
"last_name": "Smith",
"age": 21,
"about": "I like running",
"interests": [
"movie",
"book"
]
}
},
{
"_index": "my_index_one",
"_type": "employee",
"_id": "2",
"_score": 0.40211007,
"_source": {
"first_name": "Michael",
"last_name": "jack",
"age": 20,
"about": "I like reading",
"interests": [
"movie",
"book"
]
}
},
{
"_index": "my_index_one",
"_type": "employee",
"_id": "1",
"_score": 0.06836608,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
}
]
}
}
看看结果是不是很疑惑?明明搜索的是I like,怎么I love也搜索出来了?
其实Elasticsearch 是默认按照相关性得分进行排序的,即每个文档跟查询的匹配程度。第一和第二个最高得分的结果很明显:Tom Smith 和Michael jack的 about 属性清楚地写着 “I like” 。
但为什么 Jane Smith 也作为结果返回了呢?原因是她的 about 属性里也有 “I” 。因为只有 “I” 而没有 “like” ,所以她的相关性得分低于 Tom和Michael的。
高亮搜索
我们经常使用搜索引擎查询资料,在返回结果中,匹配我们查询数据的会有高亮显示,当然ES也很容易实现这种效果。
在执行查询前,加一个highlight 就可以了:
get请求:http://192.168.254.128:9200/my_index_one/employee/_search
请求体:
{
"query" : {
"match_phrase" : {
"about" : "running"
}
},
"highlight": {
"fields" : {
"about" : {}
}
}
}
返回结果:
{
"took": 346,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.9395274,
"hits": [
{
"_index": "my_index_one",
"_type": "employee",
"_id": "3",
"_score": 0.9395274,
"_source": {
"first_name": "Tom",
"last_name": "Smith",
"age": 21,
"about": "I like running",
"interests": [
"movie",
"book"
]
},
"highlight": {
"about": [
"I like <em>running</em>"
]
}
}
]
}
}
可以看到返回结果里多了一个highlight节点,而节点里面的running单词是被高亮包裹的。