提交 4b7abb07 编写于 作者: 武汉红喜's avatar 武汉红喜

TransportClient test

上级 3591a2c2
......@@ -3,6 +3,8 @@ package org.hongxi.whatsmars.elasticsearch;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
@Document(indexName = "customer", type = "customer", shards = 1, replicas = 0, refreshInterval = "-1")
public class Customer {
......@@ -12,7 +14,7 @@ public class Customer {
private String id;
private String firstName;
@Field(type = FieldType.Keyword)
private String lastName;
public Customer() {
......
......@@ -15,6 +15,10 @@ import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.After;
import org.junit.Before;
......@@ -24,7 +28,9 @@ import java.io.IOException;
import java.net.InetAddress;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Created by shenhongxi on 2018/11/19.
......@@ -107,6 +113,22 @@ public class TransportClientTest {
assert response.getHits().getTotalHits() > 0;
}
// 利用groupBy获取distinct值
@Test
public void distinct() {
TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("aggs").field("lastName");
SearchResponse response = client.prepareSearch("customer")
.addAggregation(termsAggregationBuilder)
.get();
Aggregation aggregation = response.getAggregations().asMap().get("aggs");
StringTerms stringTerms = (StringTerms) aggregation;
List<String> lastNames = stringTerms.getBuckets()
.stream()
.map(StringTerms.Bucket::getKeyAsString)
.collect(Collectors.toList());
System.out.println(lastNames);
}
@After
public void close() {
client.close();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册