本文主要是介绍RestClient操作索引库和文档,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
导入依赖:
<dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>7.12.1</version></dependency>
导入依赖成功之后就可以开始我们的测试了!!!
RestClient操作索引库
创建RestClient
在测试方法中进行测试,首先我们需要创建一个RestHighLevelClient,使用@BeforeEach注解进行create,使用@AfterEach关闭链接。然后我们打印client
private RestHighLevelClient client;/*** BeforeEach注解是指,在执行其他测试方法之前,必须先执行一次该方法*/@BeforeEachvoid setUp() {client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://公网ip:9200")));}/*** AfterEach注解是指,在执行其他测试方法之后,必须先执行一次该方法*/@AfterEachvoid tearDown() throws IOException {client.close();}
新增索引库
- 创建Request对象
- 准备请求的参数:DSL语句
- 发生请求
@Testvoid createHotelIndex() throws IOException {// 1. 创建Request对象CreateIndexRequest request = new CreateIndexRequest("user");// 2. 准备请求的参数:DSL语句request.source("{\n" +" \"mappings\": {\n" +" \"properties\": {\n" +" \"id\": {\n" +" \"type\": \"keyword\"\n" +" },\n" +" \"name\": {\n" +" \"type\": \"text\",\n" +" \"analyzer\": \"ik_max_word\"\n" +" }\n" +" }\n" +" }\n" +"}", XContentType.JSON);// 3. 发生请求client.indices().create(request, RequestOptions.DEFAULT);}
运行成功后我们回到Dev Tools查询
GET /usr
删除和查看索引库
@Testvoid deleteHotelIndex() throws IOException {DeleteIndexRequest request = new DeleteIndexRequest("user");client.indices().delete(request, RequestOptions.DEFAULT);}@Testvoid existsHotelIndex() throws IOException {GetIndexRequest request = new GetIndexRequest("user");boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);System.out.println(exists ? "索引库已经存在" : "索引库不存在");}
RestClient操作文档
准备工作
这里我们配合mysql来完成文档的操作,要使用mybatis-plus,所以给该测试类加上@SpringBootTest,并注入相应的mapper或者service。
user的sql语句
DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user` (`id` bigint(25) NOT NULL AUTO_INCREMENT,`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 12 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;INSERT INTO `tb_user` VALUES (1, 'zhangsan');
INSERT INTO `tb_user` VALUES (2, 'lisi');
INSERT INTO `tb_user` VALUES (3, 'wangwu');
INSERT INTO `tb_user` VALUES (4, 'zhaoliu');
INSERT INTO `tb_user` VALUES (5, 'xiaomei');
INSERT INTO `tb_user` VALUES (6, 'xiaobai');
INSERT INTO `tb_user` VALUES (7, 'at');
INSERT INTO `tb_user` VALUES (8, 'sh');
INSERT INTO `tb_user` VALUES (9, 'wsgp');
INSERT INTO `tb_user` VALUES (10, 'iop');
INSERT INTO `tb_user` VALUES (11, 'askdhgk');
user实体类
package cn.itcast.hotel.pojo;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;@Data
@TableName("tb_user")
public class User {@TableId(type = IdType.INPUT)private Long id;private String Name;
}
UserMapper类
package cn.itcast.hotel.mapper;import cn.itcast.hotel.pojo.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;public interface UserMapper extends BaseMapper<User> {
}
单个文档插入
@Testvoid addDoc() throws IOException {User user = userMapper.selectById(1);System.out.println(user);IndexRequest request = new IndexRequest("user").id(user.getId().toString());request.source(JSON.toJSONString(user), XContentType.JSON);client.index(request, RequestOptions.DEFAULT);}
查看是否插入成功:
查询文档
@Testvoid getDoc() throws IOException {GetRequest request = new GetRequest("user", "1");GetResponse response = client.get(request, RequestOptions.DEFAULT);String json = response.getSourceAsString();User user = JSON.parseObject(json, User.class);System.out.println(user);}
更新和删除文档
这里有全量更新和局部更新,全量更新就和插入文档一样,所以这里只介绍局部更新。
/*** 局部更新*/@Testvoid updateDoc() throws IOException {UpdateRequest request = new UpdateRequest("user", "1");request.doc("name", "zs");client.update(request, RequestOptions.DEFAULT);}/*** 删除指定文档* @throws IOException*/@Testvoid deleteDoc() throws IOException {DeleteRequest request = new DeleteRequest("user", "1");client.delete(request, RequestOptions.DEFAULT);}
批量插入文档
/*** 批量插入* @throws IOException*/@Testvoid addDocBulk() throws IOException {List<User> users = userMapper.selectList(null);BulkRequest request = new BulkRequest();for (User user : users) {request.add(new IndexRequest("test").id(user.getId().toString()).source(JSON.toJSONString(user), XContentType.JSON));}client.bulk(request, RequestOptions.DEFAULT);}
这篇关于RestClient操作索引库和文档的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!