ElasticSearch可以執行複雜的條件查詢,下面直接舉例子:
首先先添加文檔:
PUT /user_dao/user_table/1
{
"name":"baby",
"sex":0,
"age":1
}
PUT /user_dao/user_table/2
{
"name":"father",
"sex":0,
"age":26
}
PUT /user_dao/user_table/3
{
"name":"mother",
"sex":1,
"age":24
}
PUT /user_dao/user_table/4
{
"name":"grandfather",
"sex":1,
"age":60
}
PUT /user_dao/user_table/5
{
"name":"grandmother",
"sex":1,
"age":58
}
1. 根據id進行查詢
GET /user_dao/user_table/1
2.查詢目前所有類型的文檔
GET /user_dao/user_table/_search
3.根據多個ID批量查詢 ,查詢多個id分别為1、2
GET /user_dao/user_table/_mget
{
"ids":["1","2"]
}
4.查詢年齡為年齡24歲
GET /user_dao/user_table/_search?q=age:24
5. 查詢年齡20歲-60歲之間(注意:TO 一定要大寫)
GET /user_dao/user_table/_search?q=age[20 TO 60]
傳回結果:
{
"took": 77,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1,
"hits": [
{
"_index": "user_dao",
"_type": "user_table",
"_id": "5",
"_score": 1,
"_source": {
"name": "grandmother",
"sex": 1,
"age": 58
}
},
{
"_index": "user_dao",
"_type": "user_table",
"_id": "2",
"_score": 1,
"_source": {
"name": "father",
"sex": 0,
"age": 26
}
},
{
"_index": "user_dao",
"_type": "user_table",
"_id": "4",
"_score": 1,
"_source": {
"name": "grandfather",
"sex": 1,
"age": 60
}
},
{
"_index": "user_dao",
"_type": "user_table",
"_id": "3",
"_score": 1,
"_source": {
"name": "mother",
"sex": 1,
"age": 24
}
}
]
}
}
6.查詢年齡20歲-60歲之間并且年齡降序、從0條資料到第1條資料
GET /user_dao/user_table/_search?q=age[30 TO 60]&sort=age:desc&from=0&size=1
傳回内容:
{
"took": 57,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": null,
"hits": [
{
"_index": "user_dao",
"_type": "user_table",
"_id": "4",
"_score": null,
"_source": {
"name": "grandfather",
"sex": 1,
"age": 60
},
"sort": [
60
]
}
]
}
}
GET /user_dao/user_table/_search?q=age[30 TO 60]&sort=age:desc&from=0&size=1&_source=name,age
{
"took": 48,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": null,
"hits": [
{
"_index": "user_dao",
"_type": "user_table",
"_id": "4",
"_score": null,
"_source": {
"name": "grandfather",
"age": 60
},
"sort": [
60
]
}
]
}
}