文章目录
-
-
- 1. 查看节点状态
- 2. 查看索引
- 3. 查看节点列表
- 4. 增加索引
- 5. 删除索引
- 6. 新增document
- 7. 搜索 type 全部数据
- 8. 查找指定 id 的 document 数据
- 9. 修改 document
-
-
- A. 整体替换
- B. 只修改某个字段
-
- 10. 删除一个 document
- 11. 按条件查询(全部)
- 12. 按照字段的分词查询
- 13. 按照分词子属性查询
- 14. 按照短语查询
- 15. 模糊查询
- 16. 查询后过滤
- 17. 查询前过滤(推荐使用)
- 18. 按范围过滤
- 19. 排序
- 20. 分页查询
- 21. 指定查询的字段
- 22. 聚合
- 23. 分词器
-
-
- A.Elasticsearch自带的分词器
- B. 中文分词器
- C. 安装 IK 分词器
- D.使用 IK 分词器
-
- 24. 关于Mapping
-
-
- A. 查看mapping
- B. 手动指定Mapping
-
- 25. 根据时间范围查询
-
-
- A. 先查询时间字段的mapping,确定时间字段的格式
- B.根据时间字段进行查询
-
-
1. 查看节点状态
GET /_cat/health?v
前两个是时间戳,其余含义如下:
1. cluster , 集群名称
2. status, 集群状态 green代表健康;yellow代表分配了所有主分片,但至少缺少一个副本,此时集群数据仍旧完整;red代表部分主分片不可用,可能已经丢失数据。
3. node.total, 代表在线的节点总数量
4. node.data, 代表在线的数据节点的数量
5. shards, active_shards 存活的分片数量
6. pri,active_primary_shards 存活的主分片数量 正常情况下 shards的数量是pri的两倍。
7. relo, relocating_shards 迁移中的分片数量,正常情况为 0
8. init, initializing_shards 初始化中的分片数量 正常情况为 0
9. unassign, unassigned_shards 未分配的分片 正常情况为 0
10. pending_tasks, 准备中的任务,任务指迁移分片等 正常情况为 0
11. max_task_wait_time, 任务最长等待时间
12. active_shards_percent, 正常分片百分比 正常情况为 100%
参考文章: https://blog.csdn.net/weixin_44723434/article/details/90452083
2. 查看索引
GET /_cat/indices?v
含义:
1. health 索引健康状态
2. status 索引的开启状态
3. index 索引名称
4. uuid 索引uuid
5. pri 索引主分片数
6. rep 索引副本分片数量
7. docs.count 索引中文档总数
8. docs.deleted 索引中删除状态的文档
9. store.size 主分片+副本分分片的大小
10. pri.store.size 主分片的大小
参考文章: https://blog.csdn.net/qq_28988969/article/details/105079476
3. 查看节点列表
GET /_cat/nodes?v
返回字段 | 含义 |
---|---|
ip | ip |
heap.percent | 堆内存占用百分比 |
ram.percent | 内存占用百分比 |
cpu | CPU占用百分比 |
load_1m | 1分钟的系统负载 |
load_5m | 5分钟的系统负载 |
load_15m | 15分钟的系统负载 |
node.role | node节点的角色 |
master | 是否是master节点 |
name | 节点名称 |
参考文章: https://blog.csdn.net/qq_28988969/article/details/105079476
4. 增加索引
PUT /test01
5. 删除索引
DELETE /test01
6. 新增document
说明: 如果之前没建过 index 或者 type,es 会自动创建。
PUT /movie_index/movie/1
{ "id":1,
"name":"operation red sea",
"doubanScore":8.5,
"actorList":[
{"id":1,"name":"zhang yi"},
{"id":2,"name":"hai qing"},
{"id":3,"name":"zhang han yu"}
]
}
PUT /movie_index/movie/2
{
"id":2,
"name":"operation meigong river",
"doubanScore":8.0,
"actorList":[
{"id":3,"name":"zhang han yu"}
]
}
PUT /movie_index/movie/3
{
"id":3,
"name":"incident red sea",
"doubanScore":5.0,
"actorList":[
{"id":4,"name":"zhang chen"}
]
}
7. 搜索 type 全部数据
GET /movie_index/movie/_search
8. 查找指定 id 的 document 数据
9. 修改 document
A. 整体替换
说明: 修改和新增没有区别,是做一个覆盖操作
PUT /movie_index/movie/3
{
"id":"3",
"name":"incident red sea",
"doubanScore":"8.0",
"actorList":[
{"id":"1","name":"zhang chen"}
]
}
B. 只修改某个字段
POST /movie_index/movie/3/_update
{
"doc": {
"doubanScore":"8.1"
}
}
10. 删除一个 document
11. 按条件查询(全部)
GET /movie_index/movie/_search
{
"query": {
"match_all": {}
}
}
12. 按照字段的分词查询
GET /movie_index/movie/_search
{
"query": {
"match": {
"name": "sea"
}
}
}
13. 按照分词子属性查询
GET /movie_index/movie/_search
{
"query": {
"match": {
"actorList.name": "zhang"
}
}
}
14. 按照短语查询
说明: 按照短语查询是指
匹配某个 field 的整个内容, 不再利用分词技术
GET /movie_index/movie/_search
{
"query": {
"match_phrase": {
"name": "operation red"
}
}
}
对比:
### 使用此条查询会把包含 operation 或者 red 的全部查找出来
GET /movie_index/movie/_search
{
"query": {
"match": {
"name": "operation red"
}
}
}
15. 模糊查询
说明: 校正匹配分词,当一个单词都无法准确匹配,es 通过一种算法对非常接近的单词也给与一定的评分,能够查询出来,但是消耗更多的性能。
GET /movie_index/movie/_search
{
"query": {
"fuzzy": {
"name": "red"
}
}
}
16. 查询后过滤
GET /movie_index/movie/_search
{
"query": {
"match": {
"name": "red"
}
},
"post_filter": {
"term": {
"actorList.id": "3"
}
}
}
17. 查询前过滤(推荐使用)
GET movie_index/movie/_search
{
"query": {
"bool": {
"filter": [
{"term":
{"actorList.id": 3}
},
{
"term":
{"actorList.id": 1}
}
],
"must":
{"match": {
"name": "zhang"
}}
}
}
}
18. 按范围过滤
GET movie_index/movie/_search
{
"query": {
"bool": {
"filter": {
"range": {
"doubanScore": {
"gt": 5,
"lt": 9
}
}
}
}
}
}
19. 排序
GET movie_index/movie/_search
{
"query":{
"match": {"name":"red operation"}
}
,
"sort": [
{
"doubanScore": {
"order": "desc"
}
}
]
}
20. 分页查询
GET movie_index/movie/_search
{
"query": { "match_all": {} },
"from": 1,
"size": 1
}
21. 指定查询的字段
GET movie_index/movie/_search
{
"query": { "match_all": {} },
"_source": ["name", "doubanScore"]
}
22. 聚合
# 每个演员参演了多少部电影 (按名字进行计数)
GET movie_index/movie/_search
{
"aggs": {
"groupby_actor": {
"terms": {
"field": "actorList.name.keyword"
}
}
}
}
# 每个演员参演电影的平均分是多少,并按评分排序
GET movie_index/movie/_search
{
"aggs": {
"groupby_actor_id": {
"terms": {
"field": "actorList.name.keyword" ,
"order": {
"avg_score": "desc"
}
},
"aggs": {
"avg_score":{
"avg": {
"field": "doubanScore"
}
}
}
}
}
}
23. 分词器
A.Elasticsearch自带的分词器
### 使用默认的分词器
GET movie_index/_analyze
{
"text": "我是中国人"
}
B. 中文分词器
C. 安装 IK 分词器
- 下载中文分词器
使用 IK Analysis for Elasticsearch. 下载与 ElasticSearch 的版本匹配的分词器版本.
https://github.com/medcl/elasticsearch-analysis-ik/releases
- 解压分词器
需要解压到 ES 的plugins目录下
unzip elasticsearch-analysis-ik-7.8.1.zip -d /export/servers/elasticsearch/plugins/ik
- 重启启动 ElasticSearch
D.使用 IK 分词器
#使用 ik_smart
GET movie_index/_analyze
{
"analyzer": "ik_smart",
"text": "我是中国人"
}
# 使用ik_max_word
GET movie_index/_analyze
{
"analyzer": "ik_max_word",
"text": "我是中国人"
}
24. 关于Mapping
通过 Mapping 来设置和查看每个字段的数据类型.
A. 查看mapping
GET movie_index/_mapping
B. 手动指定Mapping
- 搭建索引
# 7.X 语法是这样详情可参考官方文档
# https://www.elastic.co/blog/moving-from-types-to-typeless-apis-in-elasticsearch-7-0
PUT /movie_chn?include_type_name=true
{
"mappings": {
"movie":{
"properties": {
"id":{
"type": "long"
},
"name":{
"type": "text"
, "analyzer": "ik_smart"
},
"doubanScore":{
"type": "double"
},
"actorList":{
"properties": {
"id":{
"type":"long"
},
"name":{
"type":"keyword"
}
}
}
}
}
}
}
- 插入数据
PUT /movie_chn/movie/1
{ "id":1,
"name":"红海行动",
"doubanScore":8.5,
"actorList":[
{"id":1,"name":"张译"},
{"id":2,"name":"海清"},
{"id":3,"name":"张涵予"}
]
}
PUT /movie_chn/movie/2
{
"id":2,
"name":"湄公河行动",
"doubanScore":8.0,
"actorList":[
{"id":3,"name":"张涵予"}
]
}
PUT /movie_chn/movie/3
{
"id":3,
"name":"红海事件",
"doubanScore":5.0,
"actorList":[
{"id":4,"name":"张晨"}
]
}
- 查询
GET /movie_chn/movie/_search
{
"query": {
"match": {
"name": "红海"
}
}
}
GET /movie_chn/movie/_search
{
"query": {
"term": {
"actorList.name": "张"
}
}
}
25. 根据时间范围查询
A. 先查询时间字段的mapping,确定时间字段的格式
GET kltest-panw-traffic-taged*/_mapping
B.根据时间字段进行查询
GET kltest-panw-traffic-taged*/_mapping
{
"size": 10,
"query": {
"range": {
"event.start": {
"gte": "2021/04/14 09:37:00",
"lte": "2021/04/14 09:40:00"
}
}
}
}