<dependencies>
<dependency>
<groupId>org.elasticsearch.clientgroupId>
<artifactId>transportartifactId>
<version>6.7.0version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-coreartifactId>
<version>2.9.1version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.testnggroupId>
<artifactId>testngartifactId>
<version>6.14.3version>
<scope>testscope>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.47version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<configuration>
<source>1.8source>
<target>1.8target>
configuration>
plugin>
plugins>
build>
private TransportClient client;
@BeforeEach
public void test1() throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name", "myes").build();
client = new PreBuiltTransportClient(settings).addTransportAddress(new TransportAddress(InetAddress.getByName("node01"),9300)).addTransportAddress(new TransportAddress(InetAddress.getByName("node02"),9300));
}
/**
* 插入json格式的索引数据
*/
@Test
public void createIndex(){
String json = "{" +
"\"user\":\"kimchy\"," +
"\"postDate\":\"2013-01-30\"," +
"\"message\":\"travelying out Elasticsearch\"" +
"}";
IndexResponse indexResponse = client.prepareIndex("myindex1", "article", "1").setSource(json, XContentType.JSON).get();
client.close();
}
@Test
public void index2() throws Exception {
HashMap<String, String> jsonMap = new HashMap<String, String>();
jsonMap.put("name", "zhangsan");
jsonMap.put("sex", "1");
jsonMap.put("age", "18");
jsonMap.put("address", "bj");
IndexResponse indexResponse = client.prepareIndex("myindex1", "article", "2")
.setSource(jsonMap)
.get();
client.close();
}
/**
* 通过XContentBuilder来实现索引的创建
* @throws IOException
*/
@Test
public void index3() throws IOException {
IndexResponse indexResponse = client.prepareIndex("myindex1", "article", "3")
.setSource(new XContentFactory().jsonBuilder()
.startObject()
.field("name", "lisi")
.field("age", "18")
.field("sex", "0")
.field("address", "bj")
.endObject())
.get();
client.close();
}
定义person对象
public class Person implements Serializable{
private Integer id;
private String name ;
private Integer age;
private Integer sex;
private String address;
private String phone;
private String email;
private String say;
public Person() {
}
public Person(Integer id, String name, Integer age, Integer sex, String address, String phone, String email,String say) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.address = address;
this.phone = phone;
this.email = email;
this.say = say;
}
public String getSay() {
return say;
}
public void setSay(String say) {
this.say = say;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
插入索引数据
/**
* 将java对象转换为json格式字符串进行创建索引
*/
@Test
public void objToIndex(){
Person person = new Person();
person.setAge(18);
person.setId(20);
person.setName("张三丰");
person.setAddress("武当山");
person.setEmail("[email protected]");
person.setPhone("18588888888");
person.setSex(1);
String json = JSONObject.toJSONString(person);
System.out.println(json);
client.prepareIndex("myindex1","article","32").setSource(json,XContentType.JSON).get();
client.close();
}
/**
* 批量创建索引
* @throws IOException
*/
@Test
public void index4() throws IOException {
BulkRequestBuilder bulk = client.prepareBulk();
bulk.add(client.prepareIndex("myindex1", "article", "4")
.setSource(new XContentFactory().jsonBuilder()
.startObject()
.field("name", "wangwu")
.field("age", "18")
.field("sex", "0")
.field("address", "bj")
.endObject()));
bulk.add(client.prepareIndex("news", "article", "5")
.setSource(new XContentFactory().jsonBuilder()
.startObject()
.field("name", "zhaoliu")
.field("age", "18")
.field("sex", "0")
.field("address", "bj")
.endObject()));
BulkResponse bulkResponse = bulk.get();
System.out.println(bulkResponse);
client.close();
}
根据系统给数据生成的id来进行更新索引
/**
* 更新索引
* 根据数据id来进行更新索引
*/
@Test
public void updateIndex(){
Person guansheng = new Person(5, "宋江", 88, 0, "水泊梁山", "17666666666", "[email protected]","及时雨宋江");
client.prepareUpdate().setIndex("myindex1").setType("article").setId("8")
.setDoc(JSONObject.toJSONString(guansheng),XContentType.JSON)
.get();
client.close();
}
/**
* 按照id进行删除数据
*/
@Test
public void deleteById(){
DeleteResponse deleteResponse = client.prepareDelete("myindex1", "article", "8").get();
client.close();
}
/**
* 删除索引
* 删除整个索引库
*/
@Test
public void deleteIndex(){
DeleteIndexResponse indexsearch = client.admin().indices().prepareDelete("myindex1").execute().actionGet();
client.close();
}
1、初始化一批数据到索引库中准备查询
/**
* 初始化一批数据到索引库当中去准备做查询使用
* 注意这里初始化的时候,需要给我们的数据设置分词属性
* @throws Exception
*/
@Test
public void createIndexBatch() throws Exception {
Settings settings = Settings
.builder()
.put("cluster.name", "myes") //节点名称, 在es配置的时候设置
//自动发现我们其他的es的服务器
.put("client.transport.sniff", "true")
.build();
//创建客户端
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("node01"), 9300));//以本机作为节点
//创建映射
XContentBuilder mapping = XContentFactory.jsonBuilder()
.startObject()
.startObject("properties")
// .startObject("m_id").field("type","keyword").endObject()
.startObject("id").field("type", "integer").endObject()
.startObject("name").field("type", "text").field("analyzer", "ik_max_word").endObject()
.startObject("age").field("type", "integer").endObject()
.startObject("sex").field("type", "text").field("analyzer", "ik_max_word").endObject()
.startObject("address").field("type", "text").field("analyzer", "ik_max_word").endObject()
.startObject("phone").field("type", "text").endObject()
.startObject("email").field("type", "text").endObject()
.startObject("say").field("type", "text").field("analyzer", "ik_max_word").endObject()
.endObject()
.endObject();
//pois:索引名 cxyword:类型名(可以自己定义)
PutMappingRequest putmap = Requests.putMappingRequest("indexsearch").type("mysearch").source(mapping);
//创建索引
client.admin().indices().prepareCreate("indexsearch").execute().actionGet();
//为索引添加映射
client.admin().indices().putMapping(putmap).actionGet();
BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
Person lujunyi = new Person(2, "玉麒麟卢俊义", 28, 1, "水泊梁山", "17666666666", "[email protected]","hello world今天天气还不错");
Person wuyong = new Person(3, "智多星吴用", 45, 1, "水泊梁山", "17666666666", "[email protected]","行走四方,抱打不平");
Person gongsunsheng = new Person(4, "入云龙公孙胜", 30, 1, "水泊梁山", "17666666666", "[email protected]","走一个");
Person guansheng = new Person(5, "大刀关胜", 42, 1, "水泊梁山", "17666666666", "[email protected]","我的大刀已经饥渴难耐");
Person linchong = new Person(6, "豹子头林冲", 18, 1, "水泊梁山", "17666666666", "[email protected]","梁山好汉");
Person qinming = new Person(7, "霹雳火秦明", 28, 1, "水泊梁山", "17666666666", "[email protected]","不太了解");
Person huyanzhuo = new Person(8, "双鞭呼延灼", 25, 1, "水泊梁山", "17666666666", "[email protected]","不是很熟悉");
Person huarong = new Person(9, "小李广花荣", 50, 1, "水泊梁山", "17666666666", "[email protected]","打酱油的");
Person chaijin = new Person(10, "小旋风柴进", 32, 1, "水泊梁山", "17666666666", "[email protected]","吓唬人的");
Person zhisheng = new Person(13, "花和尚鲁智深", 15, 1, "水泊梁山", "17666666666", "[email protected]","倒拔杨垂柳");
Person wusong = new Person(14, "行者武松", 28, 1, "水泊梁山", "17666666666", "[email protected]","二营长。。。。。。");
bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "1")
.setSource(JSONObject.toJSONString(lujunyi), XContentType.JSON)
);
bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "2")
.setSource(JSONObject.toJSONString(wuyong), XContentType.JSON)
);
bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "3")
.setSource(JSONObject.toJSONString(gongsunsheng), XContentType.JSON)
);
bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "4")
.setSource(JSONObject.toJSONString(guansheng), XContentType.JSON)
);
bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "5")
.setSource(JSONObject.toJSONString(linchong), XContentType.JSON)
);
bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "6")
.setSource(JSONObject.toJSONString(qinming), XContentType.JSON)
);
bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "7")
.setSource(JSONObject.toJSONString(huyanzhuo), XContentType.JSON)
);
bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "8")
.setSource(JSONObject.toJSONString(huarong), XContentType.JSON)
);
bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "9")
.setSource(JSONObject.toJSONString(chaijin), XContentType.JSON)
);
bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "10")
.setSource(JSONObject.toJSONString(zhisheng), XContentType.JSON)
);
bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "11")
.setSource(JSONObject.toJSONString(wusong), XContentType.JSON)
);
bulkRequestBuilder.get();
client.close();
}
通过id来进行查询索引
/**
* 通过id来进行精确查询
*/
@Test
public void query1() {
GetResponse documentFields = client.prepareGet("indexsearch", "mysearch", "11").get();
String index = documentFields.getIndex();
String type = documentFields.getType();
String id = documentFields.getId();
System.out.println(index);
System.out.println(type);
System.out.println(id);
Map<String, Object> source = documentFields.getSource();
for (String s : source.keySet()) {
System.out.println(source.get(s));
}
}
/**
* 查询所有数据
*/
@Test
public void queryAll() {
SearchResponse searchResponse = client
.prepareSearch("indexsearch")
.setTypes("mysearch")
.setQuery(new MatchAllQueryBuilder())
.get();
SearchHits searchHits = searchResponse.getHits();
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
String sourceAsString = hit.getSourceAsString();
System.out.println(sourceAsString);
}
client.close();
}
查找索引库当中年龄为18到28的所有人
/**
* 查找年龄18到28的人,包含18和28
*/
@Test
public void rangeQuery(){
SearchResponse searchResponse = client.prepareSearch("indexsearch")
.setTypes("mysearch")
.setQuery(new RangeQueryBuilder("age").gt(17).lt(29))
.get();
SearchHits hits = searchResponse.getHits();
SearchHit[] hits1 = hits.getHits();
for (SearchHit documentFields : hits1) {
System.out.println(documentFields.getSourceAsString());
}
client.close();
}
/**
* 词条查询
*/
@Test
public void termQuery(){
SearchResponse searchResponse = client.prepareSearch("indexsearch").setTypes("mysearch")
.setQuery(new TermQueryBuilder("say", "熟悉"))
.get();
SearchHits hits = searchResponse.getHits();
SearchHit[] hits1 = hits.getHits();
for (SearchHit documentFields : hits1) {
System.out.println(documentFields.getSourceAsString());
}
}
模糊查询可以自动帮我们纠正写错误的英文单词,最大纠正次数两次
/**
* fuzzyQuery表示英文单词的最大可纠正次数,最大可以自动纠正两次
*/
@Test
public void fuzzyQuery(){
SearchResponse searchResponse = client.prepareSearch("indexsearch").setTypes("mysearch")
.setQuery(QueryBuilders.fuzzyQuery("say", "helOL").fuzziness(Fuzziness.TWO))
.get();
SearchHits hits = searchResponse.getHits();
SearchHit[] hits1 = hits.getHits();
for (SearchHit documentFields : hits1) {
System.out.println(documentFields.getSourceAsString());
}
client.close();
}
*:匹配任意多个字符
?:仅匹配一个字符
/**
* 模糊匹配查询有两种匹配符,分别是" * " 以及 " ? ", 用" * "来匹配任何字符,包括空字符串。用" ? "来匹配任意的单个字符
*/
@Test
public void wildCardQueryTest(){
SearchResponse searchResponse = client.prepareSearch("indexsearch").setTypes("mysearch")
.setQuery(QueryBuilders.wildcardQuery("say", "hel*"))
.get();
SearchHits hits = searchResponse.getHits();
SearchHit[] hits1 = hits.getHits();
for (SearchHit documentFields : hits1) {
System.out.println(documentFields.getSourceAsString());
}
client.close();
}
使用boolQuery实现多条件组合查询
查询年龄是18到28范围内且性别是男性的,或者id范围在10到13范围内的
/**
* 多条件组合查询 boolQuery
* 查询年龄是18到28范围内且性别是男性的,或者id范围在10到13范围内的
*
*/
@Test
public void boolQueryTest(){
RangeQueryBuilder age = QueryBuilders.rangeQuery("age").gt(17).lt(29);
TermQueryBuilder sex = QueryBuilders.termQuery("sex", "1");
RangeQueryBuilder id = QueryBuilders.rangeQuery("id").gt("9").lt("15");
SearchResponse searchResponse = client.prepareSearch("indexsearch").setTypes("mysearch")
.setQuery(
QueryBuilders.boolQuery().should(id)
.should(QueryBuilders.boolQuery().must(sex).must(age)))
.get();
SearchHits hits = searchResponse.getHits();
SearchHit[] hits1 = hits.getHits();
for (SearchHit documentFields : hits1) {
System.out.println(documentFields.getSourceAsString());
}
client.close();
}
/*分页查询
*/
@Test
public void getPageIndex(){
int pageSize = 5;
int pageNum = 2;
int startNum = (pageNum-1)*5;
SearchResponse searchResponse = client.prepareSearch("indexsearch")
.setTypes("mysearch")
.setQuery(QueryBuilders.matchAllQuery())
.addSort("id",SortOrder.ASC)
.setFrom(startNum)
.setSize(pageSize)
.get();
SearchHits hits = searchResponse.getHits();
SearchHit[] hits1 = hits.getHits();
for (SearchHit documentFields : hits1) {
System.out.println(documentFields.getSourceAsString());
}
client.close();
}
在进行关键字搜索时,搜索出的内容中的关键字会显示不同的颜色,称之为高亮
京东商城搜索"笔记本"
通过开发者工具查看高亮数据的html代码实现:
ElasticSearch可以对查询出的内容中关键字部分进行标签和样式的设置,但是你需要告诉ElasticSearch使用什么标签对高亮关键字进行包裹
/**
* 高亮查询
*/
@Test
public void highLight(){
//设置我们的查询高亮字段
SearchRequestBuilder searchRequestBuilder = client.prepareSearch("indexsearch")
.setTypes("mysearch")
.setQuery(QueryBuilders.termQuery("say", "hello"));
//设置我们字段高亮的前缀与后缀
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.field("say").preTags("").postTags("");
//通过高亮来进行我们的数据查询
SearchResponse searchResponse = searchRequestBuilder.highlighter(highlightBuilder).get();
SearchHits hits = searchResponse.getHits();
System.out.println("查询出来一共"+ hits.totalHits+"条数据");
for (SearchHit hit : hits) {
//打印没有高亮显示的数据
System.out.println(hit.getSourceAsString());
System.out.println("=========================");
//打印我们经过高亮显示之后的数据
Text[] says = hit.getHighlightFields().get("say").getFragments();
for (Text say : says) {
System.out.println(say);
}
/* Map highlightFields = hit.getHighlightFields();
System.out.println(highlightFields);*/
}
client.close();
}
现有结构化数据内容如下:
name age salary team position
张云雷 26 2000 war pf
特斯拉 20 500 tim sf
于谦 25 2000 cav pg
爱迪生 40 1000 tim pf
爱因斯坦 21 300 tim sg
郭德纲 33 3000 cav sf
牛顿 21 500 tim c
岳云鹏 29 1000 war pg
初始化一批数据到es索引库当中去
/**
* 批量添加数据
* @throws IOException
* @throws ExecutionException
* @throws InterruptedException
*/
@Test
public void addIndexDatas() throws IOException, ExecutionException, InterruptedException {
//获取settings
//配置es集群的名字
Settings settings = Settings.builder().put("cluster.name", "myes").build();
//获取客户端
TransportAddress transportAddress = new TransportAddress(InetAddress.getByName("node01"), 9300);
TransportAddress transportAddress2 = new TransportAddress(InetAddress.getByName("node02"), 9300);
TransportAddress transportAddress3 = new TransportAddress(InetAddress.getByName("node03"), 9300);
//获取client客户端
TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(transportAddress).addTransportAddress(transportAddress2).addTransportAddress(transportAddress3);
/**
* 创建索引
* */
client.admin().indices().prepareCreate("player").get();
//构建json的数据格式,创建映射
XContentBuilder mappingBuilder = jsonBuilder()
.startObject()
.startObject("player")
.startObject("properties")
.startObject("name").field("type","text").field("index", "true").field("fielddata","true").endObject()
.startObject("age").field("type","integer").endObject()
.startObject("salary").field("type","integer").endObject()
.startObject("team").field("type","text").field("index", "true").field("fielddata","true").endObject()
.startObject("position").field("type","text").field("index", "true").field("fielddata","true").endObject()
.endObject()
.endObject()
.endObject();
PutMappingRequest request = Requests.putMappingRequest("player")
.type("player")
.source(mappingBuilder);
client.admin().indices().putMapping(request).get();
//批量添加数据开始
BulkRequestBuilder bulkRequest = client.prepareBulk();
// either use client#prepare, or use Requests# to directly build index/delete requests
bulkRequest.add(client.prepareIndex("player", "player", "1")
.setSource(jsonBuilder()
.startObject()
.field("name", "郭德纲")
.field("age", 33)
.field("salary",3000)
.field("team" , "cav")
.field("position" , "sf")
.endObject()
)
);
bulkRequest.add(client.prepareIndex("player", "player", "2")
.setSource(jsonBuilder()
.startObject()
.field("name", "于谦")
.field("age", 25)
.field("salary",2000)
.field("team" , "cav")
.field("position" , "pg")
.endObject()
)
);
bulkRequest.add(client.prepareIndex("player", "player", "3")
.setSource(jsonBuilder()
.startObject()
.field("name", "岳云鹏")
.field("age", 29)
.field("salary",1000)
.field("team" , "war")
.field("position" , "pg")
.endObject()
)
);
bulkRequest.add(client.prepareIndex("player", "player", "4")
.setSource(jsonBuilder()
.startObject()
.field("name", "爱因斯坦")
.field("age", 21)
.field("salary",300)
.field("team" , "tim")
.field("position" , "sg")
.endObject()
)
);
bulkRequest.add(client.prepareIndex("player", "player", "5")
.setSource(jsonBuilder()
.startObject()
.field("name", "张云雷")
.field("age", 26)
.field("salary",2000)
.field("team" , "war")
.field("position" , "pf")
.endObject()
)
);
bulkRequest.add(client.prepareIndex("player", "player", "6")
.setSource(jsonBuilder()
.startObject()
.field("name", "爱迪生")
.field("age", 40)
.field("salary",1000)
.field("team" , "tim")
.field("position" , "pf")
.endObject()
)
);
bulkRequest.add(client.prepareIndex("player", "player", "7")
.setSource(jsonBuilder()
.startObject()
.field("name", "牛顿")
.field("age", 21)
.field("salary",500)
.field("team" , "tim")
.field("position" , "c")
.endObject()
)
);
bulkRequest.add(client.prepareIndex("player", "player", "8")
.setSource(jsonBuilder()
.startObject()
.field("name", "特斯拉")
.field("age", 20)
.field("salary",500)
.field("team" , "tim")
.field("position" , "sf")
.endObject()
)
);
BulkResponse bulkResponse = bulkRequest.get();
client.close();
}
sql语句实现
select team, count(*) as player_count from player group by team;
使用javaAPI实现
@Test
public void groupAndCount() {
//1:构建查询提交
SearchRequestBuilder builder = client.prepareSearch("player").setTypes("player");
//2:指定聚合条件
TermsAggregationBuilder team = AggregationBuilders.terms("player_count").field("team");
//3:将聚合条件放入查询条件中
builder.addAggregation(team);
//4:执行action,返回searchResponse
SearchResponse searchResponse = builder.get();
Aggregations aggregations = searchResponse.getAggregations();
for (Aggregation aggregation : aggregations) {
StringTerms stringTerms = (StringTerms) aggregation;
List<StringTerms.Bucket> buckets = stringTerms.getBuckets();
for (StringTerms.Bucket bucket : buckets) {
System.out.println(bucket.getKey());
System.out.println(bucket.getDocCount());
}
}
}
sql语句实现
select team, position, count(*) as pos_count from player group by team, position;
java代码实现
/**
* 统计每个球队中每个位置的球员数量
*/
@Test
public void teamAndPosition(){
SearchRequestBuilder builder = client.prepareSearch("player").setTypes("player");
TermsAggregationBuilder team = AggregationBuilders.terms("player_count").field("team");
TermsAggregationBuilder position = AggregationBuilders.terms("posititon_count").field("position");
team.subAggregation(position);
SearchResponse searchResponse = builder.addAggregation(team).addAggregation(position).get();
Aggregations aggregations = searchResponse.getAggregations();
for (Aggregation aggregation : aggregations) {
// System.out.println(aggregation.toString());
StringTerms stringTerms = (StringTerms) aggregation;
List<StringTerms.Bucket> buckets = stringTerms.getBuckets();
for (StringTerms.Bucket bucket : buckets) {
long docCount = bucket.getDocCount();
Object key = bucket.getKey();
System.out.println("当前队伍名称为" + key + "该队伍下有"+docCount + "个球员");
Aggregation posititon_count = bucket.getAggregations().get("posititon_count");
if(null != posititon_count){
StringTerms positionTrem = (StringTerms) posititon_count;
List<StringTerms.Bucket> buckets1 = positionTrem.getBuckets();
for (StringTerms.Bucket bucket1 : buckets1) {
Object key1 = bucket1.getKey();
long docCount1 = bucket1.getDocCount();
System.out.println("该队伍下面的位置为" + key1+"该位置下有" + docCount1 +"人");
}
}
}
}
}
计算每个球队年龄最大值
select team, max(age) as max_age from player group by team;
/**
* 计算每个球队年龄最大值
*/
@Test
public void groupAndMax(){
SearchRequestBuilder builder = client.prepareSearch("player").setTypes("player");
TermsAggregationBuilder team = AggregationBuilders.terms("team_group").field("team");
MaxAggregationBuilder age = AggregationBuilders.max("max_age").field("age");
team.subAggregation(age);
SearchResponse searchResponse = builder.addAggregation(team).get();
Aggregations aggregations = searchResponse.getAggregations();
for (Aggregation aggregation : aggregations) {
StringTerms stringTerms = (StringTerms) aggregation;
List<StringTerms.Bucket> buckets = stringTerms.getBuckets();
for (StringTerms.Bucket bucket : buckets) {
Aggregation max_age = bucket.getAggregations().get("max_age");
System.out.println(max_age.toString());
}
}
}
计算每个球队年龄最大/最小/总/平均的球员年龄
select team, min(age) as min_age from player group by team;
/**
* 统计每个球队中年龄最小值
*/
@Test
public void teamMinAge(){
SearchRequestBuilder builder = client.prepareSearch("player").setTypes("player");
TermsAggregationBuilder team = AggregationBuilders.terms("team_count").field("team");
MinAggregationBuilder age = AggregationBuilders.min("min_age").field("age");
TermsAggregationBuilder termAggregation = team.subAggregation(age);
SearchResponse searchResponse = builder.addAggregation(termAggregation).get();
Aggregations aggregations = searchResponse.getAggregations();
for (Aggregation aggregation : aggregations) {
System.out.println(aggregation.toString());
StringTerms stringTerms = (StringTerms) aggregation;
List<StringTerms.Bucket> buckets = stringTerms.getBuckets();
for (StringTerms.Bucket bucket : buckets) {
Aggregations aggregations1 = bucket.getAggregations();
for (Aggregation aggregation1 : aggregations1) {
System.out.println(aggregation1.toString());
}
}
}
}
计算每个球队年龄最大/最小/总/平均的球员年龄
select team, avg(age) as max_age from player group by team;
/**
* 计算每个球队的年龄平均值
*/
@Test
public void avgTeamAge(){
SearchRequestBuilder builder = client.prepareSearch("player").setTypes("player");
TermsAggregationBuilder team_field = AggregationBuilders.terms("player_count").field("team");
AvgAggregationBuilder age_avg = AggregationBuilders.avg("age_avg").field("age");
team_field.subAggregation(age_avg);
SearchResponse searchResponse = builder.addAggregation(team_field).get();
Aggregations aggregations = searchResponse.getAggregations();
for (Aggregation aggregation : aggregations) {
System.out.println(aggregation.toString());
StringTerms stringTerms = (StringTerms) aggregation;
}
}
计算每个球队球员的平均年龄,同时又要计算总年薪
select team, avg(age)as avg_age, sum(salary) as total_salary from player group by team;
/**
* 统计每个球队当中的球员平均年龄,以及队员总年薪
*/
@Test
public void avgAndSum(){
SearchRequestBuilder builder = client.prepareSearch("player").setTypes("player");
TermsAggregationBuilder team_group = AggregationBuilders.terms("team_group").field("team");
AvgAggregationBuilder avg_age = AggregationBuilders.avg("avg_age").field("age");
SumAggregationBuilder sumMoney = AggregationBuilders.sum("sum_money").field("salary");
TermsAggregationBuilder termsAggregationBuilder = team_group.subAggregation(avg_age).subAggregation(sumMoney);
SearchResponse searchResponse = builder.addAggregation(termsAggregationBuilder).get();
Aggregations aggregations = searchResponse.getAggregations();
for (Aggregation aggregation : aggregations) {
System.out.println(aggregation.toString());
}
}
计算每个球队总年薪,并按照总年薪倒序排列
/**
* 计算每个球队总年薪,并按照年薪进行排序
* select team, sum(salary) as total_salary from player group by team order by total_salary desc;
*/
@Test
public void orderBySum(){
SearchRequestBuilder builder = client.prepareSearch("player").setTypes("player");
TermsAggregationBuilder teamGroup = AggregationBuilders.terms("team_group").field("team").order(BucketOrder.aggregation(“sum_salary”,false));
SumAggregationBuilder sumSalary = AggregationBuilders.sum("sum_salary").field("salary");
TermsAggregationBuilder termsAggregationBuilder = teamGroup.subAggregation(sumSalary);
SearchResponse searchResponse = builder.addAggregation(termsAggregationBuilder).get();
Map<String, Aggregation> stringAggregationMap = searchResponse.getAggregations().asMap();
System.out.println(stringAggregationMap.toString());
Aggregations aggregations = searchResponse.getAggregations();
for (Aggregation aggregation : aggregations) {
System.out.println(aggregation.toString());
}
}