一 前言如果返回的结果集中很多符合条件的结果,那怎么能一眼就能看到我们想要的那个结果呢?比如下面网站所示的那样,我们搜索 如上图我们搜索百度一样。我们该怎么做呢? 二 准备数据PUT lqz/doc/4 { "name":"石头", "age":29, "from":"gu", "desc":"粗中有细,狐假虎威", "tags":["粗", "大","猛"] } 三 默认高亮显示我们来查询: GET lqz/doc/_search { "query": { "match": { "name": "石头" } }, "highlight": { "fields": { "name": {} } } } #我们使用highlight属性来实现结果高亮显示,需要的字段名称添加到fields内即可,elasticsearch会自动帮我们实现高亮。 ![]() 结果如下: { "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>" ] } } ] } }查询结果 上例中, 四 自定义高亮显示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用来实现我们的自定义标签的前半部分,在这里,我们也可以为自定义的标签添加属性和样式。post_tags实现标签的后半部分,组成一个完整的标签。至于标签中的内容,则还是交给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>" ] } } ] } }查询结果 需要注意的是:自定义标签中属性或样式中的逗号一律用英文状态的单引号表示,应该与外部 前后端分离,你怎么处理?把<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" : "龙套偏房", "age" : 22 } } ] }, "aggregations" : { "my_avg" : { "value" : 27.0 } } }查询结果 上例中,在查询结果的最后是平均值信息,可以看到是27岁。 虽然我们已经使用 GET lqz/doc/_search { "query": { "match": { "from": "gu" } }, "aggs": { "my_avg": { "avg": { "field": "age" } } }, "size": 0, "_source": ["name", "age"] } 上例中,只需要在原来的查询基础上,增加一个 ![]() { "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 } } }查询结果 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# 求年龄总和 分组查询现在我想要查询所有人的年龄段,并且按照 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 } } ] } } }查询结果 上例中,在 |
|