فهرست منبع

调整测试ES聚合的用例;修改视图聚合空指针异常。

zhangjinjun 6 سال پیش
والد
کامیت
e532afc5be

+ 4 - 3
src/main/java/com/yihu/quota/service/view/ViewService.java

@ -425,15 +425,16 @@ public class ViewService extends BaseJpaService<View, ViewDao> {
        for (ViewQuota quota : viewQuotaList) {
            if ("basic".equals(quota.getFormulaMode())) {
                String aggNamePre = "";
                String dimensionCode = viewDimension.getDimensionCode();
                if (viewDimension == null) {
                    // 视图展示为数值类型的场合
                    aggNamePre += quota.getCode() + "-";
                } else if (ViewConstant.TOTAL_DIMENSION_CODE.equals(dimensionCode)) {
                } else if (ViewConstant.TOTAL_DIMENSION_CODE.equals(viewDimension.getDimensionCode())) {
                    // 合计维度场合
                    aggNamePre = ViewConstant.TOTAL_DIMENSION_CODE + "-";
                }
                // TODO 数值类型场合,又是多维度数据集时,需要指定数据集
                if ("sum".equals(quota.getBasicFormulaType())) {
                    // 求和
                    String aggName = aggNamePre + "sum-result";
@ -485,7 +486,7 @@ public class ViewService extends BaseJpaService<View, ViewDao> {
            List<ViewQuotaFilterModel> quotaFilterModelList = objectMapper.readValue(quotaFilterStr, new TypeReference<List<ViewQuotaFilterModel>>() {
            });
            BoolQueryBuilder quotaBoolQueryBuilder = QueryBuilders.boolQuery();
            joinFilters(quotaBoolQueryBuilder, quotaFilterModelList);
            this.joinFilters(quotaBoolQueryBuilder, quotaFilterModelList);
            String aggName = quota.getDimensionCode() + "-filter";
            filterAgg = AggregationBuilders.filter(aggName).filter(quotaBoolQueryBuilder);

+ 14 - 3
src/test/java/com/yihu/quota/service/EsAggsTest.java

@ -7,9 +7,13 @@ import org.apache.solr.client.solrj.response.Group;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.filter.Filter;
import org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
@ -59,18 +63,25 @@ public class EsAggsTest {
    public void termsCardinalityTest() {
        TransportClient transportClient = elasticSearchPool.getClient();
        QueryBuilder queryBuilder = QueryBuilders.termQuery("patient_id", "848738");
        FilterAggregationBuilder filterAgg = AggregationBuilders.filter("patientFilter").filter(queryBuilder);
        TermsBuilder termsBuilder = AggregationBuilders.terms("datasetTerms").field("dataset").size(200);
        CardinalityBuilder cardinalityBuilder = AggregationBuilders.cardinality("distinctCount")
                .field("event_no").precisionThreshold(40000);
        termsBuilder.subAggregation(cardinalityBuilder);
        filterAgg.subAggregation(termsBuilder);
        SearchRequestBuilder searchRequestBuilder = transportClient.prepareSearch("json_archives_qc")
                .setTypes("qc_metadata_info")
                .setSize(0)
                .addAggregation(termsBuilder);
                .addAggregation(filterAgg);
        SearchResponse searchResponse = searchRequestBuilder.get();
        StringTerms terms = searchResponse.getAggregations().get("datasetTerms");
        Filter filter = searchResponse.getAggregations().get("patientFilter");
        StringTerms terms = filter.getAggregations().get("datasetTerms");
        for (Terms.Bucket bucket : terms.getBuckets()) {
            String key = bucket.getKeyAsString();
            Cardinality cardinality = bucket.getAggregations().get("distinctCount");