之前已将spring boot原生方式介绍了,接下将结介绍的是Elasticsearch聚合操作。聚合操作一般来说是解决一下复杂的业务,比如mysql中的求和和分组,由于博主踩的坑比较多,所以博客可能更多的会介绍这些坑。

一、application.properties配置文件

##端口号
server.port=8880
##es地址
spring.data.elasticsearch.cluster-nodes =127.0.0.1:9300

二、创建一个Bean层

import org.springframework.data.elasticsearch.annotations.Document;

@Document(indexName = "article",type = "center")
public class Zoo {
private int id;
private String animal;
private Integer num;
private String breeder;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAnimal() {
return animal;
}
public void setAnimal(String animal) {
this.animal = animal;
}
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}
public String getBreeder() {
return breeder;
}
public void setBreeder(String breeder) {
this.breeder = breeder;
}
public Zoo(int id, String animal, Integer num, String breeder) {
super();
this.id = id;
this.animal = animal;
this.num = num;
this.breeder = breeder;
}
public Zoo() {
super();
} }

bean层代码

三、创建一个dao层

import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; @Configuration
public interface ZooMapper extends ElasticsearchRepository<Zoo,Integer>{ }

dao层代码

四、创建一个Controller层,编写聚合代码 

import java.util.Map;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.metrics.sum.InternalSum;
import org.elasticsearch.search.aggregations.metrics.sum.SumAggregationBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.ResultsExtractor;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class UserController { @Autowired
ZooMapper zooMapper; @Autowired
private ElasticsearchTemplate elasticsearchTemplate;
// 访问接口地址:localhost:8880/find
//存储数据
@GetMapping("find")
public Object save(){
//创建查询条件,这里表示查询所有文档
QueryBuilder queryBuilder=QueryBuilders.boolQuery();
//构造sql组装容器
NativeSearchQueryBuilder nativeSearchQueryBuilder =new NativeSearchQueryBuilder();
//创建聚合条件,求和函数
SumAggregationBuilder sumAgg = AggregationBuilders.sum("sum_num").field("num"); //将查询条件和聚合条件放入sql组装容器中
nativeSearchQueryBuilder.withQuery(queryBuilder);
nativeSearchQueryBuilder.addAggregation(sumAgg); //封装sql组装容器
SearchQuery query = nativeSearchQueryBuilder.build();
//执行sql,此时容器中的sql1为
//select SUM(num) from zoo
Aggregations aggregations = elasticsearchTemplate.query(query, new ResultsExtractor<Aggregations>() {
@Override
public Aggregations extract(SearchResponse response) {
return response.getAggregations();
}
});
//将aggregations转换成map集合
Map<String, Aggregation> aggregationMap = aggregations.asMap();
//得到聚合的值,参数是自己定义的别名
InternalSum internalSum=(InternalSum) aggregationMap.get("sum_num");
return internalSum.getValue();
} }

controller层代码

在测试之前,我们需要在ES中添加索引:

访问 localhost:8880/find

本文只介绍了求和的聚合方式,至于其他聚合方式可以参考:

http://blog.csdn.net/u010454030/article/details/63266035

五、遇到的问题

  ES中text类型无法聚合问题,错误代码:

java.lang.IllegalArgumentException: Fielddata is disabled on text fields by default. Set fielddata=true on [geoip.city_name] in order to load fielddata in memory by uninverting the inverted index. 
Note that this can however use significant memory. Alternatively use a keyword field instead.at org.elasticsearch.index.mapper.TextFieldMapper$TextFieldType.fielddataBuilder(TextFieldMapper.java:336)

解决方案:

通过get请求地址:http://127.0.0.1:9200/你的索引名/你的type名/_search

  参数为:  

{"你的type名字":
{"properties":
{"你要设置的字段名":
{"type":"text","fielddata":true}
}
}
}

  

Elasticsearch学习(4) spring boot整合Elasticsearch的聚合操作的更多相关文章

  1. Elasticsearch学习(3) spring boot整合Elasticsearch的原生方式

    前面我们已经介绍了spring boot整合Elasticsearch的jpa方式,这种方式虽然简便,但是依旧无法解决我们较为复杂的业务,所以原生的实现方式学习能够解决这些问题,而原生的学习方式也是E ...

  2. Elasticsearch学习(1) Spring boot整合Elasticsearch

    本文的Spring Boot版本为1.5.9,Elasticsearch版本为2.4.4,话不多说,直接上代码. 一.启动Elasticsearch 在官网上下载Elasticsearch后,打开bi ...

  3. Spring Boot整合Elasticsearch

    Spring Boot整合Elasticsearch   Elasticsearch是一个全文搜索引擎,专门用于处理大型数据集.根据描述,自然而然使用它来存储和搜索应用程序日志.与Logstash和K ...

  4. 【spring boot】【elasticsearch】spring boot整合elasticsearch,启动报错Caused by: java.lang.IllegalStateException: availableProcessors is already set to [8], rejecting [8

    spring boot整合elasticsearch, 启动报错: Caused by: java.lang.IllegalStateException: availableProcessors ], ...

  5. Spring Boot 项目学习 (四) Spring Boot整合Swagger2自动生成API文档

    0 引言 在做服务端开发的时候,难免会涉及到API 接口文档的编写,可以经历过手写API 文档的过程,就会发现,一个自动生成API文档可以提高多少的效率. 以下列举几个手写API 文档的痛点: 文档需 ...

  6. Spring Boot 整合 Elasticsearch,实现 function score query 权重分查询

    摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢! 『 预见未来最好的方式就是亲手创造未来 – <史蒂夫·乔布斯传> 』 运行环境: ...

  7. spring boot 整合 elasticsearch 5.x

    spring boot与elasticsearch集成有两种方式.一种是直接使用elasticsearch.一种是使用data中间件. 本文只指针使用maven集成elasticsearch 5.x, ...

  8. Spring Boot 整合 elasticsearch

    一.简介 我们的应用经常需要添加检索功能,开源的 ElasticSearch 是目前全文搜索引擎的 首选.他可以快速的存储.搜索和分析海量数据.Spring Boot通过整合Spring Data E ...

  9. Spring Boot整合ElasticSearch和Mysql 附案例源码

    导读 前二天,写了一篇ElasticSearch7.8.1从入门到精通的(点我直达),但是还没有整合到SpringBoot中,下面演示将ElasticSearch和mysql整合到Spring Boo ...

随机推荐

  1. 跟我学算法-图像识别之图像分类(上)(基础神经网络, 卷积神经网络(CNN), AlexNet,NIN, VGG)

    1.基础神经网络: 输入向量x,权重向量w, 偏置标量b, 激活函数sigmoid(增加非线性度) 优化手段: 梯度下降优化, BP向后传播(链式规则) 梯度下降优化: 1. 使用得目标函数是交叉熵  ...

  2. PM2 介绍

    [源引]https://github.com/Unitech/pm2 pm2 是一个带有负载均衡功能的Node应用的进程管理器.当你要把你的独立代码利用全部的服务器上的所有CPU,并保证进程永远都活着 ...

  3. Spring Cloud Bus实现自动更新配置

    一.概述 1. 配置环境 版本:Spring Boot版本2.0.3.RELEASE,Spring Cloud版本Finchley.SR1,RabbitMQ 3.7.7 说明:本文章是在https:/ ...

  4. 旋转图像 · Rotate Image

    [抄题]: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockw ...

  5. WebLogic(12C)——几个基本概念

    转http://blog.csdn.net/hanxuemin12345/article/details/46287597 目录(?)[-] 域Domain 服务器Server 机器Machine W ...

  6. C和C++中的不定参数

    在初学C的时候,我们都会用到printf函数来写Hello World的程序.在我们看printf函数的声明时,会看到类似于下面代码 int printf(const char * __restric ...

  7. jquery记录

    jquery validate验证框架 参考:http://www.cnblogs.com/linjiqin/p/3431835.html http://www.runoob.com/jquery/j ...

  8. 自动创建orcl表

    using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Text; ...

  9. 使用c语言实现在linux下的openssl客户端和服务器端编程

    使用c语言实现在linux下的openssl客户端和服务器端编程 摘自:https://www.cnblogs.com/etangyushan/p/3679457.html 前几天组长让我实现一个使用 ...

  10. DataStage 二、InfoSphere Information Server进程的启动和停止

    DataStage序列文章 DataStage 一.安装 1 关于进程需要了解的基础知识 名称 说明 ASB代理进程 通信代理程序,它的作用是协助层与层之间的通信,默认端口是31531,它以后台进程的 ...