索引管理
ES 提供不同的客户端对索引进行管理 .
其中 RestClient 是官方推荐的, 它包括 Java Low Level REST Client 和 Java High Level REST Client, 官方推荐使用 Java High Level REST Client.
步骤 :
- 创建搜索工程
- 添加依赖
<!-- elasticSearch 依赖 -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.5.4</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.5.4</version>
</dependency>
- 测试
创建索引库
URL 方式
创建索引 :
put http://localhost:9200/索引名称
{
"settings":{
"index":{
"number_of_shards":1,#分片的数量
"number_of_replicas":0#副本数量
}
}
}
创建映射 :
put http://localhost:9200/索引库名称 /类型名称/_mapping
{
"properties": {
"description": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"name": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"pic": {
"type": "text",
"index": false
}
}
}
Java Client 方式
//测试创建索引
@Test
public void testCreateIndex() throws IOException {
// 创建索引对象
CreateIndexRequest request = new CreateIndexRequest("video");
// 封装索引对象
request.settings(Settings.builder()
.put("number_of_shards",1)
.put("number_of_replicas",0));
// 创建映射
request.mapping("doc","{\n" +
"\t\"properties\": {\n" +
"\t\t\"description\": {\n" +
"\t\t\t\"type\": \"text\",\n" +
"\t\t\t\"analyzer\": \"ik_max_word\",\n" +
"\t\t\t\"search_analyzer\": \"ik_smart\"\n" +
"\t\t},\n" +
"\t\t\"name\": {\n" +
"\t\t\t\"type\": \"text\",\n" +
"\t\t\t\"analyzer\": \"ik_max_word\",\n" +
"\t\t\t\"search_analyzer\": \"ik_smart\"\n" +
"\t\t},\n" +
"\t\t\"pic\": {\n" +
"\t\t\t\"type\": \"text\",\n" +
"\t\t\t\"index\": false\n" +
"\t\t}\n" +
"\t}\n" +
"}\t\n", XContentType.JSON);
// 创建索引操作客户端
IndicesClient indices = highLevelClient.indices();
// 返回索引结果
CreateIndexResponse createIndexResponse = indices.create(request);
boolean shardsAcknowledged = createIndexResponse.isShardsAcknowledged();
// 打印是否索引成功
System.out.println(shardsAcknowledged);
}
添加文档
URL 方式
put http://localhost:9200/video/doc/3
{
"name":"spring cloud实战",
"description":"本课程主要从四个章节进行讲解1.微服务架构入门 2.spring cloud\n" +
"基础入门 3.实战Spring Boot 4.注册中心eureka",
"pic":"/xxx/xxx/a/a.png"
}
Java Client 方式
//测试添加文档
@Test
public void testAddDoc() throws IOException {
//构建文档
Map<String,Object> map = new HashMap<>();
map.put("name","spring cloud实战");
map.put("pic","/xxx/xxx/a/a.png");
map.put("description","本课程主要从四个章节进行讲解1.微服务架构入门 2.spring cloud\n" +
"基础入门 3.实战Spring Boot 4.注册中心eureka");
//创建索引请求对象
IndexRequest request = new IndexRequest("video","doc");
//指定索引文档的内容
request.source(map);
//索引响应对象
IndexResponse response = highLevelClient.index(request);
DocWriteResponse.Result result = response.getResult();
System.out.println(result);
}
查询文档
URL 方式
get http://localhost:9200/{index}/{type}/{id}
Java Client 方式
//查询文档
@Test
public void getDoc() throws IOException {
GetRequest request = new GetRequest("video","doc","tOFEWmwBMVTTR-zqV5vG");
GetResponse response = highLevelClient.get(request);
Map<String, Object> sourceAsMap = response.getSourceAsMap();
System.out.println(sourceAsMap);
}
更新文档
URL 方式
ES 更新文档的顺序为 :
检索到文档, 将原来的文档标记为删除, 创建新文档, 删除旧文档, 创建新文档就会重建索引.
方式一 : 完全替换
POST : http://localhost:9200/video/doc/3
{
"name": "spring cloud实战",
"description": "本课程主要从四个章节进行讲解: 1.微服务架构入门 2.spring cloud 基础入门",
"pic": "/werw/wre/w/w.png"
}
方式二 : 局部更新
只更新 name 字段 :
POST : http://localhost:9200/video/doc/3/_update
{
"doc": {
"name": "Springboot 实战"
}
}
Java Client 方式
//测试更新文档
@Test
public void testUpdateDoc() throws IOException {
UpdateRequest request = new UpdateRequest
("video","doc","tOFEWmwBMVTTR-zqV5vG");
Map<String,Object> map = new HashMap<>();
map.put("name","SpringBoot实战");
request.doc(map);
UpdateResponse update = highLevelClient.update(request);
RestStatus status = update.status();
System.out.println(status);
}
删除文档
url 方式
根据 id 删除 :
DELETE /{index}/{type}/{id}
搜索匹配删除, 将搜索出来的记录删除, 格式如下:
POST /{index}/{type}/_delete_by_query
例子:
{
"query": {
"term": {
"name": "Springboot 实战"
}
}
}
会将查询到的 name 为 springboot 实战的记录删除.
Java Client 方式
//根据id删除文档
@Test
public void testDeleteDoc() throws IOException {
DeleteRequest request = new DeleteRequest("video","doc","tOFEWmwBMVTTR-zqV5vG");
DeleteResponse response = highLevelClient.delete(request);
System.out.println(response.getResult());
}