9) 范围查询
range 查询找出那些落在指定区间内的数字或者时间。range 查询允许以下字符
在Postman中,向ES服务器发GET请求:http://127.0.0.1:9200/student/_search
用student索引报错,还是改成自己新建的zhou索引
{
"took": 17,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "zhou",
"_type": "_doc",
"_id": "1001",
"_score": 1.0,
"_source": {
"name": "zhangsan",
"nickname": "zhangsan",
"sex": "男",
"age": 30
}
},
{
"_index": "zhou",
"_type": "_doc",
"_id": "1005",
"_score": 1.0,
"_source": {
"name": "zhangsan2",
"nickname": "zhangsan2",
"sex": "女",
"age": 30
}
}
]
}
}
10) 模糊查询
返回包含与搜索字词相似的字词的文档。
编辑距离是将一个术语转换为另一个术语所需的一个字符更改的次数。
这些更改可以包括:
更改字符(box → fox)
删除字符(black → lack)
插入字符(sic → sick)
转置两个相邻字符(act → cat)
为了找到相似的术语,fuzzy 查询会在指定的编辑距离内创建一组搜索词的所有可能的变体
或扩展。然后查询返回每个扩展的完全匹配。
通过fuzziness修改编辑距离,可以被设置为“0”, “1”, “2”或“auto”,一般使用默认值AUTO,它会
根据查询词的长度定义距离。
{
"query": {
"fuzzy": {
"name": {
"value": "zhangsan"
}
}
}
}
{
"took": 138,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.3862942,
"hits": [
{
"_index": "zhou",
"_type": "_doc",
"_id": "1001",
"_score": 1.3862942,
"_source": {
"name": "zhangsan",
"nickname": "zhangsan",
"sex": "男",
"age": 30
}
},
{
"_index": "zhou",
"_type": "_doc",
"_id": "1004",
"_score": 1.2130076,
"_source": {
"name": "zhangsan1",
"nickname": "zhangsan1",
"sex": "女",
"age": 50
}
},
{
"_index": "zhou",
"_type": "_doc",
"_id": "1005",
"_score": 1.2130076,
"_source": {
"name": "zhangsan2",
"nickname": "zhangsan2",
"sex": "女",
"age": 30
}
}
]
}
}
11) 单字段排序
sort可以让我们按照不同的字段进行排序,并且通过order指定排序的方式。desc 降序,asc
升序。
在Postman中,向ES服务器发GET请求 :http://127.0.0.1:9200/zhou/_search
{
"query": {
"fuzzy": {
"name": {
"value": "zhangsan"
}
}
},
"sort": [{
"age": {
"order": "desc"
}
}]
}
{
"took": 22,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "zhou",
"_type": "_doc",
"_id": "1004",
"_score": null,
"_source": {
"name": "zhangsan1",
"nickname": "zhangsan1",
"sex": "女",
"age": 50
},
"sort": [
50
]
},
{
"_index": "zhou",
"_type": "_doc",
"_id": "1001",
"_score": null,
"_source": {
"name": "zhangsan",
"nickname": "zhangsan",
"sex": "男",
"age": 30
},
"sort": [
30
]
},
{
"_index": "zhou",
"_type": "_doc",
"_id": "1005",
"_score": null,
"_source": {
"name": "zhangsan2",
"nickname": "zhangsan2",
"sex": "女",
"age": 30
},
"sort": [
30
]
}
]
}
}
12) 多字段排序
假定我们想要结合使用age和_score进行查询,并且匹配的结果首先按照年龄排序,然后
按照相关性得分排序
在Postman中,向ES服务器发GET请求 :http://127.0.0.1:9200/zhou/_search
{
"query": {
"match_all": {
}
},
"sort": [{
"age": {
"order": "desc"
}
},
{
"_score": {
"order": "desc"
}
}]
}
{
"took": 10,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "zhou",
"_type": "_doc",
"_id": "1004",
"_score": 1.0,
"_source": {
"name": "zhangsan1",
"nickname": "zhangsan1",
"sex": "女",
"age": 50
},
"sort": [
50,
1.0
]
},
{
"_index": "zhou",
"_type": "_doc",
"_id": "1003",
"_score": 1.0,
"_source": {
"name": "wangwu",
"nickname": "wangwu",
"sex": "女",
"age": 40
},
"sort": [
40,
1.0
]
},
{
"_index": "zhou",
"_type": "_doc",
"_id": "1001",
"_score": 1.0,
"_source": {
"name": "zhangsan",
"nickname": "zhangsan",
"sex": "男",
"age": 30
},
"sort": [
30,
1.0
]
},
{
"_index": "zhou",
"_type": "_doc",
"_id": "1005",
"_score": 1.0,
"_source": {
"name": "zhangsan2",
"nickname": "zhangsan2",
"sex": "女",
"age": 30
},
"sort": [
30,
1.0
]
},
{
"_index": "zhou",
"_type": "_doc",
"_id": "1002",
"_score": 1.0,
"_source": {
"name": "lisi",
"nickname": "lisi",
"sex": "男",
"age": 20
},
"sort": [
20,
1.0
]
}
]
}
}
13) 高亮查询
在进行关键字搜索时,搜索出的内容中的关键字会显示不同的颜色,称之为高亮。
Elasticsearch 可以对查询内容中的关键字部分,进行标签和样式(高亮)的设置。
在使用match查询的同时,加上一个 highlight 属性:
pre_tags:前置标签
post_tags:后置标签
fields:需要高亮的字段
title:这里声明title字段需要高亮,后面可以为这个字段设置特有配置,也可以空
在Postman中,向ES服务器发GET请求:http://127.0.0.1:9200/zhou/_search
{
"query": {
"fuzzy": {
"name": {
"value": "zhangsan"
}
}
},
"highlight": {
"pre_tags": "",
"post_tags": "",
"fields": {
"name": {
}
}
}
}
{
"took": 19,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.3862942,
"hits": [
{
"_index": "zhou",
"_type": "_doc",
"_id": "1001",
"_score": 1.3862942,
"_source": {
"name": "zhangsan",
"nickname": "zhangsan",
"sex": "男",
"age": 30
},
"highlight": {
"name": [
"zhangsan"
]
}
},
{
"_index": "zhou",
"_type": "_doc",
"_id": "1004",
"_score": 1.2130076,
"_source": {
"name": "zhangsan1",
"nickname": "zhangsan1",
"sex": "女",
"age": 50
},
"highlight": {
"name": [
"zhangsan1"
]
}
},
{
"_index": "zhou",
"_type": "_doc",
"_id": "1005",
"_score": 1.2130076,
"_source": {
"name": "zhangsan2",
"nickname": "zhangsan2",
"sex": "女",
"age": 30
},
"highlight": {
"name": [
"zhangsan2"
]
}
}
]
}
}
改成不模糊搜索:
{
"query": {
"match": {
"name": "zhangsan"
}
},
"highlight": {
"pre_tags": "",
"post_tags": "",
"fields": {
"name": {}
}
}
}
14) 分页查询
from:当前页的起始索引,默认从0开始。 from = (pageNum - 1) * size
size:每页显示多少条
在Postman中,向ES服务器发GET请求:http://127.0.0.1:9200/zhou/_search
先不加分页,查询有5条数据
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "zhou",
"_type": "_doc",
"_id": "1004",
"_score": null,
"_source": {
"name": "zhangsan1",
"nickname": "zhangsan1",
"sex": "女",
"age": 50
},
"sort": [
50
]
},
{
"_index": "zhou",
"_type": "_doc",
"_id": "1003",
"_score": null,
"_source": {
"name": "wangwu",
"nickname": "wangwu",
"sex": "女",
"age": 40
},
"sort": [
40
]
},
{
"_index": "zhou",
"_type": "_doc",
"_id": "1001",
"_score": null,
"_source": {
"name": "zhangsan",
"nickname": "zhangsan",
"sex": "男",
"age": 30
},
"sort": [
30
]
},
{
"_index": "zhou",
"_type": "_doc",
"_id": "1005",
"_score": null,
"_source": {
"name": "zhangsan2",
"nickname": "zhangsan2",
"sex": "女",
"age": 30
},
"sort": [
30
]
},
{
"_index": "zhou",
"_type": "_doc",
"_id": "1002",
"_score": null,
"_source": {
"name": "lisi",
"nickname": "lisi",
"sex": "男",
"age": 20
},
"sort": [
20
]
}
]
}
}
加分页查询:
{
"query": {
"match_all": {
}
},
"sort": [{
"age": {
"order": "desc"
}
}],
"from": 0,
"size": 2
}
{
"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": "zhou",
"_type": "_doc",
"_id": "1004",
"_score": null,
"_source": {
"name": "zhangsan1",
"nickname": "zhangsan1",
"sex": "女",
"age": 50
},
"sort": [
50
]
},
{
"_index": "zhou",
"_type": "_doc",
"_id": "1003",
"_score": null,
"_source": {
"name": "wangwu",
"nickname": "wangwu",
"sex": "女",
"age": 40
},
"sort": [
40
]
}
]
}
}
15) 聚合查询
聚合允许使用者对es文档进行统计分析,类似与关系型数据库中的group by,当然还有很
多其他的聚合,例如取最大值、平均值等等。
先查询下所有数据:
{
"took": 16,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "zhou",
"_type": "_doc",
"_id": "1001",
"_score": 1.0,
"_source": {
"name": "zhangsan",
"nickname": "zhangsan",
"sex": "男",
"age": 30
}
},
{
"_index": "zhou",
"_type": "_doc",
"_id": "1002",
"_score": 1.0,
"_source": {
"name": "lisi",
"nickname": "lisi",
"sex": "男",
"age": 20
}
},
{
"_index": "zhou",
"_type": "_doc",
"_id": "1003",
"_score": 1.0,
"_source": {
"name": "wangwu",
"nickname": "wangwu",
"sex": "女",
"age": 40
}
},
{
"_index": "zhou",
"_type": "_doc",
"_id": "1004",
"_score": 1.0,
"_source": {
"name": "zhangsan1",
"nickname": "zhangsan1",
"sex": "女",
"age": 50
}
},
{
"_index": "zhou",
"_type": "_doc",
"_id": "1005",
"_score": 1.0,
"_source": {
"name": "zhangsan2",
"nickname": "zhangsan2",
"sex": "女",
"age": 30
}
}
]
}
}
对某个字段取最大值 max
在Postman中,向ES服务器发GET请求 :http://127.0.0.1:9200/zhou/_search
{
"aggs": {
"max_age": {
"max": {
"field": "age"
}
}
},
"size": 0
}
查询出最大年龄50岁
{
"took": 182,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"max_age": {
"value": 50.0
}
}
}
对某个字段取最小值min
在Postman中,向ES服务器发GET请求 :http://127.0.0.1:9200/zhou/_search
对某个字段求和sum
在Postman中,向ES服务器发GET请求 :http://127.0.0.1:9200/zhou/_search
对某个字段取平均值avg
在Postman中,向ES服务器发GET请求 :http://127.0.0.1:9200/zhou/_search
对某个字段的值进行去重之后再取总个数
在Postman中,向ES服务器发GET请求 :http://127.0.0.1:9200/zhou/_search
{
"aggs": {
"distinct_age": {
"cardinality": {
"field": "age"
}
}
},
"size": 0
}
总共5条数据,去重了age相同的还有4条数据
State聚合
stats聚合,对某个字段一次性返回count,max,min,avg和sum五个指标
在Postman中,向ES服务器发GET请求 :http://127.0.0.1:9200/zhou/_search
{
"aggs": {
"stats_age": {
"stats": {
"field": "age"
}
}
},
"size": 0
}
16) 桶聚合查询
桶聚和相当于sql中的group by语句
terms聚合,分组统计
在Postman中,向ES服务器发GET请求 :http://127.0.0.1:9200/zhou/_search
年龄30的数据有两条
{
"took": 40,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"age_groupby": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 30,
"doc_count": 2
},
{
"key": 20,
"doc_count": 1
},
{
"key": 40,
"doc_count": 1
},
{
"key": 50,
"doc_count": 1
}
]
}
}
}
在terms分组下再进行聚合
在Postman中,向ES服务器发GET请求 :http://127.0.0.1:9200/zhou/_search
{
"aggs": {
"age_groupby": {
"terms": {
"field": "age"
},
"aggs": {
"sum_age": {
"sum": {
"field": "age"
}
}
}
}
},
"size": 0
}
年龄为30的数据有两个,累加等于60
{
"took": 19,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"age_groupby": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 30,
"doc_count": 2,
"sum_age": {
"value": 60.0
}
},
{
"key": 20,
"doc_count": 1,
"sum_age": {
"value": 20.0
}
},
{
"key": 40,
"doc_count": 1,
"sum_age": {
"value": 40.0
}
},
{
"key": 50,
"doc_count": 1,
"sum_age": {
"value": 50.0
}
}
]
}
}
}