Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
武汉红喜
whatsmars
提交
4b7abb07
W
whatsmars
项目概览
武汉红喜
/
whatsmars
通知
3
Star
0
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
whatsmars
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
4b7abb07
编写于
12月 01, 2019
作者:
武汉红喜
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
TransportClient test
上级
3591a2c2
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
25 addition
and
1 deletion
+25
-1
whatsmars-elasticsearch/src/main/java/org/hongxi/whatsmars/elasticsearch/Customer.java
...ain/java/org/hongxi/whatsmars/elasticsearch/Customer.java
+3
-1
whatsmars-elasticsearch/src/test/java/org/hongxi/whatsmars/elasticsearch/TransportClientTest.java
...g/hongxi/whatsmars/elasticsearch/TransportClientTest.java
+22
-0
未找到文件。
whatsmars-elasticsearch/src/main/java/org/hongxi/whatsmars/elasticsearch/Customer.java
浏览文件 @
4b7abb07
...
...
@@ -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
()
{
...
...
whatsmars-elasticsearch/src/test/java/org/hongxi/whatsmars/elasticsearch/TransportClientTest.java
浏览文件 @
4b7abb07
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录