1. 程式人生 > >SpringBoot整合Jest操作ES

SpringBoot整合Jest操作ES

bsp stc auto str pid exce autowire turn ()

(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 }

SpringBoot整合Jest操作ES