SpringBoot整合Jest操作ES

(1)、添加依赖

1         <dependency>
2             <groupId>io.searchbox</groupId>
3             <artifactId>jest</artifactId>
4             <version>6.3.1</version>
5         </dependency>

(2)、配置文件中配置相关属性

1 spring.elasticsearch.jest.uris=http://192.168.205.128:9200

(3)、使用JestClient操作ES

 1     @Autowired
 2     private JestClient jestClient;
 3 
 4     public String add() throws IOException {
 5         User user = new User(1,"fanqi","123456",1);
 6         //构建一个索引
 7         Index index = new Index.Builder(user).index("coreqi").type("user").build();
 8         //执行
 9         DocumentResult result =jestClient.execute(index);
10         return result.getJsonString();
11     }
12 
13     public String search() throws IOException {
14         String searchJson = "{\n" +
15                 "    \"query\": {\n" +
16                 "        \"match\": {\n" +
17                 "            \"UserName\": \"fanqi\"\n" +
18                 "        }\n" +
19                 "    }\n" +
20                 "}";
21         //构建一个搜索
22         Search search = new Search.Builder(searchJson).addIndex("coreqi").addType("user").build();
23         //执行
24         SearchResult result = jestClient.execute(search);
25         return result.getJsonString();
26     }

猜你喜欢

转载自www.cnblogs.com/fanqisoft/p/10354027.html