创建索引库

索引库:逻辑概念,包括分词列表及文档列表,同一个索引库存储了相同类型的文档,相当于MySQL中的表,Mongodb中的集合

postman、curl工具创建

1
2
3
4
5
6
7
8
{
"srtting":{
"index":{
"number_of_shards": 1,
"number_of_replicas": 0
}
}
}

number_of_shards: 设置分片数量,集群中通常设置多个分片,表示一个索引库将拆分成多片分别存储不同节点,提高ES处理能力和高可用性

number_of_replicas: 设置副本数量,提高ES高可靠性

Mappings:结构化关键字,内容为空,说明索引是一个非结构化索引

创建映射

索引中每个文档都包含一个或多个 field,创建映射就是向索引库中创建 field 的过程

document 和 field 关系数据库概念类比
文档 document row 记录
字段 field columns 列

6.0 之前 type 相当于关系数据库的表,9.0以后删除 type

ES-head 使用

http://localhost:9200/索引/类型/id [post]

1
2
3
4
5
{
"name": "zhangsan"
"age": "19"
"country": "China"
}

http://localhost:9200/_search [get]
http://localhost:9200/xc_course/user/_search?q=name:computer [get]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{
"took": 2, // 本次操作花费时间,单位毫秒
"timed_out": false, // 请求是否超时
"_shards": { // 说明本次操作共搜索了那些分片
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": { // 搜索命中的记录
"total": { // 符合条件的文档总数
"value": 1,
"relation": "eq"
},
"max_score": 0.4700036, // 文档匹配最高分
"hits": [ // 匹配度较高的前 N 个文档
{
"_index": "xc_course",
"_type": "user",
"_id": "FIxOfHQBxoWFrDoiNWdH",
"_score": 0.4700036, // 当前匹配文档的得分
"_source": { // 匹配文档的原始内容
"name": "computer",
"country": "China",
"price": "1825",
"date": "2020-12-18"
}

]
}
}

http://localhost:9200/_analyze [post]

1
2
3
{
"text": "测试分词内容:elasticsearch"
}

默认分词对中文是单字分词,因为是单词分词

ES集成方法:解压,文件拷贝到 ES 安装目录下的 plugins > ik 目录下

IK两种分词模式:ik_max_word 和 ik_smart
ik_max_word:最细粒度
ik_smart:最粗粒度