es索引中的数据如下:
1.简单查询
先新建book索引,类型为小说,字段有world_count(字数),author(作者),title(标题),publish_date(发布日期)
查询id为1的文档
2.条件查询
查询所有:默认指查询10条
分页:
关键词查询:查询title含有ElasticSearch的文档
结果:
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1.3469357,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "AKl9yGwBp8L3WHXTw3AR",
"_score": 1.3469357,
"_source": {
"author": "瓦力",
"title": "ElasticSearch入门",
"world_count": "3000",
"publish_date": "2017-08-20"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "2",
"_score": 0.5531392,
"_source": {
"author": "很胖的瓦力",
"title": "ElasticSearch精通",
"world_count": "3000",
"publish_date": "2017-10-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "11",
"_score": 0.5531392,
"_source": {
"author": "林卫平",
"title": "elasticsearch入门",
"world_count": "2000",
"publish_date": "2017-05-08"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "5",
"_score": 0.4889865,
"_source": {
"author": "李四",
"title": "ElasticSearch大法好",
"world_count": "2000",
"publish_date": "2018-10-01"
}
}
]
}
}
排序:根据发布日期排序
{
"query": {
"match": {
"title": "ElasticSearch"
}
},
"sort": [{
"publish_date": {
"order": "desc"
}
}]
}
3.聚合查询
根据world_count聚合
代码:
localhost:9200/book/novel/_search
{
"aggs":{
"group_by_word_count":{
"terms":{
"field":"world_count"
}
}
}
}
多个聚合
{
"aggs":{
"group_by_word_count":{
"terms":{
"field":"world_count"
}
},
"group_by_publish_date":{
"terms":{
"field":"publish_date"
}
}
}
}
求平均值,总和,最大,最小值
代码
localhost:9200/book/novel/_search
{
"aggs":{
"group_by_word_count":{
"stats":{
"field":"world_count"
}
}
}
}