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

TransportClient test

上级 3591a2c2
...@@ -3,6 +3,8 @@ package org.hongxi.whatsmars.elasticsearch; ...@@ -3,6 +3,8 @@ package org.hongxi.whatsmars.elasticsearch;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document; 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") @Document(indexName = "customer", type = "customer", shards = 1, replicas = 0, refreshInterval = "-1")
public class Customer { public class Customer {
...@@ -12,7 +14,7 @@ public class Customer { ...@@ -12,7 +14,7 @@ public class Customer {
private String id; private String id;
private String firstName; private String firstName;
@Field(type = FieldType.Keyword)
private String lastName; private String lastName;
public Customer() { public Customer() {
......
...@@ -15,6 +15,10 @@ import org.elasticsearch.common.transport.TransportAddress; ...@@ -15,6 +15,10 @@ import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders; 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.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
...@@ -24,7 +28,9 @@ import java.io.IOException; ...@@ -24,7 +28,9 @@ import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
/** /**
* Created by shenhongxi on 2018/11/19. * Created by shenhongxi on 2018/11/19.
...@@ -107,6 +113,22 @@ public class TransportClientTest { ...@@ -107,6 +113,22 @@ public class TransportClientTest {
assert response.getHits().getTotalHits() > 0; 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 @After
public void close() { public void close() {
client.close(); client.close();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册