README.md
11/7/2019
1. Linux安装
1.1. 下载&安装
1.1.1. 环境需求
1.1.2. 下载
1.1.3. 安装
1.1.4. 修改配置文件
1.1.5. 启动&验证结果
1.2. 中文分词插件IK
1.2.1. 安装
1.2.2. ik_max_word和ik_smart
1.2.2.1. ik_smart分词
1.2.2.2. ik_max_word分词
1.3. 索引
1.3.1. 创建索引
1.3.1.1. 官方例子说明
1.3.1.2. 自定义索引
1.3.2. 查看索引
1.3.2.1. 全部索引
1.3.2.2. 条件查询
1.3.3. 查看索引分词器
1.3.4. 修改索引
1.3.5. 删除索引
1.4. 如何数据管理
1.4.1. 添加数据
1.4.2. 基础查询
1.4.2.1. 查询所有
1.4.2.2. 条件查询
1.4.3. 高级条件查询
1.4.3.1. 权重boost查询
1.4.3.2. 过滤coerce查询
1.4.3.2.1. 创建索引
1.4.3.2.2. 创建第一个数据
1.4.3.2.3. 创建第二个数据
1.4.3.3. copy_to
1.4.3.3.1. 定义索引
1.4.3.3.2. 新增数据
1.4.3.3.3. 查询数据
1.4.3.4. doc_values
1.4.3.5. dynamic
2. SpringBoot集成
2.1. POM
2.2. yml配置
2.3. 核心操作类
3.1. 创建索引
3. 实战
演示索引
1 / 29
README.md
11/7/2019
核心代码说明
4. 源码
1. Linux安装
1.1. 下载&安装
1.1.1. 环境需求
CentOs7
内存4G+
1.1.2. 下载
官方elasticsearch下载,下载elasticsearch,目前最新的稳定版本为 7.4.0 版本.
1.1.3. 安装
[root@localhost download]$ pwd
/data/download/
[root@localhost download]$ wget
https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.4.0-linux-
x86_64.tar.gz
[root@localhost download]$ cd ../app/
[root@localhost app]$ mkdir elastic
[root@localhost app]$ useradd elastic -g dev
[root@localhost app]$ passwd elastic
[root@localhost app]$ chown -R elastic:dev elastic
[root@localhost app]$ su elastic
[elastic@localhost app]$ cd /elastic
[elastic@localhost elastic]$ cp ../../download/elasticsearch-7.4.0-linux-
x86_64.tar.gz .
[elastic@localhost elastic]$ tar -zxvf elasticsearch-7.4.0-linux-x86_64.tar.gz
[elastic@localhost elastic]$ mv elasticsearch-7.4.0/ .
1.1.4. 修改配置文件
2 / 29
README.md
路径config/elasticsearch.yml
11/7/2019
-- 允许外部IP访问
network.host: 0.0.0.0
-- 把这个注释先放开
cluster.initial_master_nodes: ["node-1", "node-2"]
1.1.5. 启动&验证结果
启动
[elastic@localhost elastic]$ ./bin/elasticsearch
验证结果
Elastic会在默认9200端口运行,打开地址:http://192.168.147.132:9200/
1.2. 中文分词插件IK
1.2.1. 安装
ik插件地址: https://github.com/medcl/elasticsearch-analysis-ik,为了演示需要,这里选择wget方式。
下载
[root@localhost download]$ wget https://github.com/medcl/elasticsearch-analysis-
ik/releases/download/v7.4.0/elasticsearch-analysis-ik-7.4.0.zip
安装插件
3 / 29
README.md
11/7/2019
[elastic@localhost elastic]$ cd plugins
[elastic@localhost plugins]$ cd mkdir ik && cd ik
[elastic@localhost ik]$ cp ../../../download/elasticsearch-analysis-ik-7.4.0.zip .
[elastic@localhost ik]$ unzip elasticsearch-analysis-ik-7.4.0.zip
完成后重启es
验证分词器
使用crul命令,输入下面的URL地址,验证分词器是否成功。
[elastic@localhost elastic]$ curl -X GET -H "Content-Type: application/json"
"http://localhost:9200/_analyze?pretty=true" -d'{"text":"中华五千年华夏"}';
1.2.2. ik_max_word和ik_smart
ik_max_word: 将文本按最细粒度的组合来拆分,比如会将“中华五千年华夏”拆分为“五千年、五千、五
千年华、华夏、千年华夏”,总之是可能的组合;
ik_smart: 最粗粒度的拆分,比如会将“五千年华夏”拆分为“五千年、华夏”
Elastic
standard
ik
4 / 29
不
添
加
分
词
类
别
,
对
于
汉
字
默
认
使
用
只
是
将
汉
字
拆
分
成
一
个
个
的
汉
字
,
而
我
们
则
更
加
的
智
能
,
下
面
通
过
几
个
案
例
来
说
明
。
README.md
1.2.2.1. ik_smart
在JSON格式中添加analyzer节点内容为ik_smart
11/7/2019
[elastic@localhost elastic]$ curl -X GET -H "Content-Type: application/json"
"http://localhost:9200/_analyze?pretty=true" -d'{"text":"中华五千年华
夏","analyzer": "ik_smart"}';
1.2.2.2. ik_max_word
在JSON格式中添加analyzer节点内容为ik_max_word
[elastic@localhost elastic]$ curl -X GET -H "Content-Type: application/json"
"http://localhost:9200/_analyze?pretty=true" -d'{"text":"中华五千年华
夏","analyzer": "ik_max_word"}';
1.3. 索引
1.3.1. 创建索引
5 / 29
分
词
分
词
README.md
11/7/2019
由于在ElasticSearch 7.x之后就默认不在支持指定索引类型,所以在在elasticsearch7.x上执行:
{
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
},
"mappings" : {
"twitter":{
......
}
}
执行结果则会出错:Root mapping definition has unsupported parameters(刚开始接触就踩了这个坑,折煞
劳资好久)。如果在6.x上执行,则会正常执行。 出现这个的原因是,elasticsearch7默认不在支持指定索引类
型,默认索引类型是_doc,如果想改变,则配置include_type_name: true 即可(这个没有测试,官方文档说的,
无论是否可行,建议不要这么做,因为elasticsearch8后就不在提供该字段)。
https://www.elastic.co/guide/en/elasticsearch/reference/current/removal-of-types.html
1.3.1.1.
curl -X PUT "localhost:9200/twitter" -H 'Content-Type: application/json' -d'
{
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
}
}
'
-d指定了你的参数,这里将这些参数放到了json文件中
settings设置内容含义
name
number_of_shards 分片数
number_of_replicas 副本数
mappings
properties
结构化数据设置 下面的一级属性 是自定义的类型
类型的属性设置节点,下面都是属性
6 / 29
官
方
例
子
说
明
价
格
README.md
name
epoch_millis
1.3.1.2.
表示时间戳
11/7/2019
使用json文件创建索引 使用 -d‘@your jsonFile’指定你的json文件。下边我创建了一个索引名称为
product(可自己定义)的索引。
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X PUT
"http://localhost:9200/twitter?pretty=true" -d'@prod.json'
参数形式创建索引
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X PUT
"http://localhost:9200/twitter?pretty=true" -d'
{
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
},
"mappings" : {
"dynamic": false,
"properties" : {
"productid":{
"type" : "long"
},
"name":{
"type":"text",
"index":true,
"analyzer":"ik_max_word"
},
"short_name":{
"type":"text",
"index":true,
"analyzer":"ik_max_word"
},
"desc":{
"type":"text",
"index":true,
"analyzer":"ik_max_word"
}
7 / 29
价
格
自
定
义
索
引
README.md
}
}
}
'
11/7/2019
1.3.2. 查看索引
1.3.2.1.
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X GET
"http://localhost:9200/_cat/indices?v"
health status index uuid pri rep docs.count docs.deleted
store.size pri.store.size
yellow open twitter scSSD1SfRCio4F77Hh8aqQ 3 2 0 0
690b 690b
1.3.2.2.
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X GET
"http://localhost:9200/twitter?pretty=true"
{
8 / 29
全
部
索
引
条
件
查
询