Skip to content

Commit 68a67e5

Browse files
committed
新增笔记:Elasticsearch索引字段映射
1 parent ed268c7 commit 68a67e5

File tree

19 files changed

+507
-16
lines changed

19 files changed

+507
-16
lines changed
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading

基础笔记/数据库/Elasticsearch/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
[Elasticsearch核心概念](./subfile/_1Elasticsearch核心概念.md)
44

55
[Elasticsearch索引管理](./subfile/_2Elasticsearch索引管理.md)
6+
7+
[Elasticsearch索引映射](_4Elasticsearch索引字段映射.md)

基础笔记/数据库/Elasticsearch/subfile/_2Elasticsearch索引管理.md

Lines changed: 76 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,30 @@
22

33
ElasticSearch的数据交互接口是基于HTTP协议实现的,基本格式如下:
44

5-
## 一. 新增索引
5+
## 一. 创建索引
66

7-
格式:
7+
默认情况下,我们向一个不存在的的索引添加数据时,就会创建这个索引:
8+
9+
![](../images/9.png)
10+
11+
我们向一个不存在的employee索引添加一条数据,那么默认情况下ES会帮我们自动创建索引,并且也会自动帮我们创建字段映射:
12+
![](../images/9.png)
13+
14+
### 1.1 禁止自动创建索引
15+
16+
那么如果我们需要对这个建立索引的过程做更多的控制:比如想要确保这个索引有数量适中的主分片,并且在我们索引任何数据之前,分析器和映射已经被建立好。那么就会引入两点:第一个**禁止自动创建索引**,第二个是**手动创建索引**
17+
18+
- 禁止自动创建索引
19+
20+
可以通过在 config/elasticsearch.yml 的每个节点下添加下面的配置:
21+
22+
```bash
23+
action.auto_create_index: false
24+
```
25+
26+
手动创建索引就是接下来文章的内容。
27+
28+
### 1.2 手动创建索引
829

930
```shell
1031
PUT /{index_name}
@@ -42,15 +63,26 @@ PUT /order
4263
PUT /order2
4364
{
4465
"settings": {
45-
"number_of_shards": 3,
46-
"number_of_replicas": 1
47-
},
66+
"number_of_shards": 1,
67+
"number_of_replicas": 1
68+
},
4869
"mappings": {
4970
"properties": {
50-
"title": { "type": "text" },
51-
"content": { "type": "text" },
52-
"price": { "type": "scaled_float", "scaling_factor": 100 },
53-
"publish_date": { "type": "date" }
71+
"name": {
72+
"type": "text",
73+
"fields": {
74+
"keyword": {
75+
"type": "keyword",
76+
"ignore_above": 256
77+
}
78+
}
79+
},
80+
"age": {
81+
"type": "long"
82+
},
83+
"remarks": {
84+
"type": "text"
85+
}
5486
}
5587
}
5688
}
@@ -114,11 +146,6 @@ GET /{index_name}
114146
115147
### 3.1 修改setting配置
116148
117-
`settings` 里面主要是对索引的全局属性、存储、性能、分片分配等多方面的配置,下面是索引中比较重要的两个配置:
118-
119-
- number_of_shards:索引应该具有的主分片数量。7.x及以上版本默认值为`1`,6.x及以下版本默认值为`5`**此设置只能在创建索引时设置,在关闭的索引上也不能修改**
120-
- number_of_replicas:每个主分片的副本数,默认值是 1 。对于活动的索引库,这个配置可以随时修改。
121-
122149
修改settings配置的格式:
123150
124151
```txt
@@ -128,11 +155,11 @@ PUT /{index_name}/_settings
128155
}
129156
```
130157
131-
我们可以用该API动态修改 order 索引的副本数
158+
我们可以用该API动态修改 order 索引分片的副本数
132159
133160
![](../images/4.png)
134161
135-
如果尝试修改 `number_of_shards` 则会报错:
162+
`number_of_shards` 只能在创建索引时指定,如果尝试修改 `number_of_shards` 则会报错:
136163
137164
![](../images/5.png)
138165
@@ -176,3 +203,36 @@ DELETE /logs-*
176203
DELETE /my_index?master_timeout=30s&timeout=30s
177204
```
178205
206+
## 五. 打开关闭索引
207+
208+
### 5.1 关闭索引
209+
210+
一旦索引被关闭,那么这个索引只能显示元数据信息,**不能够进行读写操作**
211+
212+
语法格式:
213+
214+
```txt
215+
POST /{index_name}/_close
216+
```
217+
218+
关闭索引后,操作数据报错:
219+
220+
![](../images/11.png)
221+
222+
### 5.2 打开索引
223+
224+
索引打开后又可以重新写数据了
225+
226+
语法格式:
227+
228+
```txt
229+
POST /{index_name}/_open
230+
```
231+
232+
## 六. kibana管理索引
233+
234+
在Kibana如下路径,我们可以查看和管理索引
235+
236+
![](../images/13.png)
237+
238+
![](../images/12.png)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# 文档管理
2+
3+
ElasticSearch的数据交互接口是基于HTTP协议实现的,基本格式如下:
4+
5+
```
6+
http://localhost:9200/{index}/{type}/{id}
7+
```
8+
9+
- **index:索引名称,可以类比关系型数据库的表**
10+
- **type:类型名称,需要注意的是,在7.x之后,去掉了type属性,默认用“_doc”,8.x不再支持在请求中指定类型**
11+
- **id:即id,可以不指定,elasticSearch会自动生成**
12+
- **文档:即对象的json序列化**
13+
- ***元数据:即elasticSearch的数据格式,一般如下,”_source”对应的数据即为我们存储的文档\***
14+
15+
## 一. 插入文档
16+
17+
```bash
18+
POST /{index_name}/_doc/{id}
19+
{
20+
...
21+
}
22+
```
23+
24+
- index_name:索引名称
25+
- id:文档ID,该参数非必填。如果未填写ID则会新增文档,并自动生成ID。指定ID后,如果文档存在,则会更新;若不存在,则新增。
26+
27+
新增文档,不指定ID:
28+
29+
![](../images/6.png)
30+
31+
新增文档,指定ID:
32+
33+
![](../images/7.png)
34+
35+
## 二. 更新文档
36+
37+
```bash
38+
PUT /{index_name}/_doc/{id}
39+
{
40+
...
41+
}
42+
```
43+
44+
- index_name:索引名称
45+
- id:文档ID,必填。
46+
47+
## 三. 删除文档
48+
49+
##
50+
51+
```bash
52+
DELETE /{index_name}/_doc/{id}
53+
```
54+
55+
- index_name:索引名称
56+
- id:文档ID,必填。
57+
58+
![](../images/8.png)
59+
60+
## 四. 查询文档
61+
62+
63+

0 commit comments

Comments
 (0)