1. 分词器
es在创建倒排索引时需要对文档分词;在搜索时,需要对用户输入内容分词。但默认的分词规则对中文处理并不友好。
我们在kibana的DevTools中测试:
语法说明:
- POST:请求方式
- /_analyze:请求路径,这里省略了http://192.168.150.101:9200,有kibana帮我们补充
- 请求参数,json风格:
analyzer:分词器类型,这里是默认的standard分词器
text:要分词的内容
处理中文分词,一般会使用IK分词器。https://github.com/medcl/elasticsearch-analysis-ik
ik分词器包含两种模式:
- ik_smart:最少切分,粗粒度
- ik_max_word:最细切分,细粒度
1.1 ik分词器-拓展词库
要拓展ik分词器的词库,只需要修改一个ik分词器目录中的config目录中的IkAnalyzer.cfg.xml文件:
然后在名为ext.dic的文件中,添加想要拓展的词语即可:
1.2 ik分词器-停用词库
要禁用某些敏感词条,只需要修改一个ik分词器目录中的config目录中的IkAnalyzer.cfg.xml文件:
然后在名为stopword.dic的文件中,添加想要拓展的词语即可:
1.3 小结
分词器的作用是什么?
- 创建倒排索引时对文档分词
- 用户搜索时,对输入的内容分词
IK分词器有几种模式?
- ik_smart:智能切分,粗粒度
- ik_max_word:最细切分,细粒度
IK分词器如何拓展词条?如何停用词条?
- 利用config目录的IkAnalyzer.cfg.xml文件添加拓展词典和停用词典
- 在词典中添加拓展词条或者停用词条
2. 索引库操作
mapping是对索引库中文档的约束,常见的mapping属性包括:
- type:字段数据类型,常见的简单类型有:
字符串:text(可分词的文本)、keyword(精确值,例如:品牌、国家、ip地址)
数值:long、integer、short、byte、double、float、
布尔:boolean
日期:date
对象:object - index:是否创建索引,默认为true
- analyzer:使用哪种分词器
- properties:该字段的子字段
2.1 创建索引库
ES中通过Restful请求操作索引库、文档。请求内容用DSL语句来表示。创建索引库和mapping的DSL语法如下:
2.2 查看、删除索引库
查看索引库语法:
示例:
删除索引库的语法:
示例:
3.3 修改索引库
索引库和mapping一旦创建无法修改,但是可以添加新的字段,语法如下:
示例:
3.4 小结
索引库操作有哪些?
- 创建索引库:PUT /索引库名
- 查询索引库:GET /索引库名
- 删除索引库:DELETE /索引库名
- 添加字段:PUT /索引库名/_mapping
3. 文档操作
3.1 添加文档
新增文档的DSL语法如下:
3.2 查看、删除文档
查看文档语法:
示例:
删除索引库的语法:
示例:
3.3 修改文档
方式一:全量修改,会删除旧文档,添加新文档
方式二:增量修改,修改指定字段值
3.4 小结
文档操作有哪些?
- 创建文档:POST /索引库名/_doc/文档id { json文档 }
- 查询文档:GET /索引库名/_doc/文档id
- 删除文档:DELETE /索引库名/_doc/文档id
修改文档:
- 全量修改:PUT /索引库名/_doc/文档id { json文档 }
- 增量修改:POST /索引库名/_update/文档id { “doc”: {字段}}
4. RestClient操作索引库
什么是索引库?
ES官方提供了各种不同语言的客户端,用来操作ES。这些客户端的本质就是组装DSL语句,通过http请求发送给ES。官方文档地址:https://www.elastic.co/guide/en/elasticsearch/client/index.html
4.1 创建索引库
- 初始化JavaRestClient
(1)引入es的RestHighLevelClient依赖:
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
(2)因为SpringBoot默认的ES版本是7.6.2,所以我们需要覆盖默认的ES版本:
<properties>
<java.version>1.8</java.version>
<elasticsearch.version>7.12.1</elasticsearch.version>
</properties>
(3)初始化RestHighLevelClient:
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://192.168.150.101:9200")));
- 创建索引库
创建索引库代码如下:
@Test
void createHotelIndex() throws IOException {
// 1.创建Request对象
CreateIndexRequest request = new CreateIndexRequest("hotel");
// 2.准备请求的参数:DSL语句
request.source(MAPPING_TEMPLATE, XContentType.JSON);
// 3.发送请求
client.indices().create(request, RequestOptions.DEFAULT);
}
4.2 删除索引库
- 删除索引库代码如下
@Test
void testDeleteHotelIndex() throws IOException {
// 1.创建Request对象
DeleteIndexRequest request = new DeleteIndexRequest("hotel");
/ 2.发起请求
client.indices().delete(request, RequestOptions.DEFAULT);
}
4.4 判断索引库是否存在
- 判断索引库是否存在
@Test
void testExistHotelIndex() throws IOException {
// 1.创建Request对象
GetIndexRequest request = new GetIndexRequest("hotel");
// 2.发起请求
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
// 3.输出
System.out.println(exists ? "索引库已经存在!" : "索引库不存在!");
}
4.5 索引库操作的基本步骤
- 初始化RestHighLevelClient
- 创建XxxIndexRequest。XXX是Create、Get、Delete
- 准备DSL( Create时需要)
- 发送请求。调用RestHighLevelClient#indices().xxx()方法,xxx是create、exists、delete
5. RestClient操作文档
5.1 新增文档数据
- 初始化JavaRestClient
@BeforeEach
void setUp() {
this.client = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://192.168.169.128:9200")
));
}
@AfterEach
void tearDown() throws IOException {
this.client.close();
}
- 添加酒店数据到索引库
@Test
void testAddDocument() throws IOException {
// 根据id查询酒店数据
Hotel hotel = hotelService.getById(36934L);
// 转换为文档类型
HotelDoc hotelDoc = new HotelDoc(hotel);
// 1.准备Request对象
IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());
// 2.准备Json文档
request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);
// 3.发送请求
client.index(request, RequestOptions.DEFAULT);
}
5.3 查询文档数据
@Test
void testGetDocumentById() throws IOException {
// 1.准备Request对象
GetRequest request = new GetRequest("hotel", "36934");
// 2.发送请求,得到响应
GetResponse response = client.get(request, RequestOptions.DEFAULT);
// 3.解析响应结果
String json = response.getSourceAsString();
HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
System.out.println(hotelDoc);
}
5.4 修改文档数据
修改文档数据有两种方式:
方式一:全量更新。再次写入id一样的文档,就会删除旧文档,添加新文档
方式二:局部更新。只更新部分字段,我们演示方式二
@Test
void testUpdateDocumentById() throws IOException {
// 1.准备request请求
UpdateRequest request = new UpdateRequest("hotel", "36934");
// 2.准备请求参数
request.doc(
"price", "952",
"starName", "四钻"
);
// 3.发送请求
client.update(request, RequestOptions.DEFAULT);
}
5.5 删除文档数据
@Test
void testDeleteDocument() throws IOException {
// 1.请求Request
DeleteRequest request = new DeleteRequest("hotel", "36934");
// 2.发送请求
client.delete(request, RequestOptions.DEFAULT);
}
5.6 利用JavaRestClient批量导入酒店数据到ES
需求:批量查询酒店数据,然后批量导入索引库中
思路:
- 利用mybatis-plus查询酒店数据
- 将查询到的酒店数据(Hotel)转换为文档类型数据(HotelDoc)
- 利用JavaRestClient中的Bulk批处理,实现批量新增文档,示例代码如下
void testBulkRequest() throws IOException {
// 批量查询数据
List<Hotel> hotels = hotelService.list();
// 1.创建Request
BulkRequest bulkRequest = new BulkRequest();
// 2.准备参数,添加多个新增的Request
for (Hotel hotel : hotels) {
// 转换为文档类型
HotelDoc hotelDoc = new HotelDoc(hotel);
bulkRequest.add(new IndexRequest("hotel")
.id(hotelDoc.getId().toString())
.source(JSON.toJSON(hotelDoc), XContentType.JSON));
}
// 3.发送请求
client.bulk(bulkRequest, RequestOptions.DEFAULT);
}