Java呼び出しElasticSearch 7.2.1保存、統計、マルチフィールドパケット集約


目次
 
mavenリファレンス
コンフィギュレーション
クラスの構成
データの保存方法
公式の引用方式を参照すると誤報になる
グループ統計
再統計
マルチフィールドグループ集約
 
mavenリファレンス
注意バージョンはesバージョンと一致しています

    org.elasticsearch.client
    elasticsearch-rest-high-level-client
    7.2.1
    
        
            org.elasticsearch
            elasticsearch
        
    




    org.elasticsearch
    elasticsearch
    7.2.1

コンフィギュレーション
spring:
  data:
    elasticsearch:
      ip: 10.0.197.198
      port: 9200
      scheme: http

クラスの構成
import lombok.Data;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "spring.data.elasticsearch")
@Data
public class ElasticsearchConfig {

    private String ip;
    private Integer port;
    private String scheme;
    public RestHighLevelClient getClient() {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost(ip, port, scheme)));

        return client;
    }
}

データの保存方法
public Boolean setPageAccess(Map map) {
    try(RestHighLevelClient client= elasticsearchConfig.getClient()) {
        // 1、      
        IndexRequest indexRequest = new IndexRequest(
                "test");     //  id
        indexRequest.id(StringUtil.getUUID());
        map.put("time",new Date());

        // 2、      
        //    :   JSON 
        String jsonString = JSON.toJSONString(map);
        indexRequest.source(jsonString, XContentType.JSON);
        //4、    
        IndexResponse indexResponse = null;
        try {
            //     
            indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
        } catch (ElasticsearchException e) {
            //   ,     
            //        、create        
            if (e.status() == RestStatus.CONFLICT) {
                log.error("   
" + e.getDetailedMessage()); } log.error(" ", e); return false; } }catch (Exception e){ e.printStackTrace(); return false; } return true; }

公式の引用方式を参照すると誤報になる
java.lang.NoClassDefFoundError: org/elasticsearch/common/xcontent/DeprecationHandler
解決策:古いesリファレンスを除外する

    org.elasticsearch.client
    elasticsearch-rest-high-level-client
    7.2.1
    
        
            org.elasticsearch
            elasticsearch
        
    




    org.elasticsearch
    elasticsearch
    7.2.1

グループ統計
注意:グループが文字列フィールドにある場合は、フィールド対応を確立する必要がある.keywordフィールドです.このフィールドは集約処理をサポートし、文字列フィールドで直接エラーを報告します.
try(RestHighLevelClient client= elasticsearchConfig.getClient()) {

    // 1、  search  
    //SearchRequest searchRequest = new SearchRequest();
    SearchRequest searchRequest = new SearchRequest(INDEX_PAGEACCESS);

    // 2、 SearchSourceBuilder         ,         ,            。
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

    //sourceBuilder.query();
    sourceBuilder.size(0);

    //    
    //        
    TermsAggregationBuilder aggregation = AggregationBuilders.terms("by_method")
            .field("method.keyword").order(BucketOrder.aggregation("count", true));
    //       balance  
    aggregation.subAggregation(AggregationBuilders.count("count")
            .field("_sessionid"));
    sourceBuilder.aggregation(aggregation);

    searchRequest.source(sourceBuilder);

    //3、    
    SearchResponse searchResponse = client.search(searchRequest,RequestOptions.DEFAULT);

    //4、    
    //        
    if(RestStatus.OK.equals(searchResponse.status())) {
        //       
        Aggregations aggregations = searchResponse.getAggregations();
        Terms byAgeAggregation = aggregations.get("by_method");
        log.info("aggregation by_age   ");
        log.info("docCountError: " + byAgeAggregation.getDocCountError());
        log.info("sumOfOtherDocCounts: " + byAgeAggregation.getSumOfOtherDocCounts());
        log.info("------------------------------------");
        for(Terms.Bucket buck : byAgeAggregation.getBuckets()) {
            log.info("key: " + buck.getKeyAsString());
            log.info("docCount: " + buck.getDocCount());
            log.info("docCountError: " + buck.getDocCountError());
            //    
            ParsedValueCount averageBalance = buck.getAggregations().get("count");

            log.info("average_balance: " + averageBalance.getValue());
            log.info("------------------------------------");
        }
        //   key     
        /*Bucket elasticBucket = byCompanyAggregation.getBucketByKey("24");
        Avg averageAge = elasticBucket.getAggregations().get("average_age");
        double avg = averageAge.getValue();*/

    }
    return new ArrayList();
}catch (Exception e){
    e.printStackTrace();
    return null;
}

再統計
try(RestHighLevelClient client= elasticsearchConfig.getClient()) {

    // 1、  search  
    //SearchRequest searchRequest = new SearchRequest();
    SearchRequest searchRequest = new SearchRequest(INDEX_PAGEACCESS);

    // 2、 SearchSourceBuilder         ,         ,            。
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

    //  QueryBuilder
    QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("method.keyword", "web_event_srv.upload");
    sourceBuilder.query(matchQueryBuilder);
    sourceBuilder.size(0);

    //    
    //        
    TermsAggregationBuilder aggregation = AggregationBuilders.terms("by_fngroup")
            .field("fngroup.keyword").order(BucketOrder.aggregation("count", true));
    //       balance  
    aggregation.subAggregation(AggregationBuilders.count("count")
            .field("sessionid"));
    sourceBuilder.aggregation(aggregation);

    searchRequest.source(sourceBuilder);

    //3、    
    SearchResponse searchResponse = client.search(searchRequest,RequestOptions.DEFAULT);

    //4、    
    //        
    List result=new ArrayList<>();
    if(RestStatus.OK.equals(searchResponse.status())) {
        //       
        Aggregations aggregations = searchResponse.getAggregations();
        Terms byAgeAggregation = aggregations.get("by_fngroup");
        for(Terms.Bucket buck : byAgeAggregation.getBuckets()) {
            Map map=new HashMap();
            map.put("name", buck.getKeyAsString());
            //    
            ParsedValueCount averageBalance = buck.getAggregations().get("count");

            map.put("count",averageBalance.getValue());
            result.add(map);
        }
    }
    return result;
}catch (Exception e){
    e.printStackTrace();
    return null;
}

 
マルチフィールドグループ集約
fngroupとuser_を押しますidパケット集約
public List selectModuleCount(Map param) {
    try(RestHighLevelClient client= elasticsearchConfig.getClient()) {

        // 1、  search  
        //SearchRequest searchRequest = new SearchRequest();
        SearchRequest searchRequest = new SearchRequest(INDEX_PAGEACCESS);

        // 2、 SearchSourceBuilder         ,         ,            。
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.size(0);

        //    
        //        
        TermsAggregationBuilder aggregation = AggregationBuilders.terms("by_fngroup")
                .script(new Script("doc['fngroup.keyword'] +'#'+doc['user_id']"))
                //.field("fngroup.keyword")
                .size(Integer.MAX_VALUE)
                .order(BucketOrder.aggregation("count", true));
        //       balance  
        aggregation.subAggregation(AggregationBuilders.count("count")
                .field("sessionid"));
        sourceBuilder.aggregation(aggregation);

        searchRequest.source(sourceBuilder);

        //3、    
        SearchResponse searchResponse = client.search(searchRequest,RequestOptions.DEFAULT);

        //4、    
        //        
        List result=new ArrayList<>();
        if(RestStatus.OK.equals(searchResponse.status())) {
            //       
            Aggregations aggregations = searchResponse.getAggregations();
            Terms byAgeAggregation = aggregations.get("by_fngroup");
            for(Terms.Bucket buck : byAgeAggregation.getBuckets()) {
                Map map=new HashMap();
                String[] arr= buck.getKeyAsString().split("#");
                map.put("module",arr[0].replace("[","").replace("]",""));
                map.put("user_id",arr[1].replace("[","").replace("]",""));
                //    
                ParsedValueCount averageBalance = buck.getAggregations().get("count");

                map.put("count",averageBalance.getValue());
                result.add(map);
            }
        }
        return result;
    }catch (Exception e){
        e.printStackTrace();
        return null;
    }

}