目录
创建索引:
请求头
put 192.168.0.101:9200/log_index/
请求体:
{
"mappings":{
"log_type":{
"properties":{
"name":{
"type":"keyword"
},
"age":{
"type":"integer"
},
"birthday":{
"type": "date"
}
}
}
}
}
插入测试数据
请求头
PUT 192.168.0.101:9200/log_index/log_type/_bulk
请求体:
{"index":{}}
{"name":"小黑","age":23,"birthday":"2012-12-12"}
{"index":{}}
{"name":"王小黑","age":24,"birthday":"2012-12-13"}
{"index":{}}
{"name":"张小五","age":8,"birthday":"2012-12-14"}
{"index":{}}
{"name":"win7","age":9,"birthday":"2012-12-15"}
{"index":{}}
{"name":"梅超风","age":43,"birthday":"2012-12-16"}
{"index":{}}
{"name":"张无忌","age":59,"birthday":"2012-12-17"}
QueryDSL用法
查询所有
请求头:
GET 192.168.0.101:9200/log_index/log_type/_search
请求体:
{
"query":{
"match_all":{}
}
}
注:match_all 而不是match-all中间是下划线
排序
说明:多字段排序,先按照出生日期降序排列,如果出生日期相同则按照年龄排序
请求头:
GET 192.168.0.101:9200/log_index/log_type/_search
请求体:
{
"query":{
"match_all":{}
},
"sort":{
"birthday":"desc",
"age":"asc"
}
}
或者
请求头:
GET 192.168.0.101:9200/log_index/log_type/_search
请求体:
{
"query":{
"match_all":{ }
},
"sort": [
{
"birthday":{
"order":"desc" }
},
{
"age":{
"order":"asc"
}
}
]
}
两种写法结果一样
查询n条数据
请求头:
GET 192.168.0.101:9200/log_index/log_type/_search
请求体:
{
"query":{
"match_all":{ }
},
"size":2,
"sort": [
{
"birthday":{
"order":"desc" }
},
{
"age":{
"order":"asc"
}
}
]
}
注意:size的位置适合query关键字并列,而不是在query关键字内部
分页查询
请求头:
GET 192.168.0.101:9200/log_index/log_type/_search
请求体:
{
"query":{
"match_all":{ }
},
"size":2,
"from":1,
"sort": [
{
"birthday":{
"order":"de