1、普通查询
es的文档查询可以在请求时候拼接参数查询,如下:
GET http://127.0.0.1:9200/students/_search?q=age:24
返回结果如下:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "students",
"_type": "_doc",
"_id": "1002",
"_score": 1.0,
"_source": {
"userId": 1002,
"userName": "Tom",
"age": 24,
"hobby": "游泳,足球"
}
}
]
}
}
这种查询的方式,没有考虑查询参数是中文的时候中文的编码问题,所以推荐使用请求体带参数的方式:
如下:
POST http://127.0.0.1:9200/students/_search
请求Body
:
{
"query": {
"match": {
"age": 24
}
}
}
返回结果为:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "students",
"_type": "_doc",
"_id": "1002",
"_score": 1.0,
"_source": {
"userId": 1002,
"userName": "Tom",
"age": 24,
"hobby": "游泳,足球"
}
}
]
}
}
2、分页查询
POST http://127.0.0.1:9200/students/_search
请求Body
:
{
"query": {
"match_all": {}
},
"from": 0,
"size": 2,
"_source": [
"userName",
"hobby"
],
"sort":{
"userId":{
"order":"desc"
}
}
}
match_all
表示全量查询,from
表示开始的条数而非页数,size
表示每页的条数,_source
字段用来指定查询的字段,不需要将所有的字段全部返回,sort
字段表示排序,order
用来指定升序还是降序
返回结果为:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "students",
"_type": "_doc",
"_id": "1002",
"_score": null,
"_source": {
"userName": "Nancy",
"hobby": "游泳,学习"
},
"sort": [
1002
]
},
{
"_index": "students",
"_type": "_doc",
"_id": "1001",
"_score": null,
"_source": {
"userName": "Tom",
"hobby": "游泳,足球"
},
"sort": [
1001
]
}
]
}
}
3、复杂组合查询
POST http://127.0.0.1:9200/students/_search
请求Body
:
{
"query": {
"bool": {
"must": [
{
"match": {
"age": 24
}
},
{
"match": {
"hobby": "游泳"
}
}
]
}
},
"from": 0,
"size": 2,
"_source": [
"userName",
"hobby"
],
"sort": {
"userId": {
"order": "desc"
}
}
}
bool
表示查询条件的意思,must
表示必须满足的条件,相当于mysql
中查询使用的and
,shoud
相当于mysql
中查询使用的or
,
返回结果为:
{
"took": 10,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "students",
"_type": "_doc",
"_id": "1002",
"_score": null,
"_source": {
"userName": "Nancy",
"hobby": "游泳,学习"
},
"sort": [
1002
]
},
{
"_index": "students",
"_type": "_doc",
"_id": "1001",
"_score": null,
"_source": {
"userName": "Tom",
"hobby": "游泳,足球"
},
"sort": [
1001
]
}
]
}
}
4、范围查询
POST http://127.0.0.1:9200/students/_search
请求Body
:
{
"query": {
"bool": {
"filter": {
"range": {
"age": {
"gt": 20
}
}
}
}
},
"from": 0,
"size": 10,
"_source": [
"userName",
"hobby"
],
"sort": {
"age": {
"order": "desc"
}
}
}
使用range
进行范围查询,gt
表示大于
返回结果:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "students",
"_type": "_doc",
"_id": "1004",
"_score": null,
"_source": {
"userName": "Bom",
"hobby": "学习,羽毛球"
},
"sort": [
26
]
},
{
"_index": "students",
"_type": "_doc",
"_id": "1002",
"_score": null,
"_source": {
"userName": "Nancy",
"hobby": "游泳,学习"
},
"sort": [
24
]
}
]
}
}
5、完全匹配高亮显示
POST http://127.0.0.1:9200/students/_search
请求Body
:
{
"query": {
"match_phrase": {
"hobby": "游泳,学习"
}
},
"highlight": {
"fields": {
"hobby": {}
}
}
}
match_phrase
表示整个短语完全匹配,而不使用类似match
那样的全文索引,highlight
表示对查询结果fields
指定的字段进行高亮显示。
查询结果如下:
{
"took": 182,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 2.2392645,
"hits": [
{
"_index": "students",
"_type": "_doc",
"_id": "1002",
"_score": 2.2392645,
"_source": {
"userId": 1002,
"userName": "Nancy",
"age": 24,
"hobby": "游泳,学习"
},
"highlight": {
"hobby": [
"<em>游</em><em>泳</em>,<em>学</em><em>习</em>"
]
}
}
]
}
}
6、聚合操作
POST http://127.0.0.1:9200/students/_search
请求Body
:
{
"aggs": {
"age_group": {
"terms": {
"field": "age"
}
}
}
}
aggs
表示的是聚合操作,age_group
无特殊含义,可以随意定义,terms
分组操作会对field
指定的分组字段进行分组操作。
返回结果:
{
"took": 123,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "students",
"_type": "_doc",
"_id": "1002",
"_score": 1.0,
"_source": {
"userId": 1002,
"userName": "Nancy",
"age": 24,
"hobby": "游泳,学习"
}
},
{
"_index": "students",
"_type": "_doc",
"_id": "1001",
"_score": 1.0,
"_source": {
"userId": 1001,
"userName": "Tom",
"age": 24,
"hobby": "游泳,足球"
}
},
{
"_index": "students",
"_type": "_doc",
"_id": "1003",
"_score": 1.0,
"_source": {
"userId": 1003,
"userName": "Jack",
"age": 22,
"hobby": "学习,足球"
}
},
{
"_index": "students",
"_type": "_doc",
"_id": "1004",
"_score": 1.0,
"_source": {
"userId": 1004,
"userName": "Bom",
"age": 26,
"hobby": "学习,羽毛球"
}
},
{
"_index": "students",
"_type": "_doc",
"_id": "1005",
"_score": 1.0,
"_source": {
"userId": 1005,
"userName": "Jop",
"age": 23,
"hobby": "游泳,羽毛球"
}
}
]
},
"aggregations": {
"age_group": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 24,
"doc_count": 2
},
{
"key": 22,
"doc_count": 1
},
{
"key": 23,
"doc_count": 1
},
{
"key": 26,
"doc_count": 1
}
]
}
}
}
如果不想查看到聚合查询的原始数据,可以加个size
的参数,如下:
{
"aggs": {
"age_group": {
"terms": {
"field": "age"
}
}
},
"size": 0
}
返回结果如下:
{
"took": 53,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"age_group": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 24,
"doc_count": 2
},
{
"key": 22,
"doc_count": 1
},
{
"key": 23,
"doc_count": 1
},
{
"key": 26,
"doc_count": 1
}
]
}
}
}
聚合比如还可以求平均值,例如:
{
"aggs": {
"age_avg": {
"avg": {
"field": "age"
}
}
},
"size": 0
}
返回结果如下:
{
"took": 17,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"age_avg": {
"value": 23.8
}
}
}