上面我们已经介绍了Elasticsearch的一些基本操作,这篇文章属于进阶篇,我们一起来学习。
前面我们创建了sdb和user文档,现在我们来看如何查询user中所有的文档呢?
GET /sdb/user/_search
此时输出入下:
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 1.0,
"hits" : [
{
"_index" : "sdb",
"_type" : "user",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"username" : "小丽丽",
"age" : 16,
"gender" : "女",
"about" : "this is my info",
"addrs" : [
"北京",
"江西",
"香港"
]
}
},
{
"_index" : "sdb",
"_type" : "user",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"username" : "张三",
"age" : 16,
"gender" : "男",
"about" : "this is my info",
"addrs" : [
"甘肃",
"陕西",
"兰州"
]
}
},
{
"_index" : "sdb",
"_type" : "user",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"username" : "秦雪",
"age" : 16,
"gender" : "女",
"about" : "this is my student",
"addrs" : [
"甘肃",
"陕西",
"天津"
]
}
}
]
}
}
接下来我们来查询姓名为秦雪的人
GET /sdb/user/_search?q=username:%e7%a7%a6%e9%9b%aa (这里需要注意,username:是urlencoding过后的字符串,如果是中文,kibana dev tools会报错)
执行结果如下:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "sdb",
"_type" : "user",
"_id" : "3",
"_score" : 0.5753642,
"_source" : {
"username" : "秦雪",
"age" : 16,
"gender" : "女",
"about" : "this is my student",
"addrs" : [
"甘肃",
"陕西",
"天津"
]
}
}
]
}
}
可以看到,我们检索出了名字为秦雪的人。
下面我们介绍如何使用Query String的形式来查询
GET /sdb/user/_search
{
"query" : {
"match" : {
"username" : "秦雪"
}
}
}
我们可以看到,检索结果如下:
下面我们来看看更为复杂的检索
例如要查询addr在甘肃的同学
GET /sdb/user/_search
{
"query": {
"bool": {
"must": [
{"match": {
"addrs": "甘肃"
}}
]
}
}
}
结果如下:
{
"took" : 11,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "sdb",
"_type" : "user",
"_id" : "1",
"_score" : 0.5753642,
"_source" : {
"username" : "张三",
"age" : 16,
"gender" : "男",
"about" : "this is my info",
"addrs" : [
"甘肃",
"陕西",
"兰州"
]
}
},
{
"_index" : "sdb",
"_type" : "user",
"_id" : "3",
"_score" : 0.5753642,
"_source" : {
"username" : "秦雪",
"age" : 16,
"gender" : "女",
"about" : "this is my student",
"addrs" : [
"甘肃",
"陕西",
"天津"
]
}
}
]
}
}
检索包含甘肃但是不在包含天津的同学
GET /sdb/user/_search
{
"query": {
"bool": {
"must": [
{"match": {
"addrs": "甘肃"
}}
],
"must_not": [
{"match": {
"addrs": "天津"
}}
]
}
}
}
结果如下:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "sdb",
"_type" : "user",
"_id" : "1",
"_score" : 0.5753642,
"_source" : {
"username" : "张三",
"age" : 16,
"gender" : "男",
"about" : "this is my info",
"addrs" : [
"甘肃",
"陕西",
"兰州"
]
}
}
]
}
}
接下来为大家介绍es里边的短语搜索
首先使用match_all来显示所有文档
GET /sdb/user/_search
{
"query": {
"match_all": {}
}
}
结果如下:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 1.0,
"hits" : [
{
"_index" : "sdb",
"_type" : "user",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"username" : "小丽丽",
"age" : 16,
"gender" : "女",
"about" : "this is my info",
"addrs" : [
"北京",
"江西",
"香港"
]
}
},
{
"_index" : "sdb",
"_type" : "user",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"username" : "张三",
"age" : 16,
"gender" : "男",
"about" : "this is my info",
"addrs" : [
"甘肃",
"陕西",
"兰州"
]
}
},
{
"_index" : "sdb",
"_type" : "user",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"username" : "秦雪",
"age" : 16,
"gender" : "女",
"about" : "this is my student",
"addrs" : [
"甘肃",
"陕西",
"天津"
]
}
}
]
}
}
接着我们查询
GET /sdb/user/_search
{
"query": {
"match_phrase": {
"about": "my student"
}
}
}
结果如下:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "sdb",
"_type" : "user",
"_id" : "3",
"_score" : 0.5753642,
"_source" : {
"username" : "秦雪",
"age" : 16,
"gender" : "女",
"about" : "this is my student",
"addrs" : [
"甘肃",
"陕西",
"天津"
]
}
}
]
}
}
我们发现,查询到的带有my student的记录只有一个,说明查询是正确的。
下面我们说一下关键词高亮,这个在搜索显示的地方用的比较多
GET /sdb/user/_search
{
"query": {
"match_phrase": {
"about": "my student"
}
},
"highlight": {
"fields": {
"about": {}
}
}
}
输出结果如下:
{
"took" : 233,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "sdb",
"_type" : "user",
"_id" : "3",
"_score" : 0.5753642,
"_source" : {
"username" : "秦雪",
"age" : 16,
"gender" : "女",
"about" : "this is my student",
"addrs" : [
"甘肃",
"陕西",
"天津"
]
},
"highlight" : {
"about" : [
"this is <em>my</em> <em>student</em>"
]
}
}
]
}
}
可能有很多同学会问,我不想用em标签,我想用其他的,那怎么办?不用着急,elasticsearch已经为我们想到了,请看下面
GET /sdb/user/_search
{
"query": {
"match_phrase": {
"about": "my student"
}
},
"highlight": {
"fields": {
"about": {}
},
"pre_tags" : ["<color>"],
"post_tags" : ["</color>"]
}
}
结果如下:
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "sdb",
"_type" : "user",
"_id" : "3",
"_score" : 0.5753642,
"_source" : {
"username" : "秦雪",
"age" : 16,
"gender" : "女",
"about" : "this is my student",
"addrs" : [
"甘肃",
"陕西",
"天津"
]
},
"highlight" : {
"about" : [
"this is <color>my</color> <color>student</color>"
]
}
}
]
}
}
接下来我们来看看分析
GET /sdb/user/_search
{
"aggs": {
"all_interests": {
"terms": {"field": "interests"}
}
}
}
以上写法是死的,结果如下:
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 1.0,
"hits" : [
{
"_index" : "sdb",
"_type" : "user",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"username" : "小丽丽",
"age" : 16,
"gender" : "女",
"about" : "this is my info",
"addrs" : [
"北京",
"江西",
"香港"
]
}
},
{
"_index" : "sdb",
"_type" : "user",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"username" : "张三",
"age" : 16,
"gender" : "男",
"about" : "this is my info",
"addrs" : [
"甘肃",
"陕西",
"兰州"
]
}
},
{
"_index" : "sdb",
"_type" : "user",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"username" : "秦雪",
"age" : 16,
"gender" : "女",
"about" : "this is my student",
"addrs" : [
"甘肃",
"陕西",
"天津"
]
}
}
]
},
"aggregations" : {
"all_interests" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ ]
}
}
}
文章到此就结束了,有问题可以在下方评论,技术问题可以私聊我