ElasticSearch 复杂查询
*** url 中 shopping 为索引名 ***
- 条件查询
查询所有名字为张三的数据
GET /_search?q=name:张三 HTTP/1.1
Host: http://127.0.0.1:9200/shopping
上面在url中写查询参数的方式一般不常用(汉字可能出现乱码等情况)。可以改成用请求体的方式
GET /_search HTTP/1.1
Host: http://127.0.0.1:9200/shopping
Content-Type: application/json
Content-Length: 72
{
"query":{
"match":{
"name":"张三"
}
}
}
- 查询所有
GET /_search HTTP/1.1
Host: http://127.0.0.1:9200/shopping
Content-Type: application/json
Content-Length: 53
{
"query":{
"match_all":{
}
}
}
查询所有一般数据量过大,可以采用分页查询
- 分页查询
如第0页开始,每页3条数据。
GET /_search HTTP/1.1
Host: http://127.0.0.1:9200/shopping
Content-Type: application/json
Content-Length: 81
{
"query":{
"match_all":{
}
},
"from":0, //分页参数
"size":3
}
要是想更加精细的返回查询的字段,可以在请求体中加上"_source":[xxx,xxx],如只想要name
- 指定字段查询
GET /_search HTTP/1.1
Host: http://127.0.0.1:9200/shopping
Content-Type: application/json
Content-Length: 105
{
"query":{
"match_all":{
}
},
"from":0,
"size":3,
"_source":["name"] // 限制查询返回的数据字段
}
- 对结果进行排序
排序 只需要在请求体里面加上"sort"
GET /_search HTTP/1.1
Host: http://127.0.0.1:9200/shopping
Content-Type: application/json
Content-Length: 184
{
"query":{
"match_all":{
}
},
"from":0,
"size":10,
"_source":["name","age"],
"sort":{
"age":{
"order":"asc" //根据年龄正序排序
}
}
}
- 查询条件-must(类似sql的and)
查询名字叫做王五的且年龄为30的数据
GET /_search HTTP/1.1
Host: http://127.0.0.1:9200/shopping
Content-Type: application/json
Content-Length: 260
{
"query":{
"bool":{ //条件
"must":[{ //且
"match":{
"age":30
}
},{
"match":{
"name":"王五"
}
}
]
}
}
}
- 查询条件-should(类似sql的or)
查询名字叫做王五的或者年龄为30的数据
GET /_search HTTP/1.1
Host: http://127.0.0.1:9200/shopping
Content-Type: application/json
Content-Length: 260
{
"query":{
"bool":{ //条件
"should":[{ //或
"match":{
"age":30
}
},{
"match":{
"name":"王五"
}
}
]
}
}
}
- 范围查询
查询姓名为李四或王五、且年龄在20到35之间的数据。
GET /_search HTTP/1.1
Host: http://127.0.0.1:9200/shopping
Content-Type: application/json
Content-Length: 454
{
"query":{
"bool":{
"should":[{
"match":{
"name":"李四"
}
},{
"match":{
"name":"王五"
}
}
],
"filter":{
"range":{ //范围限制
"age":{
"gt":20,
"lt":35
}
}
}
}
}
}
match 会根据es分词进行查询,如"match":{"name":"王"}、"match":{"name":"王二"} 既可以查出姓名为王五的也可以查询出姓名为王晓明的数据;"match":{"phoneType":"苹华"}即可查询出使用苹果手机的数据也可以查询出使用华为手机的用户。
要是完全匹配可以用match_phrase :"match":{"name":"王五"},这样就只能查询出姓名为王五的数据了。
- 聚合查询
根据年龄分组统计。
GET /_search HTTP/1.1
Host: http://127.0.0.1:9200/shopping
Content-Type: application/json
Content-Length: 180
{
"aggs":{ //聚合查询
"age_group":{ //统计结果的名称、可以随意取名
"terms":{ //分组
"field":"age" //分组字段。
}
}
},
"size":0 //不用返回原始数据
}
返回结果
{
"took": 29,
"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": 30,
"doc_count": 3
},
{
"key": 20,
"doc_count": 2
}
]
}
}
}
- 聚合查询-平均值
求年龄平均值
GET /_search HTTP/1.1
Host: http://127.0.0.1:9200/shopping
Content-Type: application/json
Content-Length: 180
{
"aggs":{ //聚合查询
"age_avg":{ //统计结果的名称、可以随意取名
"avg":{ //平均值
"field":"age" //分组字段。
}
}
},
"size":0 //不用返回原始数据
}
本文来自博客园,作者:iyandongsheng,转载请注明原文链接:https://www.cnblogs.com/ieas/articles/17496795.html

浙公网安备 33010602011771号