# mapping
PUT /test_001
{
"mappings":{
"properties":{
"name":{
"type":"keyword"
},
"age":{
"type":"integer"
},
"sex":{
"type": "integer"
},
"score":{
"type": "keyword"
}
}
}
}
# 数据
POST /test_001/_doc
{
"name":"a",
"age":100,
"sex":1,
"score":"120"
}
POST /test_001/_doc
{
"name":"a",
"age":18,
"sex":0,
"score":"20"
}
POST /test_001/_doc
{
"name":"a",
"age":19,
"sex":0,
"score":"21"
}
POST /test_001/_doc
{
"name":"b",
"age":1,
"sex":1,
"score":"200"
}
POST /test_001/_doc
{
"name":"b",
"age":2,
"sex":1,
"score":"211"
}
POST /test_001/_doc
{
"name":"b",
"age":3,
"sex":1,
"score":"2100"
}
查询
GET /test_001/_search
{
"size": 0,
"query": {
"match_all": {}
},
"aggs": {
"d1": {
"terms": {
"field": "name",
"order": [
{
"top_age": "desc"
},
{
"top_score": "desc"
}
]
},
"aggs": {
"d2": {
"terms": {
"field": "sex"
},
"aggs": {
"top1": {
"top_hits": {
"size": 1,
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
}
}
},
"top_age": {
"max": {
"script": "doc['age'].value"
}
},
"top_score": {
"max": {
"script": "java.lang.Long.parseLong(doc['score'].value)"
}
},
"pipele-bucket-sort": {
"bucket_sort": {
"sort": {},
"from": 0,
"size": 10
}
}
}
}
}
}