與我們kibana語法幾乎無縫銜接
@SpringBootTest
public class RestHighLevelClientTest {
private RestHighLevelClient restHighLevelClient;
@Autowired
public RestHighLevelClientTest(RestHighLevelClient restHighLevelClient) {
this.restHighLevelClient = restHighLevelClient;
}
//添加索引&映射
@Test
public void testIndexAndMapping() throws IOException {
//參數1:創建索引請求對象 參數2:請求配置對象
CreateIndexRequest createIndexRequest = new CreateIndexRequest("products");
//一般在kibana中寫好后再復制過來
createIndexRequest.mapping("{\n" +
" \"properties\": {\n" +
" \"title\":{\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"price\":{\n" +
" \"type\": \"double\"\n" +
" },\n" +
" \"created_at\":{\n" +
" \"type\": \"date\"\n" +
" },\n" +
" \"description\":{\n" +
" \"type\": \"text\",\n" +
" \"analyzer\": \"ik_max_word\"\n" +
" }\n" +
" }\n" +
" }", XContentType.JSON); //參數1:指定映射json結構 參數2:指定數據結構
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
System.out.println("創建狀態:" + createIndexResponse.isAcknowledged());
restHighLevelClient.close();//關閉資源
}
//刪除索引
@Test
public void testDeleteIndex() throws IOException {
//參數1:刪除索引對象 參數2:請求配置對象
AcknowledgedResponse acknowledgedResponse = restHighLevelClient.indices().delete(new DeleteIndexRequest("products"), RequestOptions.DEFAULT);
System.out.println(acknowledgedResponse.isAcknowledged());
}
}