一 前言如果返回的結(jié)果集中很多符合條件的結(jié)果,那怎么能一眼就能看到我們想要的那個結(jié)果呢?比如下面網(wǎng)站所示的那樣,我們搜索
如上圖我們搜索百度一樣。我們該怎么做呢? 二 準備數(shù)據(jù)PUT lqz/doc/4
{
"name":"石頭",
"age":29,
"from":"gu",
"desc":"粗中有細,狐假虎威",
"tags":["粗", "大","猛"]
}三 默認高亮顯示我們來查詢: GET lqz/doc/_search
{
"query": {
"match": {
"name": "石頭"
}
},
"highlight": {
"fields": {
"name": {}
}
}
}
#我們使用highlight屬性來實現(xiàn)結(jié)果高亮顯示,需要的字段名稱添加到fields內(nèi)即可,elasticsearch會自動幫我們實現(xiàn)高亮。![]() 結(jié)果如下:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.5098256,
"hits" : [
{
"_index" : "lqz",
"_type" : "doc",
"_id" : "4",
"_score" : 1.5098256,
"_source" : {
"name" : "石頭",
"age" : 29,
"from" : "gu",
"desc" : "粗中有細,狐假虎威",
"tags" : [
"粗",
"大",
"猛"
]
},
"highlight" : {
"name" : [
"<em>石</em><em>頭</em>"
]
}
}
]
}
}查詢結(jié)果上例中, 四 自定義高亮顯示GET lqz/chengyuan/_search
{
"query": {
"match": {
"from": "gu"
}
},
"highlight": {
"pre_tags": "<b class='key' style='color:red'>",
"post_tags": "</b>",
"fields": {
"from": {}
}
}
}
上例中,在highlight中,pre_tags用來實現(xiàn)我們的自定義標簽的前半部分,在這里,我們也可以為自定義的標簽添加屬性和樣式。post_tags實現(xiàn)標簽的后半部分,組成一個完整的標簽。至于標簽中的內(nèi)容,則還是交給fields來完成。![]() {
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "lqz",
"_type" : "chengyuan",
"_id" : "1",
"_score" : 0.5753642,
"_source" : {
"name" : "老二",
"age" : 30,
"sex" : "male",
"birth" : "1070-10-11",
"from" : "gu",
"desc" : "皮膚黑,武器長,性格直",
"tags" : [
"黑",
"長",
"直"
]
},
"highlight" : {
"name" : [
"<b class='key' style='color:red'>老</b><b class='key' style='color:red'>二</b>"
]
}
}
]
}
}查詢結(jié)果需要注意的是:自定義標簽中屬性或樣式中的逗號一律用英文狀態(tài)的單引號表示,應(yīng)該與外部 前后端分離,你怎么處理?把<b class='key' style='color:red'>串直接以json格式返回,前端自行渲染 Elasticsearch之聚合查詢
avg# 查詢`from`是`gu`的人的平均年齡。
# select max(age) as my_avg from user;
GET lqz/doc/_search
{
"query": {
"match": {
"from": "gu"
}
},
"aggs": {
"my_avg": {
"avg": {
"field": "age"
}
}
},
"_source": ["name", "age"]
}上例中,首先匹配查詢 ![]() {
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 0.6931472,
"hits" : [
{
"_index" : "lqz",
"_type" : "doc",
"_id" : "4",
"_score" : 0.6931472,
"_source" : {
"name" : "石頭",
"age" : 29
}
},
{
"_index" : "lqz",
"_type" : "doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"name" : "顧老二",
"age" : 30
}
},
{
"_index" : "lqz",
"_type" : "doc",
"_id" : "3",
"_score" : 0.2876821,
"_source" : {
"name" : "龍?zhí)灼?quot;,
"age" : 22
}
}
]
},
"aggregations" : {
"my_avg" : {
"value" : 27.0
}
}
}查詢結(jié)果上例中,在查詢結(jié)果的最后是平均值信息,可以看到是27歲。 雖然我們已經(jīng)使用 GET lqz/doc/_search
{
"query": {
"match": {
"from": "gu"
}
},
"aggs": {
"my_avg": {
"avg": {
"field": "age"
}
}
},
"size": 0,
"_source": ["name", "age"]
}上例中,只需要在原來的查詢基礎(chǔ)上,增加一個 ![]() {
"took" : 8,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"my_avg" : {
"value" : 27.0
}
}
}查詢結(jié)果maxGET lqz/doc/_search
{
"query": {
"match": {
"from": "gu"
}
},
"aggs": {
"my_max": {
"max": {
"field": "age"
}
}
},
"size": 0
}上例中,只需要在查詢條件中將 minGET lqz/doc/_search
{
"query": {
"match": {
"from": "gu"
}
},
"aggs": {
"my_min": {
"min": {
"field": "age"
}
}
},
"size": 0
}sum# 求年齡總和 分組查詢現(xiàn)在我想要查詢所有人的年齡段,并且按照 GET lqz/doc/_search
{
"size": 0,
"query": {
"match_all": {}
},
"aggs": {
"age_group": {
"range": {
"field": "age",
"ranges": [
{
"from": 15,
"to": 20
},
{
"from": 20,
"to": 25
},
{
"from": 25,
"to": 30
}
]
},
"aggs": {
"my_avg": {
"avg": {
"field": "age"
}
}
}
}
}
}![]() {
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 5,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"age_group" : {
"buckets" : [
{
"key" : "15.0-20.0",
"from" : 15.0,
"to" : 20.0,
"doc_count" : 1,
"my_avg" : {
"value" : 18.0
}
},
{
"key" : "20.0-25.0",
"from" : 20.0,
"to" : 25.0,
"doc_count" : 1,
"my_avg" : {
"value" : 22.0
}
},
{
"key" : "25.0-30.0",
"from" : 25.0,
"to" : 30.0,
"doc_count" : 2,
"my_avg" : {
"value" : 27.0
}
}
]
}
}
}查詢結(jié)果上例中,在 |
|
|