반응형
Recent Posts
Recent Comments
관리 메뉴

개발잡부

[es] aggregation test 1 본문

ElasticStack/Elasticsearch

[es] aggregation test 1

닉의네임 2022. 7. 16. 18:36
반응형

작업


상품데이터 필터조건 중 float, array, string 데이터의 집계를 하여 필터의 활성화 비활성화를 확인한다.  

 

index mapping 구조

 

PUT aggs_doo
{
  "settings": {
    "index": {
      "number_of_shards": 3,
      "number_of_replicas": 0
    }
  },
  "mappings": {
    "properties": {
      "benefit": {
        "type": "keyword"
      },
      "grade": {
        "type": "float"
      },
      "mallType": {
        "type": "keyword"
      }
    }
  }
}

데이터 색인 

PUT aggs_doo/_doc/1
{
  "benefit":["AAA", "BBB"],
  "grade": 2.2,
  "mallType": "DS"
}

PUT aggs_doo/_doc/2
{
  "benefit":["AAA", "CCC"],
  "grade": 1.1,
  "mallType": "DS"
}

PUT aggs_doo/_doc/3
{
  "benefit":["DDD", "EEE"],
  "grade": 4.1,
  "mallType": "DS"
}

PUT aggs_doo/_doc/4
{
  "benefit":["AAA", "FFF"],
  "grade": 3.5,
  "mallType": "DS"
}

PUT aggs_doo/_doc/5
{
  "benefit":["BBB", "CCC", "ZZZ"],
  "grade": 3.9,
  "mallType": "DS"
}

QUERY DSL

{
   "size":0,
   "query":{
      "bool":{
         "must":[
            {
               "match_all":{
                  "boost":1.0
               }
            }
         ],
         "adjust_pure_negative":true,
         "boost":1.0
      }
   },
   "aggregations":{
      "MALL_TYPE":{
         "terms":{
            "field":"mallType",
            "size":10,
            "min_doc_count":1,
            "shard_min_doc_count":0,
            "show_term_doc_count_error":false,
            "order":[
               {
                  "_count":"desc"
               },
               {
                  "_key":"asc"
               }
            ]
         }
      },
      "BENEFIT":{
         "terms":{
            "field":"benefit",
            "size":10,
            "min_doc_count":1,
            "shard_min_doc_count":0,
            "show_term_doc_count_error":false,
            "order":[
               {
                  "_count":"desc"
               },
               {
                  "_key":"asc"
               }
            ]
         }
      },
      "GRADE":{
         "range":{
            "field":"grade",
            "ranges":[
               {
                  "to":1.0
               },
               {
                  "from":1.0,
                  "to":2.0
               },
               {
                  "from":2.0,
                  "to":3.0
               },
               {
                  "from":3.0,
                  "to":4.0
               },
               {
                  "from":4.0,
                  "to":5.0
               },
               {
                  "from":5.0
               }
            ],
            "keyed":false
         }
      }
   }
}

 

TEST CODE - java

package com.bbongdoo.doo.service;

import com.bbongdoo.doo.model.response.CommonResult;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.range.Range;
import org.elasticsearch.search.aggregations.bucket.range.RangeAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class AggsService {

    private final ResponseService responseService;
    private final RestHighLevelClient client;
    private final String ALIAS = "aggs_doo";

    public CommonResult getProducts() {

        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices(ALIAS);
        try {
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

            QueryBuilder queryBuilder = QueryBuilders.boolQuery().must(
                    QueryBuilders.matchAllQuery()
            );

            searchSourceBuilder.query(queryBuilder);
            searchSourceBuilder.size(0);

            TermsAggregationBuilder mallType = new TermsAggregationBuilder("MALL_TYPE", null).field("mallType");
            searchSourceBuilder.aggregation(mallType);
            TermsAggregationBuilder benefit = new TermsAggregationBuilder("BENEFIT", null).field("benefit");
            searchSourceBuilder.aggregation(benefit);
            RangeAggregationBuilder grade = new RangeAggregationBuilder("GRADE").field("grade")
                    .addUnboundedTo(1)
                    .addRange(1, 2)
                    .addRange(2, 3)
                    .addRange(3, 4)
                    .addRange(4, 5)
                    .addUnboundedFrom(5);
            searchSourceBuilder.aggregation(grade);
            searchRequest.source(searchSourceBuilder);
            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            RestStatus status = searchResponse.status();

            List<ReturnAggs> returnAggs = new ArrayList<>();
            if (status == RestStatus.OK) {
                Aggregations aggregations = searchResponse.getAggregations();

                Terms benefitAggs = aggregations.get("BENEFIT");
                for (Terms.Bucket bucketBenefit : benefitAggs.getBuckets()) {
                    returnAggs.add(new ReturnAggs(bucketBenefit.getKey().toString(), bucketBenefit.getDocCount()));
                }

                Terms mallTypeAggs = aggregations.get("MALL_TYPE");
                for (Terms.Bucket bucketMallType : mallTypeAggs.getBuckets()) {
                    returnAggs.add(new ReturnAggs(bucketMallType.getKey().toString(), bucketMallType.getDocCount()));
                }

                Range gradeAggs = aggregations.get("GRADE");
                for (Range.Bucket bucketGrade : gradeAggs.getBuckets()) {
                    returnAggs.add(new ReturnAggs(bucketGrade.getKey().toString(), bucketGrade.getDocCount()));
                }
            }

            return responseService.getListResult(returnAggs);

        } catch (IOException e) {
            e.printStackTrace();
        }

        return new CommonResult();
    }

}

@Getter
@Setter
@RequiredArgsConstructor
class ReturnAggs {
    private final String key;
    private final long count;
}
반응형

'ElasticStack > Elasticsearch' 카테고리의 다른 글

[es] aggregation test 2  (0) 2022.07.17
[es] nested aggregation  (0) 2022.07.16
[es] Array Aggregations  (0) 2022.07.15
[es] Aggregations  (0) 2022.07.14
[es] bucket aggregations  (0) 2022.07.14
Comments