JavaRest 高级客户端入门(elasticsearch整合java)

一. JavaRest 高级客户端简介

elasticsearch 存在三种Java客户端。

  1. Transport Client
  2. Java Low Level Rest Client(低级rest客户端)
  3. Java High Level REST Client(高级rest客户端)

这三者的区别是:

  • TransportClient没有使用RESTful风格的接口,而是二进制的方式传输数据。
  • ES官方推出了Java Low Level REST Client,它支持RESTful。但是缺点是因为
    TransportClient的使用者把代码迁移到Low Level REST Client的工作量比较大。
  • ES官方推出Java High Level REST Client,它是基于Java Low Level REST Client的封
    装,并且API接收参数和返回值和TransportClient是一样的,使得代码迁移变得容易

并且支持了RESTful的风格,兼容了这两种客户端的优点。强烈建议ES5及其以后的 版本使用Java High Level REST
Client。


准备工作:新建工程,引入依赖

 <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>6.5.3</version>
 </dependency>

二.快速入门

1.新增和修改数据
插入单条数据:

  • HttpHost : url地址封装
  • RestClientBuilder: rest客户端构建器 RestHighLevelClient: rest高级客户端
  • IndexRequest: 新增或修改请求
  • IndexResponse:新增或修改的响应结果
//1.连接rest接口
    HttpHost httpHost=new HttpHost("127.0.0.1",9200,"http");
    RestClientBuilder restClientBuilder= RestClient.builder(httpHost);
    RestHighLevelClient restHighLevelClient=new RestHighLevelClient(restClientBuilder);

    //2.封装请求对象
    IndexRequest indexRequest =new IndexRequest("sku","doc","3");//如果ID不存在则新增,如果ID存在则修改。

     Map skuMap=new HashMap();
     skuMap.put("name","华为p30pro");
     skuMap.put("brandName","华为");
     skuMap.put("categoryName","手机");
     skuMap.put("price",1010221);
     skuMap.put("createTime","2019-05-01");
     skuMap.put("saleNum",101021); skuMap.put("commentNum",10102321);
     Map spec=new HashMap();
     spec.put("网络制式","移动4G");
     spec.put("屏幕尺寸","5");
     skuMap.put("spec",spec);
     indexRequest.source(skuMap);

    //3.获取响应结果
    IndexResponse response = restHighLevelClient.index(indexRequest,RequestOptions.DEFAULT);
     int status = response.status().getStatus();
     System.out.println(status);
     restHighLevelClient.close();//关闭连接

批处理请求(插入删除多条数据):
BulkRequest: 批量请求(用于增删改操作)
BulkResponse:批量请求(用于增删改操作)
注:可用于查询数据库然后批量插入

//1.连接rest接口
    HttpHost httpHost=new HttpHost("127.0.0.1",9200,"http");
    RestClientBuilder restClientBuilder= RestClient.builder(httpHost);
    RestHighLevelClient restHighLevelClient=new RestHighLevelClient(restClientBuilder);

    //2.封装请求对象
    BulkRequest bulkRequest=new BulkRequest();
    IndexRequest indexRequest =new IndexRequest("sku","doc","5");
     Map skuMap=new HashMap();
     skuMap.put("name","华为Mate20pro");
     skuMap.put("brandName","华为");
     skuMap.put("categoryName","手机");
     skuMap.put("price",1010221);
     skuMap.put("createTime","2019-05-01");
     skuMap.put("saleNum",101021); skuMap.put("commentNum",10102321);
     Map spec=new HashMap();
     spec.put("网络制式","移动4G");
     spec.put("屏幕尺寸","5");
     skuMap.put("spec",spec);
     indexRequest.source(skuMap);

     bulkRequest.add(indexRequest);

    //3.获取响应结果
   // IndexResponse response = restHighLevelClient.index(indexRequest,RequestOptions.DEFAULT);
     BulkResponse bulkItemResponses= restHighLevelClient.bulk(bulkRequest,RequestOptions.DEFAULT);
     int status = bulkItemResponses.status().getStatus();
     System.out.println(status);
     restHighLevelClient.close();

2.匹配查询
SearchRequest: 查询请求对象
SearchResponse:查询响应对象
SearchSourceBuilder:查询源构建器
MatchQueryBuilder:匹配查询构建器

示例:查询商品名称包含手机的记录。

 //1.连接rest接口
    HttpHost httpHost=new HttpHost("127.0.0.1",9200,"http");
    RestClientBuilder restClientBuilder= RestClient.builder(httpHost);
    RestHighLevelClient restHighLevelClient=new RestHighLevelClient(restClientBuilder);

    //2.封装查询请求
    SearchRequest searchRequest=new SearchRequest("sku");//相当于kibana的 GET sku/语句
    searchRequest.types("doc");//指定查询类型,不写的话查询所有类型  相当于GET xxx/doc

    SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();//相当于kibana的 "query"
    MatchQueryBuilder matchQueryBuilder= QueryBuilders.matchQuery("name","手机");//相当于"match": {"name":"手机"}
    searchSourceBuilder.query(matchQueryBuilder);

    searchRequest.source(searchSourceBuilder);

    //3.获取查询结果
   SearchResponse searchResponse= restHighLevelClient.search(searchRequest,RequestOptions.DEFAULT);//相当于_search语句
    SearchHits searchHits = searchResponse.getHits();//返回的hits有两层
    int totalShards = searchResponse.getTotalShards();
    System.out.println("totalShards记录数:"+totalShards);
    SearchHit[] hits = searchHits.getHits();
    for (SearchHit hit:hits) {
        String sourceAsString = hit.getSourceAsString();//得到一个Json字符串
         //hit.getSourceAsMap();//得到一个Map
        System.out.println(sourceAsString);
    }

    restHighLevelClient.close();//关闭连接

3.布尔与词条查询
BoolQueryBuilder:布尔查询构建器
TermQueryBuilder:词条查询构建器
QueryBuilders:查询构建器工

示例:查询名称包含手机的,并且品牌为小米的记录

    //1.连接rest接口
    HttpHost httpHost=new HttpHost("127.0.0.1",9200,"http");
    RestClientBuilder restClientBuilder= RestClient.builder(httpHost);
    RestHighLevelClient restHighLevelClient=new RestHighLevelClient(restClientBuilder);

    //2.封装查询请求
    SearchRequest searchRequest=new SearchRequest("sku");//相当于kibana的 GET sku/语句
    searchRequest.types("doc");//指定查询类型,不写的话查询所有类型  相当于GET xxx/doc

    SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();//相当于kibana的 "query"
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();//构建布尔查询构建器,相当于 "bool": {}

    MatchQueryBuilder matchQueryBuilder= QueryBuilders.matchQuery("name","手机");//相当于"match": {"name":"手机"}
    boolQueryBuilder.must(matchQueryBuilder);

    TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("brandName", "小米");
    boolQueryBuilder.must(termQueryBuilder);

    searchSourceBuilder.query(boolQueryBuilder);

    searchRequest.source(searchSourceBuilder);

    //3.获取查询结果
    SearchResponse searchResponse= restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);//相当于_search语句
    SearchHits searchHits = searchResponse.getHits();//返回的hits有两层
    int totalShards = searchResponse.getTotalShards();
    System.out.println("totalShards记录数:"+totalShards);
    SearchHit[] hits = searchHits.getHits();
    for (SearchHit hit:hits) {
        String sourceAsString = hit.getSourceAsString();//得到一个Json字符串
        //hit.getSourceAsMap();//得到一个Map
        System.out.println(sourceAsString);
    }

    restHighLevelClient.close();//关闭连接

4.过滤查询(即不需要评分)

//1.连接rest接口
    HttpHost httpHost=new HttpHost("127.0.0.1",9200,"http");
    RestClientBuilder restClientBuilder= RestClient.builder(httpHost);
    RestHighLevelClient restHighLevelClient=new RestHighLevelClient(restClientBuilder);

    //2.封装查询请求
    SearchRequest searchRequest=new SearchRequest("sku");//相当于kibana的 GET sku/语句
    searchRequest.types("doc");//指定查询类型,不写的话查询所有类型  相当于GET xxx/doc

    SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();//相当于kibana的 "query"
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();//构建布尔查询构建器,相当于 "bool": {}

    TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("brandName", "小米");
    boolQueryBuilder.filter(termQueryBuilder);

    searchSourceBuilder.query(boolQueryBuilder);

    searchRequest.source(searchSourceBuilder);

    //3.获取查询结果
    SearchResponse searchResponse= restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);//相当于_search语句
    SearchHits searchHits = searchResponse.getHits();//返回的hits有两层
    int totalShards = searchResponse.getTotalShards();
    System.out.println("totalShards记录数:"+totalShards);
    SearchHit[] hits = searchHits.getHits();
    for (SearchHit hit:hits) {
        String sourceAsString = hit.getSourceAsString();//得到一个Json字符串
        //hit.getSourceAsMap();//得到一个Map
        System.out.println(sourceAsString);
    }

    restHighLevelClient.close();//关闭连接

5.分组(聚合)查询
AggregationBuilders:聚合构建器工厂
TermsAggregationBuilder:词条聚合构建器
Aggregations:分组结果封装
Terms.Bucket: 桶(具体每一条分组结果)

示例:按商品分类分组查询,求出每个分类的文档数

//1.连接rest接口
    HttpHost httpHost=new HttpHost("127.0.0.1",9200,"http");
    RestClientBuilder restClientBuilder= RestClient.builder(httpHost);
    RestHighLevelClient restHighLevelClient=new RestHighLevelClient(restClientBuilder);

    //2.封装查询请求
    SearchRequest searchRequest=new SearchRequest("sku");//相当于kibana的 GET sku/语句
    searchRequest.types("doc");//指定查询类型,不写的话查询所有类型  相当于GET xxx/doc
    SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();//相当于kibana的 "query"

    TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("sku_category")
            .field("categoryName");//sku_category指定分组结果名称,指定按"categoryName"分组
    searchSourceBuilder.aggregation(termsAggregationBuilder);
    searchSourceBuilder.size(0);

    searchRequest.source(searchSourceBuilder);

    //3.获取查询结果
    SearchResponse searchResponse= restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);//相当于_search语句
    Aggregations aggregations = searchResponse.getAggregations();
    Map<String, Aggregation> aggregationsAsMap = aggregations.getAsMap();

    Terms sku_category = (Terms) aggregationsAsMap.get("sku_category");
    List<? extends Terms.Bucket> buckets = sku_category.getBuckets();
    for (Terms.Bucket bucket:buckets) {
        System.out.println(bucket.getKeyAsString()+":"+bucket.getDocCount());
    }

    restHighLevelClient.close();//关闭连接
Last modification:December 4th, 2019 at 10:22 pm

Leave a Comment