ES6 - 3简单数据查询
2018-11-28 本文已影响0人
阿尔卡雷特
- 查询单个文档
格式:http://127.0.0.1:9200/people/man/1 [get]
查到数据返回json
未查到数据返回:
{
"_index": "people",
"_type": "man",
"_id": "1",
"found": false
}
- 查询全部数据
格式:http://127.0.0.1:9200/people/_search [post]
请求内容:
2.1 查全部(默认只返回10条)
{
"query": {
"match_all": {}
}
}
2.2 查分页
{
"query": {
"match_all": {}
},
"from": 1, // 从第几条查,第一条索引为0
"size": 2, // 查几条
"sort": [ // 排序规则
{
"date": {
"order":"desc"
}
}
]
}
查询返回json格式:
{
"took": 47, // 查询消耗时间毫秒
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": { // 响应结果
"total": 2, // 总数
"max_score": 1,
"hits": [ // 结果集
{
"_index": "people",
"_type": "man",
"_id": "36k2WWcBRM5zB9jDvej9",
"_score": 1,
"_source": {
"name": "张无忌",
"country": "China",
"age": 20,
"date": "1988-9-1"
}
},
{
"_index": "people",
"_type": "man",
"_id": "1",
"_score": 1,
"_source": {
"name": "张三丰",
"country": "China",
"age": 30,
"date": "1987-9-1"
}
}
]
}
}
- 按关键词查询
格式:http://127.0.0.1:9200/people/_search [post]
请求内容:
{
"query": {
"match": {
"name": "张"
}
},
"sort": [
{
"date": {
"order":"desc"
}
}
]
}
- 聚合查询
请求内容:
{
"aggs": {
"group_by_xx": { // 结果节点,任意名称
"terms": { // 分组
"field": "date"
}
},
"group_by_yy": {
"terms": {
"field": "country"
}
},
"stats_1": {
"stats": { // 统计数
"field": "age"
}
},
"stats_2": {
"min": { // 指定某个具体统计类型(min、max、count、avg、sum)
"field": "age"
}
}
}
}
返回结果:
{
"took": 79,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "people",
"_type": "man",
"_id": "36k2WWcBRM5zB9jDvej9",
"_score": 1,
"_source": {
"name": "张无忌",
"country": "China",
"age": 20,
"date": "1988-9-1"
}
},
{
"_index": "people",
"_type": "man",
"_id": "1",
"_score": 1,
"_source": {
"name": "张三丰",
"country": "China",
"age": 30,
"date": "1987-9-1"
}
}
]
},
"aggregations": {
"group_by_yy": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "China",
"doc_count": 2
}
]
},
"group_by_xx": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 557452800000,
"key_as_string": "1987-09-01 00:00:00",
"doc_count": 1
},
{
"key": 589075200000,
"key_as_string": "1988-09-01 00:00:00",
"doc_count": 1
}
]
},
"stats_1": {
"count": 2,
"min": 20,
"max": 30,
"avg": 25,
"sum": 50
},
"stats_2": {
"value": 20
}
}
}