一. kibanan中用id: get 路径/id
1 @Test 2 public void getByIdTesg() throws IOException { 3 // 1. 创建查询对象 4 String index = "sms-logs-index"; 5 String type = "sms-logs-type"; 6 SearchRequest searchRequest = new SearchRequest(index);//指定索引 7 searchRequest.types(type);//指定类型 8 // 2. 封装查询条件 9 GetRequest getRequest = new GetRequest(index, type, "1"); 10 11 // 3.执行查询 12 // client执行 13 HttpHost httpHost = new HttpHost("192.168.43.30", 9200); 14 RestClientBuilder restClientBuilder = RestClient.builder(httpHost); 15 RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder); 16 GetResponse documentFields = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT); 17 // 4.获取数据(source中的数据) 18 System.out.println(documentFields); 19 20 }
二. ids查询, in多个id;
@Test public void getByIdsTesg() throws IOException { // 1. 创建查询对象 String index = "sms-logs-index"; String type = "sms-logs-type"; SearchRequest searchRequest = new SearchRequest(index);//指定索引 searchRequest.types(type);//指定类型 // 2. 封装查询条件 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.idsQuery().addIds("1","2","3")); searchRequest.source(searchSourceBuilder); // 3.执行查询 // client执行 HttpHost httpHost = new HttpHost("192.168.43.30", 9200); RestClientBuilder restClientBuilder = RestClient.builder(httpHost); RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder); SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); // 4.获取数据(source中的数据) SearchHit[] hits = search.getHits().getHits(); for(SearchHit searchHit : hits){ System.out.println(searchHit); } }