ES之条件、分页及排序查询
一、条件查询
发送【GET】请求:http://127.0.0.1:9200/test-index-3/_doc/1001,参数如下
{
"query":{
"match":{
"num":5
}
}
}
查询num为5的数据,结果如下
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "test-index-1",
"_type": "_doc",
"_id": "svHyw30BJJ5e1YHwN2dz",
"_score": 1.0,
"_source": {
"title": "test5",
"num": 5,
"date": "20211213"
}
},
{
"_index": "test-index-1",
"_type": "_doc",
"_id": "s_Hyw30BJJ5e1YHwWWcU",
"_score": 1.0,
"_source": {
"title": "test6",
"num": 5,
"date": "20211213"
}
},
{
"_index": "test-index-1",
"_type": "_doc",
"_id": "tPHyw30BJJ5e1YHwYmet",
"_score": 1.0,
"_source": {
"title": "test7",
"num": 5,
"date": "20211213"
}
},
{
"_index": "test-index-1",
"_type": "_doc",
"_id": "tfHyw30BJJ5e1YHwbmfT",
"_score": 1.0,
"_source": {
"title": "test8",
"num": 5,
"date": "20211213"
}
}
]
}
}
二、全量查询
1.发送【GET】请求:http://127.0.0.1:9200/test-index-3/_doc/1001,参数如下
{
"query":{
"match_all":{
}
}
}
这里参数为 match_all 结果如下
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 8,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "test-index-1",
"_type": "_doc",
"_id": "rvHxw30BJJ5e1YHw6meH",
"_score": 1.0,
"_source": {
"title": "test1",
"num": 1,
"date": "20211213"
}
},
{
"_index": "test-index-1",
"_type": "_doc",
"_id": "r_Hxw30BJJ5e1YHw_2fX",
"_score": 1.0,
"_source": {
"title": "test2",
"num": 2,
"date": "20211213"
}
},
{
"_index": "test-index-1",
"_type": "_doc",
"_id": "sPHyw30BJJ5e1YHwEGfB",
"_score": 1.0,
"_source": {
"title": "test3",
"num": 3,
"date": "20211213"
}
},
{
"_index": "test-index-1",
"_type": "_doc",
"_id": "sfHyw30BJJ5e1YHwKWdp",
"_score": 1.0,
"_source": {
"title": "test4",
"num": 4,
"date": "20211213"
}
},
{
"_index": "test-index-1",
"_type": "_doc",
"_id": "svHyw30BJJ5e1YHwN2dz",
"_score": 1.0,
"_source": {
"title": "test5",
"num": 5,
"date": "20211213"
}
},
{
"_index": "test-index-1",
"_type": "_doc",
"_id": "s_Hyw30BJJ5e1YHwWWcU",
"_score": 1.0,
"_source": {
"title": "test6",
"num": 5,
"date": "20211213"
}
},
{
"_index": "test-index-1",
"_type": "_doc",
"_id": "tPHyw30BJJ5e1YHwYmet",
"_score": 1.0,
"_source": {
"title": "test7",
"num": 5,
"date": "20211213"
}
},
{
"_index": "test-index-1",
"_type": "_doc",
"_id": "tfHyw30BJJ5e1YHwbmfT",
"_score": 1.0,
"_source": {
"title": "test8",
"num": 5,
"date": "20211213"
}
}
]
}
}
2.增加查询条件,发送【GET】请求:http://127.0.0.1:9200/test-index-3/_doc/1001,参数如下
{
"query" : {
"match_all" : {
}
},
"from" : 4,
"size" : 4,
"_source" : [
"title", "num"
],
"sort" : {
"num" : {
"order" : "desc"
}
}
}
这里参数含义为
from :起始位置,初始值为0【实际使用时,(页码-1)*size】,例子中为第二页数据
size :每页数据条数
_source :过滤显示的数据
sort :排序,例子中根据num倒序
结果如下
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 8,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "test-index-1",
"_type": "_doc",
"_id": "sfHyw30BJJ5e1YHwKWdp",
"_score": null,
"_source": {
"num": 4,
"title": "test4"
},
"sort": [
4
]
},
{
"_index": "test-index-1",
"_type": "_doc",
"_id": "sPHyw30BJJ5e1YHwEGfB",
"_score": null,
"_source": {
"num": 3,
"title": "test3"
},
"sort": [
3
]
},
{
"_index": "test-index-1",
"_type": "_doc",
"_id": "r_Hxw30BJJ5e1YHw_2fX",
"_score": null,
"_source": {
"num": 2,
"title": "test2"
},
"sort": [
2
]
},
{
"_index": "test-index-1",
"_type": "_doc",
"_id": "rvHxw30BJJ5e1YHw6meH",
"_score": null,
"_source": {
"num": 1,
"title": "test1"
},
"sort": [
1
]
}
]
}
}