介绍
根据之前记录,已完成对于es_api的了解,但是对于es的query应该具有熟练的应用,所以本文档记录一些高频或特殊的聚合查询测试,需要补充的是,如果考虑在es做聚合查询,请考虑系统的性能问题
暂未包含所有函数,后续有需要再更新
该索引是学生信息,包含学生姓名,学生班级,各科目成绩
共填充50条测试数据
建立索引语句
PUT student_scores
{
"mappings": {
"properties": {
"student_id": { "type": "keyword" },
"name": { "type": "keyword" },
"class": { "type": "keyword" },
"math": { "type": "integer" },
"english": { "type": "integer" },
"physics": { "type": "integer" },
"chemistry": { "type": "integer" },
"biology": { "type": "integer" },
"created_at": { "type": "date" }
}
}
}
填充数据语句
POST student_scores/_bulk
{"index":{}}
{"student_id":"S001","name":"张三","class":"一班","math":92,"english":88,"physics":79,"chemistry":81,"biology":95,"created_at":"2025-01-01"}
{"index":{}}
{"student_id":"S002","name":"李四","class":"一班","math":85,"english":91,"physics":83,"chemistry":90,"biology":78,"created_at":"2025-01-01"}
{"index":{}}
{"student_id":"S003","name":"王五","class":"一班","math":97,"english":80,"physics":88,"chemistry":92,"biology":91,"created_at":"2025-01-02"}
{"index":{}}
{"student_id":"S004","name":"赵六","class":"二班","math":73,"english":95,"physics":91,"chemistry":85,"biology":88,"created_at":"2025-01-02"}
{"index":{}}
{"student_id":"S005","name":"孙七","class":"二班","math":90,"english":76,"physics":84,"chemistry":93,"biology":82,"created_at":"2025-01-03"}
{"index":{}}
{"student_id":"S006","name":"周八","class":"二班","math":81,"english":89,"physics":92,"chemistry":78,"biology":87,"created_at":"2025-01-03"}
{"index":{}}
{"student_id":"S007","name":"吴九","class":"三班","math":95,"english":91,"physics":94,"chemistry":89,"biology":90,"created_at":"2025-01-03"}
{"index":{}}
{"student_id":"S008","name":"郑十","class":"三班","math":66,"english":72,"physics":70,"chemistry":69,"biology":75,"created_at":"2025-01-04"}
{"index":{}}
{"student_id":"S009","name":"冯十一","class":"三班","math":88,"english":86,"physics":89,"chemistry":80,"biology":82,"created_at":"2025-01-04"}
{"index":{}}
{"student_id":"S010","name":"陈十二","class":"一班","math":91,"english":84,"physics":77,"chemistry":88,"biology":89,"created_at":"2025-01-05"}
{"index":{}}
{"student_id":"S011","name":"钱十三","class":"一班","math":78,"english":90,"physics":85,"chemistry":79,"biology":83,"created_at":"2025-01-05"}
{"index":{}}
{"student_id":"S012","name":"黄十四","class":"二班","math":84,"english":77,"physics":90,"chemistry":88,"biology":91,"created_at":"2025-01-05"}
{"index":{}}
{"student_id":"S013","name":"孔十五","class":"三班","math":92,"english":94,"physics":87,"chemistry":91,"biology":85,"created_at":"2025-01-06"}
{"index":{}}
{"student_id":"S014","name":"尹十六","class":"二班","math":75,"english":82,"physics":78,"chemistry":86,"biology":80,"created_at":"2025-01-06"}
{"index":{}}
{"student_id":"S015","name":"许十七","class":"一班","math":89,"english":88,"physics":92,"chemistry":84,"biology":90,"created_at":"2025-01-06"}
{"index":{}}
{"student_id":"S016","name":"郭十八","class":"三班","math":94,"english":79,"physics":93,"chemistry":95,"biology":88,"created_at":"2025-01-07"}
{"index":{}}
{"student_id":"S017","name":"赖十九","class":"二班","math":77,"english":85,"physics":74,"chemistry":80,"biology":76,"created_at":"2025-01-07"}
{"index":{}}
{"student_id":"S018","name":"贺二十","class":"一班","math":82,"english":93,"physics":90,"chemistry":87,"biology":92,"created_at":"2025-01-07"}
{"index":{}}
{"student_id":"S019","name":"施二一","class":"三班","math":90,"english":76,"physics":82,"chemistry":83,"biology":81,"created_at":"2025-01-07"}
{"index":{}}
{"student_id":"S020","name":"鲁二二","class":"一班","math":85,"english":82,"physics":89,"chemistry":90,"biology":78,"created_at":"2025-01-08"}
{"index":{}}
{"student_id":"S021","name":"魏二三","class":"三班","math":96,"english":87,"physics":95,"chemistry":94,"biology":93,"created_at":"2025-01-08"}
{"index":{}}
{"student_id":"S022","name":"陶二四","class":"二班","math":78,"english":75,"physics":70,"chemistry":73,"biology":76,"created_at":"2025-01-08"}
{"index":{}}
{"student_id":"S023","name":"章二五","class":"二班","math":88,"english":91,"physics":90,"chemistry":92,"biology":89,"created_at":"2025-01-08"}
{"index":{}}
{"student_id":"S024","name":"苏二六","class":"一班","math":93,"english":82,"physics":88,"chemistry":85,"biology":90,"created_at":"2025-01-09"}
{"index":{}}
{"student_id":"S025","name":"潘二七","class":"一班","math":74,"english":73,"physics":76,"chemistry":75,"biology":79,"created_at":"2025-01-09"}
{"index":{}}
{"student_id":"S026","name":"丁二八","class":"三班","math":91,"english":90,"physics":93,"chemistry":92,"biology":94,"created_at":"2025-01-09"}
{"index":{}}
{"student_id":"S027","name":"洪二九","class":"二班","math":80,"english":79,"physics":77,"chemistry":78,"biology":81,"created_at":"2025-01-09"}
{"index":{}}
{"student_id":"S028","name":"包三十","class":"三班","math":87,"english":85,"physics":88,"chemistry":84,"biology":86,"created_at":"2025-01-10"}
{"index":{}}
{"student_id":"S029","name":"石三一","class":"一班","math":95,"english":96,"physics":94,"chemistry":93,"biology":97,"created_at":"2025-01-10"}
{"index":{}}
{"student_id":"S030","name":"向三二","class":"二班","math":62,"english":71,"physics":68,"chemistry":65,"biology":70,"created_at":"2025-01-10"}
{"index":{}}
{"student_id":"S031","name":"黎三三","class":"三班","math":85,"english":83,"physics":87,"chemistry":89,"biology":82,"created_at":"2025-01-10"}
{"index":{}}
{"student_id":"S032","name":"谭三四","class":"二班","math":91,"english":88,"physics":90,"chemistry":93,"biology":89,"created_at":"2025-01-10"}
{"index":{}}
{"student_id":"S033","name":"廖三五","class":"一班","math":83,"english":79,"physics":81,"chemistry":84,"biology":78,"created_at":"2025-01-10"}
{"index":{}}
{"student_id":"S034","name":"傅三六","class":"三班","math":77,"english":82,"physics":80,"chemistry":78,"biology":81,"created_at":"2025-01-10"}
{"index":{}}
{"student_id":"S035","name":"程三七","class":"一班","math":94,"english":92,"physics":93,"chemistry":91,"biology":96,"created_at":"2025-01-10"}
{"index":{}}
{"student_id":"S036","name":"吉三八","class":"二班","math":88,"english":84,"physics":87,"chemistry":89,"biology":85,"created_at":"2025-01-10"}
{"index":{}}
{"student_id":"S037","name":"彭三九","class":"三班","math":90,"english":93,"physics":92,"chemistry":94,"biology":91,"created_at":"2025-01-10"}
{"index":{}}
{"student_id":"S038","name":"汤四十","class":"一班","math":79,"english":81,"physics":80,"chemistry":78,"biology":82,"created_at":"2025-01-10"}
{"index":{}}
{"student_id":"S039","name":"覃四一","class":"二班","math":86,"english":88,"physics":84,"chemistry":87,"biology":89,"created_at":"2025-01-10"}
{"index":{}}
{"student_id":"S040","name":"夏四二","class":"三班","math":98,"english":94,"physics":97,"chemistry":96,"biology":95,"created_at":"2025-01-10"}
{"index":{}}
{"student_id":"S041","name":"凌四三","class":"一班","math":82,"english":85,"physics":84,"chemistry":86,"biology":83,"created_at":"2025-01-10"}
{"index":{}}
{"student_id":"S042","name":"易四四","class":"一班","math":93,"english":89,"physics":90,"chemistry":91,"biology":92,"created_at":"2025-01-10"}
{"index":{}}
{"student_id":"S043","name":"迟四五","class":"二班","math":74,"english":78,"physics":77,"chemistry":79,"biology":76,"created_at":"2025-01-10"}
{"index":{}}
{"student_id":"S044","name":"宫四六","class":"二班","math":89,"english":90,"physics":88,"chemistry":91,"biology":87,"created_at":"2025-01-10"}
{"index":{}}
{"student_id":"S045","name":"左四七","class":"三班","math":84,"english":82,"physics":81,"chemistry":83,"biology":80,"created_at":"2025-01-10"}
{"index":{}}
{"student_id":"S046","name":"田四八","class":"一班","math":97,"english":95,"physics":96,"chemistry":94,"biology":98,"created_at":"2025-01-10"}
{"index":{}}
{"student_id":"S047","name":"韩四九","class":"二班","math":71,"english":73,"physics":70,"chemistry":72,"biology":75,"created_at":"2025-01-10"}
{"index":{}}
{"student_id":"S048","name":"邹五十","class":"三班","math":92,"english":91,"physics":94,"chemistry":93,"biology":90,"created_at":"2025-01-10"}
{"index":{}}
{"student_id":"S049","name":"卢五一","class":"二班","math":68,"english":70,"physics":71,"chemistry":73,"biology":69,"created_at":"2025-01-10"}
{"index":{}}
{"student_id":"S050","name":"花五二","class":"三班","math":86,"english":87,"physics":89,"chemistry":88,"biology":85,"created_at":"2025-01-10"}
问题以及对应post
非聚合查询
查询姓张的所有学生信息
查询语句
POST student_scores/_search
{
"query":{
"wildcard": {
"name": {
"value": "张*"
}
}
}
}
执行结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "student_scores",
"_id": "ZiRap5oBBUWbZYrK_vaQ",
"_score": 1,
"_source": {
"student_id": "S001",
"name": "张三",
"class": "一班",
"math": 92,
"english": 88,
"physics": 79,
"chemistry": 81,
"biology": 95,
"created_at": "2025-01-01"
}
}
]
}
}
查询一班学生的所有学生姓名
查询语句
POST student_scores/_search
{
"query":{
"term": {
"class": {
"value": "一班"
}
}
},
"_source":["name"]
}
执行结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 17,
"relation": "eq"
},
"max_score": 1.0696248,
"hits": [
{
"_index": "student_scores",
"_id": "ZiRap5oBBUWbZYrK_vaQ",
"_score": 1.0696248,
"_source": {
"name": "张三"
}
},
{
"_index": "student_scores",
"_id": "ZyRap5oBBUWbZYrK_vaQ",
"_score": 1.0696248,
"_source": {
"name": "李四"
}
},
{
"_index": "student_scores",
"_id": "aCRap5oBBUWbZYrK_vaQ",
"_score": 1.0696248,
"_source": {
"name": "王五"
}
},
{
"_index": "student_scores",
"_id": "byRap5oBBUWbZYrK_vaR",
"_score": 1.0696248,
"_source": {
"name": "陈十二"
}
},
{
"_index": "student_scores",
"_id": "cCRap5oBBUWbZYrK_vaR",
"_score": 1.0696248,
"_source": {
"name": "钱十三"
}
},
{
"_index": "student_scores",
"_id": "dCRap5oBBUWbZYrK_vaR",
"_score": 1.0696248,
"_source": {
"name": "许十七"
}
},
{
"_index": "student_scores",
"_id": "dyRap5oBBUWbZYrK_vaR",
"_score": 1.0696248,
"_source": {
"name": "贺二十"
}
},
{
"_index": "student_scores",
"_id": "eSRap5oBBUWbZYrK_vaR",
"_score": 1.0696248,
"_source": {
"name": "鲁二二"
}
},
{
"_index": "student_scores",
"_id": "fSRap5oBBUWbZYrK_vaR",
"_score": 1.0696248,
"_source": {
"name": "苏二六"
}
},
{
"_index": "student_scores",
"_id": "fiRap5oBBUWbZYrK_vaR",
"_score": 1.0696248,
"_source": {
"name": "潘二七"
}
}
]
}
}
查询数学成绩大于等于90分的学生姓名与数学成绩
查询语句
POST student_scores/_search
{
"query":{
"range": {
"math": {
"gte":90
}
}
},
"_source":["name","math"]
}
执行结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 19,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "student_scores",
"_id": "ZiRap5oBBUWbZYrK_vaQ",
"_score": 1,
"_source": {
"name": "张三",
"math": 92
}
},
{
"_index": "student_scores",
"_id": "aCRap5oBBUWbZYrK_vaQ",
"_score": 1,
"_source": {
"name": "王五",
"math": 97
}
},
{
"_index": "student_scores",
"_id": "aiRap5oBBUWbZYrK_vaR",
"_score": 1,
"_source": {
"name": "孙七",
"math": 90
}
},
{
"_index": "student_scores",
"_id": "bCRap5oBBUWbZYrK_vaR",
"_score": 1,
"_source": {
"name": "吴九",
"math": 95
}
},
{
"_index": "student_scores",
"_id": "byRap5oBBUWbZYrK_vaR",
"_score": 1,
"_source": {
"name": "陈十二",
"math": 91
}
},
{
"_index": "student_scores",
"_id": "ciRap5oBBUWbZYrK_vaR",
"_score": 1,
"_source": {
"name": "孔十五",
"math": 92
}
},
{
"_index": "student_scores",
"_id": "dSRap5oBBUWbZYrK_vaR",
"_score": 1,
"_source": {
"name": "郭十八",
"math": 94
}
},
{
"_index": "student_scores",
"_id": "eCRap5oBBUWbZYrK_vaR",
"_score": 1,
"_source": {
"name": "施二一",
"math": 90
}
},
{
"_index": "student_scores",
"_id": "eiRap5oBBUWbZYrK_vaR",
"_score": 1,
"_source": {
"name": "魏二三",
"math": 96
}
},
{
"_index": "student_scores",
"_id": "fSRap5oBBUWbZYrK_vaR",
"_score": 1,
"_source": {
"name": "苏二六",
"math": 93
}
}
]
}
}
查询数学大于等于90分或英语大于等于90分的学生
查询语句
POST student_scores/_search
{
"query":{
"bool": {
"should": [
{"range":{"math":{"gte":90}}},
{"range":{"english":{"gte":90}}}
]
}
}
}
执行结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 25,
"relation": "eq"
},
"max_score": 2,
"hits": [
{
"_index": "student_scores",
"_id": "bCRap5oBBUWbZYrK_vaR",
"_score": 2,
"_source": {
"student_id": "S007",
"name": "吴九",
"class": "三班",
"math": 95,
"english": 91,
"physics": 94,
"chemistry": 89,
"biology": 90,
"created_at": "2025-01-03"
}
},
{
"_index": "student_scores",
"_id": "ciRap5oBBUWbZYrK_vaR",
"_score": 2,
"_source": {
"student_id": "S013",
"name": "孔十五",
"class": "三班",
"math": 92,
"english": 94,
"physics": 87,
"chemistry": 91,
"biology": 85,
"created_at": "2025-01-06"
}
},
{
"_index": "student_scores",
"_id": "fyRap5oBBUWbZYrK_vaR",
"_score": 2,
"_source": {
"student_id": "S026",
"name": "丁二八",
"class": "三班",
"math": 91,
"english": 90,
"physics": 93,
"chemistry": 92,
"biology": 94,
"created_at": "2025-01-09"
}
},
{
"_index": "student_scores",
"_id": "giRap5oBBUWbZYrK_vaR",
"_score": 2,
"_source": {
"student_id": "S029",
"name": "石三一",
"class": "一班",
"math": 95,
"english": 96,
"physics": 94,
"chemistry": 93,
"biology": 97,
"created_at": "2025-01-10"
}
},
{
"_index": "student_scores",
"_id": "iCRap5oBBUWbZYrK_vaR",
"_score": 2,
"_source": {
"student_id": "S035",
"name": "程三七",
"class": "一班",
"math": 94,
"english": 92,
"physics": 93,
"chemistry": 91,
"biology": 96,
"created_at": "2025-01-10"
}
},
{
"_index": "student_scores",
"_id": "iiRap5oBBUWbZYrK_vaR",
"_score": 2,
"_source": {
"student_id": "S037",
"name": "彭三九",
"class": "三班",
"math": 90,
"english": 93,
"physics": 92,
"chemistry": 94,
"biology": 91,
"created_at": "2025-01-10"
}
},
{
"_index": "student_scores",
"_id": "jSRap5oBBUWbZYrK_vaR",
"_score": 2,
"_source": {
"student_id": "S040",
"name": "夏四二",
"class": "三班",
"math": 98,
"english": 94,
"physics": 97,
"chemistry": 96,
"biology": 95,
"created_at": "2025-01-10"
}
},
{
"_index": "student_scores",
"_id": "kyRap5oBBUWbZYrK_vaR",
"_score": 2,
"_source": {
"student_id": "S046",
"name": "田四八",
"class": "一班",
"math": 97,
"english": 95,
"physics": 96,
"chemistry": 94,
"biology": 98,
"created_at": "2025-01-10"
}
},
{
"_index": "student_scores",
"_id": "lSRap5oBBUWbZYrK_vaR",
"_score": 2,
"_source": {
"student_id": "S048",
"name": "邹五十",
"class": "三班",
"math": 92,
"english": 91,
"physics": 94,
"chemistry": 93,
"biology": 90,
"created_at": "2025-01-10"
}
},
{
"_index": "student_scores",
"_id": "ZiRap5oBBUWbZYrK_vaQ",
"_score": 1,
"_source": {
"student_id": "S001",
"name": "张三",
"class": "一班",
"math": 92,
"english": 88,
"physics": 79,
"chemistry": 81,
"biology": 95,
"created_at": "2025-01-01"
}
}
]
}
}
查询至少3门成绩达到95分的学生
查询语句
POST student_scores/_search
{
"query":{
"bool": {
"minimum_should_match": 3,
"should": [
{"range":{"math":{"gte":95}}},
{"range":{"english":{"gte":95}}},
{"range":{"physics":{"gte":95}}},
{"range":{"chemistry":{"gte":95}}},
{"range":{"biology":{"gte":95}}}
]
}
}
}
执行结果
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 4,
"hits": [
{
"_index": "student_scores",
"_id": "jSRap5oBBUWbZYrK_vaR",
"_score": 4,
"_source": {
"student_id": "S040",
"name": "夏四二",
"class": "三班",
"math": 98,
"english": 94,
"physics": 97,
"chemistry": 96,
"biology": 95,
"created_at": "2025-01-10"
}
},
{
"_index": "student_scores",
"_id": "kyRap5oBBUWbZYrK_vaR",
"_score": 4,
"_source": {
"student_id": "S046",
"name": "田四八",
"class": "一班",
"math": 97,
"english": 95,
"physics": 96,
"chemistry": 94,
"biology": 98,
"created_at": "2025-01-10"
}
},
{
"_index": "student_scores",
"_id": "giRap5oBBUWbZYrK_vaR",
"_score": 3,
"_source": {
"student_id": "S029",
"name": "石三一",
"class": "一班",
"math": 95,
"english": 96,
"physics": 94,
"chemistry": 93,
"biology": 97,
"created_at": "2025-01-10"
}
}
]
}
}
查询一门成绩100或者至少3们成绩达到95或者全部成绩大于90的学生
查询语句
POST student_scores/_search
{
"query":{
"bool": {
"minimum_should_match": 1,
"should": [
{"range":{"math":{"gte":100}}},
{"range":{"english":{"gte":100}}},
{"range":{"physics":{"gte":100}}},
{"range":{"chemistry":{"gte":100}}},
{"range":{"biology":{"gte":100}}},
{"bool":
{"minimum_should_match": 3,
"should":[
{"range":{"math":{"gte":95}}},
{"range":{"english":{"gte":95}}},
{"range":{"physics":{"gte":95}}},
{"range":{"chemistry":{"gte":95}}},
{"range":{"biology":{"gte":95}}}
]}
},
{"bool":
{"must":[
{"range":{"math":{"gte":90}}},
{"range":{"english":{"gte":90}}},
{"range":{"physics":{"gte":90}}},
{"range":{"chemistry":{"gte":90}}},
{"range":{"biology":{"gte":90}}}
]}
}
]
}
}
}
执行结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 7,
"relation": "eq"
},
"max_score": 9,
"hits": [
{
"_index": "student_scores",
"_id": "jSRap5oBBUWbZYrK_vaR",
"_score": 9,
"_source": {
"student_id": "S040",
"name": "夏四二",
"class": "三班",
"math": 98,
"english": 94,
"physics": 97,
"chemistry": 96,
"biology": 95,
"created_at": "2025-01-10"
}
},
{
"_index": "student_scores",
"_id": "kyRap5oBBUWbZYrK_vaR",
"_score": 9,
"_source": {
"student_id": "S046",
"name": "田四八",
"class": "一班",
"math": 97,
"english": 95,
"physics": 96,
"chemistry": 94,
"biology": 98,
"created_at": "2025-01-10"
}
},
{
"_index": "student_scores",
"_id": "giRap5oBBUWbZYrK_vaR",
"_score": 8,
"_source": {
"student_id": "S029",
"name": "石三一",
"class": "一班",
"math": 95,
"english": 96,
"physics": 94,
"chemistry": 93,
"biology": 97,
"created_at": "2025-01-10"
}
},
{
"_index": "student_scores",
"_id": "fyRap5oBBUWbZYrK_vaR",
"_score": 5,
"_source": {
"student_id": "S026",
"name": "丁二八",
"class": "三班",
"math": 91,
"english": 90,
"physics": 93,
"chemistry": 92,
"biology": 94,
"created_at": "2025-01-09"
}
},
{
"_index": "student_scores",
"_id": "iCRap5oBBUWbZYrK_vaR",
"_score": 5,
"_source": {
"student_id": "S035",
"name": "程三七",
"class": "一班",
"math": 94,
"english": 92,
"physics": 93,
"chemistry": 91,
"biology": 96,
"created_at": "2025-01-10"
}
},
{
"_index": "student_scores",
"_id": "iiRap5oBBUWbZYrK_vaR",
"_score": 5,
"_source": {
"student_id": "S037",
"name": "彭三九",
"class": "三班",
"math": 90,
"english": 93,
"physics": 92,
"chemistry": 94,
"biology": 91,
"created_at": "2025-01-10"
}
},
{
"_index": "student_scores",
"_id": "lSRap5oBBUWbZYrK_vaR",
"_score": 5,
"_source": {
"student_id": "S048",
"name": "邹五十",
"class": "三班",
"math": 92,
"english": 91,
"physics": 94,
"chemistry": 93,
"biology": 90,
"created_at": "2025-01-10"
}
}
]
}
}
聚合查询
查询全部数据下数学成绩的前三名学生姓名和班级
查询语句
GET student_scores/_search
{
"size": 0,
"aggs": {
"top_math": {
"top_hits": {
"sort":[
{"math":"desc"},
{"student_id":"asc"}],
"size":3,
"_source": ["name","class"]
}
}
}
}
- size放弃query.hits
- aggs.sort可以指定多个字段,处理同值顺序问题·
- aggs下依然支持指定source
执行结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 50,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"top_math": {
"hits": {
"total": {
"value": 50,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "student_scores",
"_id": "jSRap5oBBUWbZYrK_vaR",
"_score": null,
"_source": {
"name": "夏四二",
"class": "三班"
},
"sort": [
98,
"S040"
]
},
{
"_index": "student_scores",
"_id": "aCRap5oBBUWbZYrK_vaQ",
"_score": null,
"_source": {
"name": "王五",
"class": "一班"
},
"sort": [
97,
"S003"
]
},
{
"_index": "student_scores",
"_id": "kyRap5oBBUWbZYrK_vaR",
"_score": null,
"_source": {
"name": "田四八",
"class": "一班"
},
"sort": [
97,
"S046"
]
}
]
}
}
}
}
查询每个班级的数学成绩前三名的学生姓名、学号
查询语句
GET student_scores/_search
{
"size": 0,
"aggs": {
"top_math": {
"terms": { "field": "class", "size": 3 },
"aggs": {
"top_students": {
"top_hits": {
"sort": [
{ "math": "desc" },
{"student_id":"asc"}
],
"size": 3,
"_source": ["name","student_id"]
}
}
}
}
}
}
- 一次性计算过大的
terms.size可能会有内存问题 terms桶聚合后必须设置size,否则会有默认设置,低版本es受index.max_terms_count最大计算数量限制,terms.size超出此值后可能会出现警告或者数据问题。这就限制了terms的桶聚合查询必须对于keySize是知晓的,是限制的。- 比如此题如果未来可能扩展更多的class,那么现在这个size=3在之后使用是一定会出问题的。要么查询前获取一下数量,要么使用
composite来分页获取
执行结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 50,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"top_math": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "一班",
"doc_count": 17,
"top_students": {
"hits": {
"total": {
"value": 17,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "student_scores",
"_id": "aCRap5oBBUWbZYrK_vaQ",
"_score": null,
"_source": {
"student_id": "S003",
"name": "王五"
},
"sort": [
97,
"S003"
]
},
{
"_index": "student_scores",
"_id": "kyRap5oBBUWbZYrK_vaR",
"_score": null,
"_source": {
"student_id": "S046",
"name": "田四八"
},
"sort": [
97,
"S046"
]
},
{
"_index": "student_scores",
"_id": "giRap5oBBUWbZYrK_vaR",
"_score": null,
"_source": {
"student_id": "S029",
"name": "石三一"
},
"sort": [
95,
"S029"
]
}
]
}
}
},
{
"key": "二班",
"doc_count": 17,
"top_students": {
"hits": {
"total": {
"value": 17,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "student_scores",
"_id": "hSRap5oBBUWbZYrK_vaR",
"_score": null,
"_source": {
"student_id": "S032",
"name": "谭三四"
},
"sort": [
91,
"S032"
]
},
{
"_index": "student_scores",
"_id": "aiRap5oBBUWbZYrK_vaR",
"_score": null,
"_source": {
"student_id": "S005",
"name": "孙七"
},
"sort": [
90,
"S005"
]
},
{
"_index": "student_scores",
"_id": "kSRap5oBBUWbZYrK_vaR",
"_score": null,
"_source": {
"student_id": "S044",
"name": "宫四六"
},
"sort": [
89,
"S044"
]
}
]
}
}
},
{
"key": "三班",
"doc_count": 16,
"top_students": {
"hits": {
"total": {
"value": 16,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "student_scores",
"_id": "jSRap5oBBUWbZYrK_vaR",
"_score": null,
"_source": {
"student_id": "S040",
"name": "夏四二"
},
"sort": [
98,
"S040"
]
},
{
"_index": "student_scores",
"_id": "eiRap5oBBUWbZYrK_vaR",
"_score": null,
"_source": {
"student_id": "S021",
"name": "魏二三"
},
"sort": [
96,
"S021"
]
},
{
"_index": "student_scores",
"_id": "bCRap5oBBUWbZYrK_vaR",
"_score": null,
"_source": {
"student_id": "S007",
"name": "吴九"
},
"sort": [
95,
"S007"
]
}
]
}
}
}
]
}
}
}
查询学生们的班级数量
查询语句
GET student_scores/_search
{
"size": 0,
"aggs": {
"class_count": {
"cardinality": {
"field": "class"
}
}
}
}
cardinality聚合函数有点拗口,但是value_count不去重,且不支持distinct配置。
执行结果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 50,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"class_count": {
"value": 3
}
}
}
查询每个班级的数学成绩前三名的学生姓名、学号(plus版)
查询语句
POST student_scores/_search
{
"size": 0,
"aggs": {
"class_list": {
"composite": {
"after": {
"class_name": ""
},
"size": 2,
"sources": [
{
"class_name": {
"terms": {
"field": "class"
}
}
}
]
},
"aggs": {
"top_students": {
"top_hits": {
"size": 2,
"sort": [
{
"math": "desc"
},
{
"student_id": "desc"
}
],
"_source": [
"name",
"class"
]
}
}
}
}
}
}
- 我使用的es版本是8.14,翻页使用的关键字是
composite.after,有些版本可能是composite.after_key,具体关键字可以试下,然后查看报错提示。 after内的key是翻页的key,必须要与下面source内某个key保持一致,初次请求时,可以设置为空,代表第0页。- 最后一页的的
after_key依然是最后一页的最后一个元素指定的sources字段值,最后一页可能不满size。
执行结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 50,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"class_list": {
"after_key": {
"class_name": "三班"
},
"buckets": [
{
"key": {
"class_name": "一班"
},
"doc_count": 17,
"top_students": {
"hits": {
"total": {
"value": 17,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "student_scores",
"_id": "kyRap5oBBUWbZYrK_vaR",
"_score": null,
"_source": {
"name": "田四八",
"class": "一班"
},
"sort": [
97,
"S046"
]
},
{
"_index": "student_scores",
"_id": "aCRap5oBBUWbZYrK_vaQ",
"_score": null,
"_source": {
"name": "王五",
"class": "一班"
},
"sort": [
97,
"S003"
]
}
]
}
}
},
{
"key": {
"class_name": "三班"
},
"doc_count": 16,
"top_students": {
"hits": {
"total": {
"value": 16,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "student_scores",
"_id": "jSRap5oBBUWbZYrK_vaR",
"_score": null,
"_source": {
"name": "夏四二",
"class": "三班"
},
"sort": [
98,
"S040"
]
},
{
"_index": "student_scores",
"_id": "eiRap5oBBUWbZYrK_vaR",
"_score": null,
"_source": {
"name": "魏二三",
"class": "三班"
},
"sort": [
96,
"S021"
]
}
]
}
}
}
]
}
}
}
- 使用最后一页的
after_key作为after时,响应结果为空,代表后续无数据。
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 50,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"class_list": {
"buckets": []
}
}
}
统计物理成绩得分区间内学生数量{ [0,10),[10,20)…}
查询语句
GET student_scores/_search
{
"size": 0,
"aggs": {
"top_math": {
"histogram": {
"field": "physics",
"interval": 10,
"order": {
"_key": "DESC"
},
"extended_bounds": {
"min": 0,
"max": 100
}
}
}
}
}
extended_bounds: 可以设置区间边界,并设置各区间默认统计值为0,不设置的话,没值的区间不展示
执行结果
- 区间范围时前闭后开的
- 所以实际值应该在 {key,key+interval-1}之间
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 50,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"top_math": {
"buckets": [
{
"key": 100,
"doc_count": 0
},
{
"key": 90,
"doc_count": 18
},
{
"key": 80,
"doc_count": 20
},
{
"key": 70,
"doc_count": 11
},
{
"key": 60,
"doc_count": 1
},
{
"key": 50,
"doc_count": 0
},
{
"key": 40,
"doc_count": 0
},
{
"key": 30,
"doc_count": 0
},
{
"key": 20,
"doc_count": 0
},
{
"key": 10,
"doc_count": 0
},
{
"key": 0,
"doc_count": 0
}
]
}
}
}
统计数学成绩,使用以下得分区间算出得到各分数级别的学生人数
- [0,60) : D
- [60,75): C
- [75,90): B
- [90,101): A
查询语句
GET student_scores/_search
{
"size": 0,
"aggs": {
"top_math": {
"range": {
"field": "math",
"ranges": [
{"from": 0,"to":60,"key": "D"},
{"from": 60,"to":75,"key": "C"},
{"from": 75,"to":90,"key": "B"},
{"from": 90,"to":101,"key": "A"}
]
}
}
}
}
- 注意数据区间仍然是
前闭后开:[from,to),所以A区间的to应该大于100
执行结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 50,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"top_math": {
"buckets": [
{
"key": "D",
"from": 0,
"to": 60,
"doc_count": 0
},
{
"key": "C",
"from": 60,
"to": 75,
"doc_count": 7
},
{
"key": "B",
"from": 75,
"to": 90,
"doc_count": 24
},
{
"key": "A",
"from": 90,
"to": 101,
"doc_count": 19
}
]
}
}
}
统计数学成绩,使用以下得分区间计算,并得到各分数级别内各班级的学生人数
- [0,60) : D
- [60,75): C
- [75,90): B
- [90,101): A
查询语句
GET student_scores/_search
{
"size": 0,
"aggs": {
"数学成绩统计": {
"range": {
"field": "math",
"ranges": [
{"from": 0,"to":60,"key": "D"},
{"from": 60,"to":75,"key": "C"},
{"from": 75,"to":90,"key": "B"},
{"from": 90,"to":101,"key": "A"}
]
},
"aggs": {
"班级": {
"terms": {
"field": "class"
},
"aggs": {
"range_total":{
"value_count": {
"field": "math"
}
}
}
}
}
}
}
}
- 注意数据区间仍然是
前闭后开:[from,to),所以A区间的to应该大于100 - 聚合函数的外层名称可以写成中文,但不建议
- 无法获取父级bucket内的doc_count,所以如果需要计算百分比,需要在客户端做
执行结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 50,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"数学成绩统计": {
"buckets": [
{
"key": "D",
"from": 0,
"to": 60,
"doc_count": 0,
"班级": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": []
}
},
{
"key": "C",
"from": 60,
"to": 75,
"doc_count": 7,
"班级": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "二班",
"doc_count": 5,
"range_total": {
"value": 5
}
},
{
"key": "一班",
"doc_count": 1,
"range_total": {
"value": 1
}
},
{
"key": "三班",
"doc_count": 1,
"range_total": {
"value": 1
}
}
]
}
},
{
"key": "B",
"from": 75,
"to": 90,
"doc_count": 24,
"班级": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "二班",
"doc_count": 10,
"range_total": {
"value": 10
}
},
{
"key": "一班",
"doc_count": 8,
"range_total": {
"value": 8
}
},
{
"key": "三班",
"doc_count": 6,
"range_total": {
"value": 6
}
}
]
}
},
{
"key": "A",
"from": 90,
"to": 101,
"doc_count": 19,
"班级": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "三班",
"doc_count": 9,
"range_total": {
"value": 9
}
},
{
"key": "一班",
"doc_count": 8,
"range_total": {
"value": 8
}
},
{
"key": "二班",
"doc_count": 2,
"range_total": {
"value": 2
}
}
]
}
}
]
}
}
}
5271

被折叠的 条评论
为什么被折叠?



