|
@ -0,0 +1,79 @@
|
|
|
package com.yihu.quota.service;
|
|
|
|
|
|
import com.yihu.ehr.elasticsearch.ElasticSearchPool;
|
|
|
import com.yihu.ehr.solr.SolrUtil;
|
|
|
import com.yihu.quota.SvrQuotaApplication;
|
|
|
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.search.aggregations.Aggregation;
|
|
|
import org.elasticsearch.search.aggregations.AggregationBuilder;
|
|
|
import org.elasticsearch.search.aggregations.AggregationBuilders;
|
|
|
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
|
|
|
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
|
|
import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
|
|
|
import org.elasticsearch.search.aggregations.metrics.cardinality.Cardinality;
|
|
|
import org.elasticsearch.search.aggregations.metrics.cardinality.CardinalityBuilder;
|
|
|
import org.junit.Test;
|
|
|
import org.junit.runner.RunWith;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* @author 张进军
|
|
|
* @date 2018/9/13 17:19
|
|
|
*/
|
|
|
@RunWith(SpringRunner.class)
|
|
|
@SpringBootTest(classes = SvrQuotaApplication.class)
|
|
|
public class EsAggsTest {
|
|
|
|
|
|
@Autowired
|
|
|
ElasticSearchPool elasticSearchPool;
|
|
|
|
|
|
@Test
|
|
|
public void cardinalityTest() {
|
|
|
TransportClient transportClient = elasticSearchPool.getClient();
|
|
|
|
|
|
CardinalityBuilder cardinalityBuilder = AggregationBuilders.cardinality("orgAreaDistinctCount")
|
|
|
.field("org_area").precisionThreshold(40000);
|
|
|
|
|
|
SearchRequestBuilder searchRequestBuilder = transportClient.prepareSearch("test1")
|
|
|
.setTypes("hzy")
|
|
|
.setSize(0)
|
|
|
.addAggregation(cardinalityBuilder);
|
|
|
|
|
|
SearchResponse searchResponse = searchRequestBuilder.get();
|
|
|
Cardinality cardinality = searchResponse.getAggregations().get("orgAreaDistinctCount");
|
|
|
long distinctCount = cardinality.getValue();
|
|
|
|
|
|
System.out.println(distinctCount);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void termsCardinalityTest() {
|
|
|
TransportClient transportClient = elasticSearchPool.getClient();
|
|
|
|
|
|
TermsBuilder termsBuilder = AggregationBuilders.terms("datasetTerms").field("dataset").size(200);
|
|
|
CardinalityBuilder cardinalityBuilder = AggregationBuilders.cardinality("distinctCount")
|
|
|
.field("event_no").precisionThreshold(40000);
|
|
|
termsBuilder.subAggregation(cardinalityBuilder);
|
|
|
|
|
|
SearchRequestBuilder searchRequestBuilder = transportClient.prepareSearch("json_archives_qc")
|
|
|
.setTypes("qc_metadata_info")
|
|
|
.setSize(0)
|
|
|
.addAggregation(termsBuilder);
|
|
|
|
|
|
SearchResponse searchResponse = searchRequestBuilder.get();
|
|
|
StringTerms terms = searchResponse.getAggregations().get("datasetTerms");
|
|
|
for (Terms.Bucket bucket : terms.getBuckets()) {
|
|
|
String key = bucket.getKeyAsString();
|
|
|
Cardinality cardinality = bucket.getAggregations().get("distinctCount");
|
|
|
System.out.println(key + " : " + cardinality.getValue());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|