es在创建倒排索引时需要对文档分词;在搜索时,需要对用户输入内容分词。但默认的分词规则对中文处理并不友好。
我们在kibana的DevTools中测试:

语法说明:
处理中文分词,一般会使用IK分词器。https://github.com/medcl/elasticsearch-analysis-ik
ik分词器包含两种模式:
要拓展ik分词器的词库,只需要修改一个ik分词器目录中的config目录中的IkAnalyzer.cfg.xml文件:

然后在名为ext.dic的文件中,添加想要拓展的词语即可:

要禁用某些敏感词条,只需要修改一个ik分词器目录中的config目录中的IkAnalyzer.cfg.xml文件:

然后在名为stopword.dic的文件中,添加想要拓展的词语即可:

分词器的作用是什么?
IK分词器有几种模式?
IK分词器如何拓展词条?如何停用词条?
mapping是对索引库中文档的约束,常见的mapping属性包括:

ES中通过Restful请求操作索引库、文档。请求内容用DSL语句来表示。创建索引库和mapping的DSL语法如下:

查看索引库语法:

示例:

删除索引库的语法:

示例:

索引库和mapping一旦创建无法修改,但是可以添加新的字段,语法如下:

示例:

索引库操作有哪些?
新增文档的DSL语法如下:

查看文档语法:

示例:

删除索引库的语法:

示例:

方式一:全量修改,会删除旧文档,添加新文档

方式二:增量修改,修改指定字段值

文档操作有哪些?
修改文档:
什么是索引库?
ES官方提供了各种不同语言的客户端,用来操作ES。这些客户端的本质就是组装DSL语句,通过http请求发送给ES。官方文档地址:https://www.elastic.co/guide/en/elasticsearch/client/index.html

(1)引入es的RestHighLevelClient依赖:
org.elasticsearch.client
elasticsearch-rest-high-level-client
(2)因为SpringBoot默认的ES版本是7.6.2,所以我们需要覆盖默认的ES版本:
1.8
7.12.1
(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);
}
@Test
void testDeleteHotelIndex() throws IOException {
// 1.创建Request对象
DeleteIndexRequest request = new DeleteIndexRequest("hotel");
/ 2.发起请求
client.indices().delete(request, RequestOptions.DEFAULT);
}
@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 ? "索引库已经存在!" : "索引库不存在!");
}
@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);
}
@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);
}
修改文档数据有两种方式:
方式一:全量更新。再次写入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);
}
@Test
void testDeleteDocument() throws IOException {
// 1.请求Request
DeleteRequest request = new DeleteRequest("hotel", "36934");
// 2.发送请求
client.delete(request, RequestOptions.DEFAULT);
}
需求:批量查询酒店数据,然后批量导入索引库中
思路:
void testBulkRequest() throws IOException {
// 批量查询数据
List 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);
}