Elasticsearch常用查询

Elasticsearch常用查询

注意:Elasticsearch版本6.4

根据条件查询数据

SearchRequest searchRequest = new SearchRequest("索引");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//按照create_time字段降序排序
searchSourceBuilder.query(QueryBuilders.termQuery("video_id", videoId)).from(startIndex).size(pageSize).sort("create_time", SortOrder.DESC);
searchRequest.types("类型").source(searchSourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchResponse.getHits();
long totalHits = hits.getTotalHits();
if (totalHits == 0) {
  System.out.println("No Data Found!");
}
for (SearchHit hit : hits) {
  String sourceAsString = hit.getSourceAsString();
}

根据条件查询数据,并过滤返回字段

SearchRequest searchRequest = new SearchRequest("索引");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//按照create_time字段降序排序
searchSourceBuilder.query(QueryBuilders.termQuery("user_id", userId)).from(startIndex).size(pageSize).sort("create_time", SortOrder.DESC);
String[] includes = new String[]{"user_id", "video_id", "create_time"};
//FetchSourceContext context = new FetchSourceContext(true,includes,null);
//searchSourceBuilder.fetchSource(context);
searchSourceBuilder.fetchSource(includes,null);
searchRequest.types(TYPE).source(searchSourceBuilder);

SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

SearchHits hits = searchResponse.getHits();
long totalHits = hits.getTotalHits();
if (totalHits == 0) {
    System.out.println("No Data Found!");
}
for (SearchHit hit : hits) {
    String sourceAsString = hit.getSourceAsString();
}

有两种方法可以过滤返回字段:

第一种:

#要返回的字段
String[] includes = new String[]{"user_id", "video_id", "create_time"};
searchSourceBuilder.fetchSource(includes,null);

第二种:

#要返回的字段
String[] includes = new String[]{"user_id", "video_id", "create_time"};
FetchSourceContext context = new FetchSourceContext(true,includes,null);
searchSourceBuilder.fetchSource(context);

无条件批量查询数据

MultiGetRequest multiGetRequest = new MultiGetRequest();
int count = userIdsList.size();
for (int i = 0; i < count; i++) {
    multiGetRequest.add(new MultiGetRequest.Item("索引", "类型", userIdsList.get(i)));
}
MultiGetResponse multiGetResponse = restHighLevelClient.mget(multiGetRequest, RequestOptions.DEFAULT);
MultiGetItemResponse[] responses = multiGetResponse.getResponses();
for (MultiGetItemResponse res : responses) {
    if (res.getFailure() != null) {
        ElasticsearchException e = (ElasticsearchException) res.getFailure().getFailure();
        //if (e.getMessage().contains("reason=no such index")) {
        //    log.error("查询的文档库不存在: ", e);
        //}
    log.error("查询异常:", e);
    continue;
   }
   GetResponse item = res.getResponse();
   if (item.isExists()) {
            String sourceAsString = item.getSourceAsString();
   }
}


有条件批量查询

MultiSearchRequest multiSearchRequest = new MultiSearchRequest();
int count = videoIdsList.size();
for (int i = 0; i < count; i++) {
    //构造查询条件
    SearchRequest searchRequest = new SearchRequest("索引");
    searchRequest.types("类型");
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchRequest.source(searchSourceBuilder);
    searchSourceBuilder.query(QueryBuilders.termQuery("video_id", videoIdsList.get(i)));
    multiSearchRequest.add(searchRequest);
}
//批量查询返回
MultiSearchResponse searchResponse = restHighLevelClient.msearch(multiSearchRequest, RequestOptions.DEFAULT);
MultiSearchResponse.Item[] responses = searchResponse.getResponses();
//处理返回结果
for (MultiSearchResponse.Item respon : responses) {
    if (respon.getFailure() != null) {
        log.info("查询异常Error:{}", respon.getFailureMessage());
    } else {
        SearchResponse res = respon.getResponse();
        SearchHits hits = res.getHits();
        for (SearchHit hit : hits) {
            String sourceAsString = hit.getSourceAsString();
        }
    }
}



除特别注明外,本站所有文章均为东哥技术分享原创,转载请注明出处来自http://quliming.com/post/95.html

评论回复