5.整合(最新高级版)
下面使用高级别客户端方式进行springboot整合ES,操作步骤如下:
(1)导入springboot整合ES高级别客户端的坐标
此种形式目前没有对应的starter,需要去找。
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> </dependency>
(2)使用编程的形式设置连接的ES服务器,并获取客户端对象
@SpringBootTest class HighClientTest { private RestHighLevelClient client; @Test void testCreateClient() throws IOException { HttpHost host = HttpHost.create("http://localhost:9200"); RestClientBuilder builder = RestClient.builder(host); client = new RestHighLevelClient(builder); client.close(); } }
注: 记得客户端使用完毕需要手工关闭。配置ES服务器地址与端口9200,由于当前客户端是手工维护的,因此不能通过自动装配的形式加载对象。
(3)使用客户端对象操作ES
例如创建索引:(这里需要先执行上面的删除索引操作,否则会报错)
@SpringBootTest class HighClientTest{ private RestHighLevelClient client; @Test void testCreateIndex() throws IOException { HttpHost host = HttpHost.create("http://localhost:9200"); RestClientBuilder builder = RestClient.builder(host); client = new RestHighLevelClient(builder); CreateIndexRequest request = new CreateIndexRequest("user"); client.indices().create(request, RequestOptions.DEFAULT); client.close(); } }
注:第一步永远是获取RestHighLevelClient对象,创建索引的对象是CreateIndexRequest,其他操作也会有自己专用的Request对象。最后一步永远是关闭该对象的连接。可以得出以下结论,进行方法提取。
@SpringBootTest class Springboot18EsApplicationTests { @BeforeEach //在测试类中每个操作运行前运行的方法 void setUp() { HttpHost host = HttpHost.create("http://localhost:9200"); RestClientBuilder builder = RestClient.builder(host); client = new RestHighLevelClient(builder); } @AfterEach //在测试类中每个操作运行后运行的方法 void tearDown() throws IOException { client.close(); } private RestHighLevelClient client; @Test void testCreateIndex() throws IOException { CreateIndexRequest request = new CreateIndexRequest("book"); client.indices().create(request, RequestOptions.DEFAULT); } }
现在的书写简化了很多,也更合理。下面使用上述模式将所有的ES操作执行一遍,测试结果
创建索引(IK分词器):
@Test void testCreateIndexByIK() throws IOException { CreateIndexRequest request = new CreateIndexRequest("books"); String json = "{\n" + " \"mappings\":{\n" + " \"properties\":{\n" + " \"id\":{\n" + " \"type\":\"keyword\"\n" + " },\n" + " \"name\":{\n" + " \"type\":\"text\",\n" + " \"analyzer\":\"ik_max_word\",\n" + " \"copy_to\":\"all\"\n" + " },\n" + " \"type\":{\n" + " \"type\":\"keyword\"\n" + " },\n" + " \"description\":{\n" + " \"type\":\"text\",\n" + " \"analyzer\":\"ik_max_word\",\n" + " \"copy_to\":\"all\"\n" + " },\n" + " \"all\":{\n" + " \"type\":\"text\",\n" + " \"analyzer\":\"ik_max_word\"\n" + " }\n" + " }\n" + " }\n" + "}"; //设置请求中的参数 request.source(json, XContentType.JSON); client.indices().create(request, RequestOptions.DEFAULT); }
注:IK分词器是通过请求参数的形式进行设置的,设置请求参数使用request对象中的source方法进行设置,至于参数是什么,取决于你的操作种类。当请求中需要参数时,均可使用当前形式进行参数设置。
添加文档:
@Test //添加文档 void testCreateDoc() throws IOException { User user = userDao.selectById(1); IndexRequest request = new IndexRequest("user").id(book.getId().toString()); String json = JSON.toJSONString(book); request.source(json,XContentType.JSON); client.index(request,RequestOptions.DEFAULT); }
添加文档使用的请求对象是IndexRequest,与创建索引使用的请求对象不同。
批量添加文档:
@Test //批量添加文档 void testCreateDocAll() throws IOException { List<User> userList = userDao.selectList(null); BulkRequest bulk = new BulkRequest(); for (User user : userList) { IndexRequest request = new IndexRequest("user").id(user.getId().toString()); String json = JSON.toJSONString(book); request.source(json,XContentType.JSON); bulk.add(request); } client.bulk(bulk,RequestOptions.DEFAULT); }
注:批量做时,先创建一个BulkRequest的对象,可以将该对象理解为是一个保存request对象的容器,将所有的请求都初始化好后,添加到BulkRequest对象中,再使用BulkRequest对象的bulk方法,一次性执行完毕。
按id查询文档:
@Test //按id查询 void testGet() throws IOException { GetRequest request = new GetRequest("user","1"); GetResponse response = client.get(request, RequestOptions.DEFAULT); String json = response.getSourceAsString(); System.out.println(json); }
根据id查询文档使用的请求对象是GetRequest。
按条件查询文档:
@Test //按条件查询 void testSearch() throws IOException { SearchRequest request = new SearchRequest("user"); SearchSourceBuilder builder = new SearchSourceBuilder(); builder.query(QueryBuilders.termQuery("all","spring")); request.source(builder); SearchResponse response = client.search(request, RequestOptions.DEFAULT); SearchHits hits = response.getHits(); for (SearchHit hit : hits) { String source = hit.getSourceAsString(); //System.out.println(source); Book book = JSON.parseObject(source, Book.class); System.out.println(book); } }