开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 18 天,点击查看活动详情
大家好,我是半夏之沫 😁😁 一名金融科技领域的JAVA系统研发😊😊
我希望将自己工作和学习中的经验以最朴实,最严谨的方式分享给大家,共同进步👉💓👈
👉👉👉👉👉👉👉👉💓写作不易,期待大家的关注和点赞💓👈👈👈👈👈👈👈👈
👉👉👉👉👉👉👉👉💓关注微信公众号【技术探界】 💓👈👈👈👈👈👈👈👈、
前言
在超详细Elasticsearch的查询总结(上)中,对索引相关操作,match查询,term查询,布尔查询和排序查询进行了总结。
本文将对模糊查询和聚合查询进行总结。
Elasticsearch版本:7.2.1
正文
一. 模糊查询
1. fuzzy
注意:查询条件不分词。fuzzy查询中可以通过fuzziness来指定编辑距离,即查询条件如果与文档对应字段分词后的Token的编辑距离满足fuzziness,则匹配。
编辑距离:将一个字符串变成另外一个字符串需要的最小变更次数。
语句如下所示。
GET /testindex/_search
{
"query": {
"fuzzy": {
"description": {
"value": "boll",
"fuzziness": 1
}
}
}
}
结果如下。
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0397209,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0000",
"_score" : 1.0397209,
"_source" : {
"name" : "Bill",
"description" : "This Is Demo One Bill",
"constellation" : "Sagittarius",
"age" : 20,
"sid" : "0000",
"sex" : "男性",
"status" : true,
"birthday" : "1995-08-11"
}
}
]
}
}
因为boll与bill的编辑距离是1,所以模糊匹配上了。
2. wildcard
通配符查询,注意:查询条件不分词。有两种通配符,如下所示。
*:匹配任意字符序列;?:匹配任意单字符。
* 示例语句如下。
GET /testindex/_search
{
"query": {
"wildcard": {
"description": {
"value": "bi*"
}
}
}
}
结果如下。
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0000",
"_score" : 1.0,
"_source" : {
"name" : "Bill",
"description" : "This Is Demo One Bill",
"constellation" : "Sagittarius",
"age" : 20,
"sid" : "0000",
"sex" : "男性",
"status" : true,
"birthday" : "1995-08-11"
}
}
]
}
}
? 示例语句如下。
GET /testindex/_search
{
"query": {
"wildcard": {
"description": {
"value": "bil?"
}
}
}
}
结果如下。
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0000",
"_score" : 1.0,
"_source" : {
"name" : "Bill",
"description" : "This Is Demo One Bill",
"constellation" : "Sagittarius",
"age" : 20,
"sid" : "0000",
"sex" : "男性",
"status" : true,
"birthday" : "1995-08-11"
}
}
]
}
}
二. 指标聚合
1. max,min,avg和sum
指标说明如下。
| 指标项 | 说明 |
|---|---|
| max | 最大值 |
| min | 最小值 |
| avg | 平均值 |
| sum | 累加值 |
示例语句如下。
GET /testindex/_search
{
"query": {
"match_all": {}
},
"aggs": {
"test_max": {
"max": {
"field": "age"
}
},
"test_min": {
"min": {
"field": "age"
}
},
"test_avg": {
"avg": {
"field": "age"
}
},
"test_sum": {
"sum": {
"field": "age"
}
}
}
}
结果如下。
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0000",
"_score" : 1.0,
"_source" : {
"name" : "Bill",
"description" : "This Is Demo One Bill",
"constellation" : "Sagittarius",
"age" : 20,
"sid" : "0000",
"sex" : "男性",
"status" : true,
"birthday" : "1995-08-11"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0001",
"_score" : 1.0,
"_source" : {
"name" : "Tom",
"description" : "This Is Demo Two Tom",
"constellation" : "Libra",
"age" : 31,
"sid" : "0001",
"sex" : "男性",
"status" : true,
"birthday" : "1990-04-23"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0002",
"_score" : 1.0,
"_source" : {
"name" : "Jack",
"description" : "This Is Demo Three Jack",
"constellation" : "Aquarius",
"age" : 25,
"sid" : "0002",
"sex" : "男性",
"status" : true,
"birthday" : "1994-12-08"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0003",
"_score" : 1.0,
"_source" : {
"name" : "Mary",
"description" : "This Is Demo Four Mary",
"constellation" : "Gemini",
"age" : 33,
"sid" : "0003",
"sex" : "女性",
"status" : false,
"birthday" : "1988-01-23"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0004",
"_score" : 1.0,
"_source" : {
"name" : "Wendy",
"description" : "This Is Demo Five Wendy",
"constellation" : "Virgo",
"age" : 19,
"sid" : "0004",
"sex" : "女性",
"status" : true,
"birthday" : "2001-05-05"
}
}
]
},
"aggregations" : {
"test_sum" : {
"value" : 128.0
},
"test_max" : {
"value" : 33.0
},
"test_avg" : {
"value" : 25.6
},
"test_min" : {
"value" : 19.0
}
}
}
2. value_count
统计某个字段出现的次数。语句如下。
GET /testindex/_search
{
"query": {
"match_all": {}
},
"aggs": {
"test_value_count": {
"value_count": {
"field": "name"
}
}
}
}
结果如下。
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0000",
"_score" : 1.0,
"_source" : {
"name" : "Bill",
"description" : "This Is Demo One Bill",
"constellation" : "Sagittarius",
"age" : 20,
"sid" : "0000",
"sex" : "男性",
"status" : true,
"birthday" : "1995-08-11"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0001",
"_score" : 1.0,
"_source" : {
"name" : "Tom",
"description" : "This Is Demo Two Tom",
"constellation" : "Libra",
"age" : 31,
"sid" : "0001",
"sex" : "男性",
"status" : true,
"birthday" : "1990-04-23"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0002",
"_score" : 1.0,
"_source" : {
"name" : "Jack",
"description" : "This Is Demo Three Jack",
"constellation" : "Aquarius",
"age" : 25,
"sid" : "0002",
"sex" : "男性",
"status" : true,
"birthday" : "1994-12-08"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0003",
"_score" : 1.0,
"_source" : {
"name" : "Mary",
"description" : "This Is Demo Four Mary",
"constellation" : "Gemini",
"age" : 33,
"sid" : "0003",
"sex" : "女性",
"status" : false,
"birthday" : "1988-01-23"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0004",
"_score" : 1.0,
"_source" : {
"name" : "Wendy",
"description" : "This Is Demo Five Wendy",
"constellation" : "Virgo",
"age" : 19,
"sid" : "0004",
"sex" : "女性",
"status" : true,
"birthday" : "2001-05-05"
}
}
]
},
"aggregations" : {
"test_value_count" : {
"value" : 5
}
}
}
3. cardinality
用于统计某个字段不同的值的个数。语句如下。
GET /testindex/_search
{
"query": {
"match_all": {}
},
"aggs": {
"test_cardinality": {
"cardinality": {
"field": "status"
}
}
}
}
结果如下。
{
"took" : 8,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0000",
"_score" : 1.0,
"_source" : {
"name" : "Bill",
"description" : "This Is Demo One Bill",
"constellation" : "Sagittarius",
"age" : 20,
"sid" : "0000",
"sex" : "男性",
"status" : true,
"birthday" : "1995-08-11"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0001",
"_score" : 1.0,
"_source" : {
"name" : "Tom",
"description" : "This Is Demo Two Tom",
"constellation" : "Libra",
"age" : 31,
"sid" : "0001",
"sex" : "男性",
"status" : true,
"birthday" : "1990-04-23"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0002",
"_score" : 1.0,
"_source" : {
"name" : "Jack",
"description" : "This Is Demo Three Jack",
"constellation" : "Aquarius",
"age" : 25,
"sid" : "0002",
"sex" : "男性",
"status" : true,
"birthday" : "1994-12-08"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0003",
"_score" : 1.0,
"_source" : {
"name" : "Mary",
"description" : "This Is Demo Four Mary",
"constellation" : "Gemini",
"age" : 33,
"sid" : "0003",
"sex" : "女性",
"status" : false,
"birthday" : "1988-01-23"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0004",
"_score" : 1.0,
"_source" : {
"name" : "Wendy",
"description" : "This Is Demo Five Wendy",
"constellation" : "Virgo",
"age" : 19,
"sid" : "0004",
"sex" : "女性",
"status" : true,
"birthday" : "2001-05-05"
}
}
]
},
"aggregations" : {
"test_cardinality" : {
"value" : 2
}
}
}
4. stats
方便的计算出统计信息min,max,avg,sum和count。语句如下。
GET /testindex/_search
{
"query": {
"match_all": {}
},
"aggs": {
"test_stats": {
"stats": {
"field": "age"
}
}
}
}
结果如下。
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0000",
"_score" : 1.0,
"_source" : {
"name" : "Bill",
"description" : "This Is Demo One Bill",
"constellation" : "Sagittarius",
"age" : 20,
"sid" : "0000",
"sex" : "男性",
"status" : true,
"birthday" : "1995-08-11"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0001",
"_score" : 1.0,
"_source" : {
"name" : "Tom",
"description" : "This Is Demo Two Tom",
"constellation" : "Libra",
"age" : 31,
"sid" : "0001",
"sex" : "男性",
"status" : true,
"birthday" : "1990-04-23"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0002",
"_score" : 1.0,
"_source" : {
"name" : "Jack",
"description" : "This Is Demo Three Jack",
"constellation" : "Aquarius",
"age" : 25,
"sid" : "0002",
"sex" : "男性",
"status" : true,
"birthday" : "1994-12-08"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0003",
"_score" : 1.0,
"_source" : {
"name" : "Mary",
"description" : "This Is Demo Four Mary",
"constellation" : "Gemini",
"age" : 33,
"sid" : "0003",
"sex" : "女性",
"status" : false,
"birthday" : "1988-01-23"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0004",
"_score" : 1.0,
"_source" : {
"name" : "Wendy",
"description" : "This Is Demo Five Wendy",
"constellation" : "Virgo",
"age" : 19,
"sid" : "0004",
"sex" : "女性",
"status" : true,
"birthday" : "2001-05-05"
}
}
]
},
"aggregations" : {
"test_stats" : {
"count" : 5,
"min" : 19.0,
"max" : 33.0,
"avg" : 25.6,
"sum" : 128.0
}
}
}
5. extended_stats
在stats基础上增加了平方和,方差,标准差和平均值加减标准差的统计信息。语句如下。
GET /testindex/_search
{
"query": {
"match_all": {}
},
"aggs": {
"test_extended_stats": {
"extended_stats": {
"field": "age"
}
}
}
}
结果如下。
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0000",
"_score" : 1.0,
"_source" : {
"name" : "Bill",
"description" : "This Is Demo One Bill",
"constellation" : "Sagittarius",
"age" : 20,
"sid" : "0000",
"sex" : "男性",
"status" : true,
"birthday" : "1995-08-11"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0001",
"_score" : 1.0,
"_source" : {
"name" : "Tom",
"description" : "This Is Demo Two Tom",
"constellation" : "Libra",
"age" : 31,
"sid" : "0001",
"sex" : "男性",
"status" : true,
"birthday" : "1990-04-23"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0002",
"_score" : 1.0,
"_source" : {
"name" : "Jack",
"description" : "This Is Demo Three Jack",
"constellation" : "Aquarius",
"age" : 25,
"sid" : "0002",
"sex" : "男性",
"status" : true,
"birthday" : "1994-12-08"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0003",
"_score" : 1.0,
"_source" : {
"name" : "Mary",
"description" : "This Is Demo Four Mary",
"constellation" : "Gemini",
"age" : 33,
"sid" : "0003",
"sex" : "女性",
"status" : false,
"birthday" : "1988-01-23"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0004",
"_score" : 1.0,
"_source" : {
"name" : "Wendy",
"description" : "This Is Demo Five Wendy",
"constellation" : "Virgo",
"age" : 19,
"sid" : "0004",
"sex" : "女性",
"status" : true,
"birthday" : "2001-05-05"
}
}
]
},
"aggregations" : {
"test_extended_stats" : {
"count" : 5,
"min" : 19.0,
"max" : 33.0,
"avg" : 25.6,
"sum" : 128.0,
"sum_of_squares" : 3436.0,
"variance" : 31.839999999999964,
"std_deviation" : 5.642694391866351,
"std_deviation_bounds" : {
"upper" : 36.8853887837327,
"lower" : 14.3146112162673
}
}
}
}
6. percentiles
语句如下。
GET /testindex/_search
{
"query": {
"match_phrase": {
"sex": "女性"
}
},
"aggs": {
"test_percentiles": {
"percentiles": {
"field": "age",
"percents": [
1,
5,
25,
50,
75,
95,
100
]
}
}
}
}
结果如下。
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.9624802,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0003",
"_score" : 0.9624802,
"_source" : {
"name" : "Mary",
"description" : "This Is Demo Four Mary",
"constellation" : "Gemini",
"age" : 33,
"sid" : "0003",
"sex" : "女性",
"status" : false,
"birthday" : "1988-01-23"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0004",
"_score" : 0.9624802,
"_source" : {
"name" : "Wendy",
"description" : "This Is Demo Five Wendy",
"constellation" : "Virgo",
"age" : 19,
"sid" : "0004",
"sex" : "女性",
"status" : true,
"birthday" : "2001-05-05"
}
}
]
},
"aggregations" : {
"test_percentiles" : {
"values" : {
"1.0" : 19.0,
"5.0" : 19.0,
"25.0" : 19.0,
"50.0" : 26.0,
"75.0" : 33.0,
"95.0" : 33.0,
"100.0" : 33.0
}
}
}
}
查询结果中,以50档位为例,表示age < 26的文档,占总命中文档数的50%。
三. 桶聚合
1. terms
字段的一个唯一值就是一个桶。语句如下。
GET /testindex/_search
{
"query": {
"match_all": {}
},
"aggs": {
"test_terms": {
"terms": {
"field": "status"
}
}
}
}
结果如下。
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0000",
"_score" : 1.0,
"_source" : {
"name" : "Bill",
"description" : "This Is Demo One Bill",
"constellation" : "Sagittarius",
"age" : 20,
"sid" : "0000",
"sex" : "男性",
"status" : true,
"birthday" : "1995-08-11"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0001",
"_score" : 1.0,
"_source" : {
"name" : "Tom",
"description" : "This Is Demo Two Tom",
"constellation" : "Libra",
"age" : 31,
"sid" : "0001",
"sex" : "男性",
"status" : true,
"birthday" : "1990-04-23"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0002",
"_score" : 1.0,
"_source" : {
"name" : "Jack",
"description" : "This Is Demo Three Jack",
"constellation" : "Aquarius",
"age" : 25,
"sid" : "0002",
"sex" : "男性",
"status" : true,
"birthday" : "1994-12-08"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0003",
"_score" : 1.0,
"_source" : {
"name" : "Mary",
"description" : "This Is Demo Four Mary",
"constellation" : "Gemini",
"age" : 33,
"sid" : "0003",
"sex" : "女性",
"status" : false,
"birthday" : "1988-01-23"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0004",
"_score" : 1.0,
"_source" : {
"name" : "Wendy",
"description" : "This Is Demo Five Wendy",
"constellation" : "Virgo",
"age" : 19,
"sid" : "0004",
"sex" : "女性",
"status" : true,
"birthday" : "2001-05-05"
}
}
]
},
"aggregations" : {
"test_terms" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 1,
"key_as_string" : "true",
"doc_count" : 4
},
{
"key" : 0,
"key_as_string" : "false",
"doc_count" : 1
}
]
}
}
}
上述聚合字段是status,为布尔类型,可以进行terms桶聚合,但是正常情况下,类型为text的字段,不能作为聚合字段,除非为text字段添加keyword字段。
同时可以在每个桶的基础上再进一步聚合,示例语句如下。
GET /testindex/_search
{
"query": {
"match_all": {}
},
"aggs": {
"test_terms": {
"terms": {
"field": "status"
},
"aggs": {
"test_stats": {
"stats": {
"field": "age"
}
}
}
}
}
}
查询结果如下。
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0000",
"_score" : 1.0,
"_source" : {
"name" : "Bill",
"description" : "This Is Demo One Bill",
"constellation" : "Sagittarius",
"age" : 20,
"sid" : "0000",
"sex" : "男性",
"status" : true,
"birthday" : "1995-08-11"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0001",
"_score" : 1.0,
"_source" : {
"name" : "Tom",
"description" : "This Is Demo Two Tom",
"constellation" : "Libra",
"age" : 31,
"sid" : "0001",
"sex" : "男性",
"status" : true,
"birthday" : "1990-04-23"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0002",
"_score" : 1.0,
"_source" : {
"name" : "Jack",
"description" : "This Is Demo Three Jack",
"constellation" : "Aquarius",
"age" : 25,
"sid" : "0002",
"sex" : "男性",
"status" : true,
"birthday" : "1994-12-08"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0003",
"_score" : 1.0,
"_source" : {
"name" : "Mary",
"description" : "This Is Demo Four Mary",
"constellation" : "Gemini",
"age" : 33,
"sid" : "0003",
"sex" : "女性",
"status" : false,
"birthday" : "1988-01-23"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0004",
"_score" : 1.0,
"_source" : {
"name" : "Wendy",
"description" : "This Is Demo Five Wendy",
"constellation" : "Virgo",
"age" : 19,
"sid" : "0004",
"sex" : "女性",
"status" : true,
"birthday" : "2001-05-05"
}
}
]
},
"aggregations" : {
"test_terms" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 1,
"key_as_string" : "true",
"doc_count" : 4,
"test_stats" : {
"count" : 4,
"min" : 19.0,
"max" : 31.0,
"avg" : 23.75,
"sum" : 95.0
}
},
{
"key" : 0,
"key_as_string" : "false",
"doc_count" : 1,
"test_stats" : {
"count" : 1,
"min" : 33.0,
"max" : 33.0,
"avg" : 33.0,
"sum" : 33.0
}
}
]
}
}
}
在每个桶中又进行了一次status指标聚合。
2. filter
filter用于对查询结果进行过滤,示例语句如下所示。
GET /testindex/_search
{
"query": {
"match_all": {}
},
"aggs": {
"test_filter": {
"filter": {
"match": {
"status": true
}
}
}
}
}
执行结果如下所示。
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0000",
"_score" : 1.0,
"_source" : {
"name" : "Bill",
"description" : "This Is Demo One Bill",
"constellation" : "Sagittarius",
"age" : 20,
"sid" : "0000",
"sex" : "男性",
"status" : true,
"birthday" : "1995-08-11"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0001",
"_score" : 1.0,
"_source" : {
"name" : "Tom",
"description" : "This Is Demo Two Tom",
"constellation" : "Libra",
"age" : 31,
"sid" : "0001",
"sex" : "男性",
"status" : true,
"birthday" : "1990-04-23"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0002",
"_score" : 1.0,
"_source" : {
"name" : "Jack",
"description" : "This Is Demo Three Jack",
"constellation" : "Aquarius",
"age" : 25,
"sid" : "0002",
"sex" : "男性",
"status" : true,
"birthday" : "1994-12-08"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0003",
"_score" : 1.0,
"_source" : {
"name" : "Mary",
"description" : "This Is Demo Four Mary",
"constellation" : "Gemini",
"age" : 33,
"sid" : "0003",
"sex" : "女性",
"status" : false,
"birthday" : "1988-01-23"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0004",
"_score" : 1.0,
"_source" : {
"name" : "Wendy",
"description" : "This Is Demo Five Wendy",
"constellation" : "Virgo",
"age" : 19,
"sid" : "0004",
"sex" : "女性",
"status" : true,
"birthday" : "2001-05-05"
}
}
]
},
"aggregations" : {
"test_filter" : {
"doc_count" : 4
}
}
}
3. filters
filters用于对查询结果进行多个条件过滤,一个过滤条件对应一个桶,每个条件的过滤结果之间是相互独立的,所以同一条文档可能会进入多个桶。示例语句如下所示。
GET /testindex/_search
{
"query": {
"match_all": {}
},
"aggs": {
"test_filters": {
"filters": {
"filters": {
"filter_status": {
"match": {
"status": true
}
},
"filter_age": {
"range": {
"age": {
"gte": 30
}
}
}
}
}
}
}
}
结果如下所示。
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0000",
"_score" : 1.0,
"_source" : {
"name" : "Bill",
"description" : "This Is Demo One Bill",
"constellation" : "Sagittarius",
"age" : 20,
"sid" : "0000",
"sex" : "男性",
"status" : true,
"birthday" : "1995-08-11"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0001",
"_score" : 1.0,
"_source" : {
"name" : "Tom",
"description" : "This Is Demo Two Tom",
"constellation" : "Libra",
"age" : 31,
"sid" : "0001",
"sex" : "男性",
"status" : true,
"birthday" : "1990-04-23"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0002",
"_score" : 1.0,
"_source" : {
"name" : "Jack",
"description" : "This Is Demo Three Jack",
"constellation" : "Aquarius",
"age" : 25,
"sid" : "0002",
"sex" : "男性",
"status" : true,
"birthday" : "1994-12-08"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0003",
"_score" : 1.0,
"_source" : {
"name" : "Mary",
"description" : "This Is Demo Four Mary",
"constellation" : "Gemini",
"age" : 33,
"sid" : "0003",
"sex" : "女性",
"status" : false,
"birthday" : "1988-01-23"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0004",
"_score" : 1.0,
"_source" : {
"name" : "Wendy",
"description" : "This Is Demo Five Wendy",
"constellation" : "Virgo",
"age" : 19,
"sid" : "0004",
"sex" : "女性",
"status" : true,
"birthday" : "2001-05-05"
}
}
]
},
"aggregations" : {
"test_filters" : {
"buckets" : {
"filter_age" : {
"doc_count" : 2
},
"filter_status" : {
"doc_count" : 4
}
}
}
}
}
上述filter_age和filter_status两个桶的统计数量加起来大于了索引下文档总数量,说明有文档同时进入了两个桶中。
4. range
range用于在查询结果上基于范围来聚合,一个范围就是一个桶,并且范围区间是前闭后开,被聚合的字段类型需要是数字类型。示例语句如下所示。
GET /testindex/_search
{
"query": {
"match_all": {}
},
"aggs": {
"test_range": {
"range": {
"field": "age",
"ranges": [
{
"from": 10,
"to": 20
}, {
"from": 20,
"to": 30
}, {
"from": 30,
"to": 40
}
]
}
}
}
}
查询结果如下。
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0000",
"_score" : 1.0,
"_source" : {
"name" : "Bill",
"description" : "This Is Demo One Bill",
"constellation" : "Sagittarius",
"age" : 20,
"sid" : "0000",
"sex" : "男性",
"status" : true,
"birthday" : "1995-08-11"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0001",
"_score" : 1.0,
"_source" : {
"name" : "Tom",
"description" : "This Is Demo Two Tom",
"constellation" : "Libra",
"age" : 31,
"sid" : "0001",
"sex" : "男性",
"status" : true,
"birthday" : "1990-04-23"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0002",
"_score" : 1.0,
"_source" : {
"name" : "Jack",
"description" : "This Is Demo Three Jack",
"constellation" : "Aquarius",
"age" : 25,
"sid" : "0002",
"sex" : "男性",
"status" : true,
"birthday" : "1994-12-08"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0003",
"_score" : 1.0,
"_source" : {
"name" : "Mary",
"description" : "This Is Demo Four Mary",
"constellation" : "Gemini",
"age" : 33,
"sid" : "0003",
"sex" : "女性",
"status" : false,
"birthday" : "1988-01-23"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0004",
"_score" : 1.0,
"_source" : {
"name" : "Wendy",
"description" : "This Is Demo Five Wendy",
"constellation" : "Virgo",
"age" : 19,
"sid" : "0004",
"sex" : "女性",
"status" : true,
"birthday" : "2001-05-05"
}
}
]
},
"aggregations" : {
"test_range" : {
"buckets" : [
{
"key" : "10.0-20.0",
"from" : 10.0,
"to" : 20.0,
"doc_count" : 1
},
{
"key" : "20.0-30.0",
"from" : 20.0,
"to" : 30.0,
"doc_count" : 2
},
{
"key" : "30.0-40.0",
"from" : 30.0,
"to" : 40.0,
"doc_count" : 2
}
]
}
}
}
5. histogram
histogram用于在查询结果上基于固定间隔来聚合,一个间隔就是一个桶,聚合的字段类型需要是数字类型。语句如下所示。
GET /testindex/_search
{
"query": {
"match_all": {}
},
"aggs": {
"test_histogram": {
"histogram": {
"field": "age",
"interval": 5,
"extended_bounds": {
"min": 10,
"max": 34
}
}
}
}
}
查询结果如下。
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0000",
"_score" : 1.0,
"_source" : {
"name" : "Bill",
"description" : "This Is Demo One Bill",
"constellation" : "Sagittarius",
"age" : 20,
"sid" : "0000",
"sex" : "男性",
"status" : true,
"birthday" : "1995-08-11"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0001",
"_score" : 1.0,
"_source" : {
"name" : "Tom",
"description" : "This Is Demo Two Tom",
"constellation" : "Libra",
"age" : 31,
"sid" : "0001",
"sex" : "男性",
"status" : true,
"birthday" : "1990-04-23"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0002",
"_score" : 1.0,
"_source" : {
"name" : "Jack",
"description" : "This Is Demo Three Jack",
"constellation" : "Aquarius",
"age" : 25,
"sid" : "0002",
"sex" : "男性",
"status" : true,
"birthday" : "1994-12-08"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0003",
"_score" : 1.0,
"_source" : {
"name" : "Mary",
"description" : "This Is Demo Four Mary",
"constellation" : "Gemini",
"age" : 33,
"sid" : "0003",
"sex" : "女性",
"status" : false,
"birthday" : "1988-01-23"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0004",
"_score" : 1.0,
"_source" : {
"name" : "Wendy",
"description" : "This Is Demo Five Wendy",
"constellation" : "Virgo",
"age" : 19,
"sid" : "0004",
"sex" : "女性",
"status" : true,
"birthday" : "2001-05-05"
}
}
]
},
"aggregations" : {
"test_histogram" : {
"buckets" : [
{
"key" : 10.0,
"doc_count" : 0
},
{
"key" : 15.0,
"doc_count" : 1
},
{
"key" : 20.0,
"doc_count" : 1
},
{
"key" : 25.0,
"doc_count" : 1
},
{
"key" : 30.0,
"doc_count" : 2
}
]
}
}
}
总结
本文主要对模糊查询和聚合查询进行了总结。在超详细Elasticsearch的查询总结(上)中已经对索引相关操作,match查询,term查询,布尔查询和排序查询进行了总结,希望两篇关于Elasticsearch的查询总结能够帮助大家。
大家好,我是半夏之沫 😁😁 一名金融科技领域的JAVA系统研发😊😊
我希望将自己工作和学习中的经验以最朴实,最严谨的方式分享给大家,共同进步👉💓👈
👉👉👉👉👉👉👉👉💓写作不易,期待大家的关注和点赞💓👈👈👈👈👈👈👈👈
👉👉👉👉👉👉👉👉💓关注微信公众号【技术探界】 💓👈👈👈👈👈👈👈👈
开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 18 天,点击查看活动详情