上面我們已經介紹了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" : [ ]
}
}
}
文章到此就結束了,有問題可以在下方評論,技術問題可以私聊我
