SpringBoot -- 集成Elasticsearch
来源:互联网 发布:80c51系列单片机 编辑:程序博客网 时间:2024/06/14 10:35
前置工作
- 当前服务器为CentOS6.5+ 64bit
新建 elasticsearch用户,ES无法用root启动
- useradd -d /usr/elasticsearch -g elastic -m elasticsearch
获取elasticsearch,本文用的v2.3.4 找到要获取的版本
- ES已经有5.x版本,然而Spring data目前还只支持ES2.x版本,Springdata支持ES版本说明
了解ES与mysql的对应关系
- index –> DB
- type –> Table
- Document –> row
安装Elasticsearch
用wget命令 获取elasticsearch后,直接解压到对应的目录中
修改 config下的elasticsearch.yml
配置 cluster.name、network.host
cluster.name:nininetwork.host 0.0.0.0 #为任意都可连接
启动ES,bin目录下
sh elasticsearch
Springboot集成ES
新建ES module,引入 spring-boot-starter-data-elasticsearch、spring-data-elasticsearch
build.gradle
apply plugin: 'org.springframework.boot'dependencyManagement{ imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:" + springCloudVersion mavenBom "org.springframework.boot:spring-boot-starter:"+ springBootVersion }}repositories { mavenCentral()}dependencies { compile ('org.springframework.data:spring-data-redis') compile ('org.springframework.boot:spring-boot-starter-data-mongodb:'+springBootVersion) compile ('org.springframework.boot:spring-boot-starter-web:'+springBootVersion) compile('org.springframework.cloud:spring-cloud-starter-eureka') compile ('mysql:mysql-connector-java:'+mysqlVersion) compile ('com.alibaba:druid:'+druidVersion) compile ('org.mybatis:mybatis-spring:'+mybatisSpringBootVersion) compile ('org.mybatis:mybatis:'+mybatisVersion) compile('org.springframework.boot:spring-boot-starter-log4j2') compile ('org.springframework.boot:spring-boot-starter-thymeleaf') compile ('net.sourceforge.nekohtml:nekohtml:'+nekoHtmlVersion) compile('org.apache.logging.log4j:log4j-1.2-api:'+ log4jAPIVersion) compile('org.springframework.boot:spring-boot-starter-jdbc') compile('org.springframework.boot:spring-boot-starter-aop') compile ('com.alibaba:fastjson:'+fastjsonVersion) compile ('redis.clients:jedis') compile ('org.springframework.boot:spring-boot-starter-data-elasticsearch:'+springBootVersion) compile ('org.springframework.data:spring-data-elasticsearch') testCompile ('org.springframework.boot:spring-boot-starter-test') testCompile group: 'junit', name: 'junit', version: '4.11'}configurations { all*.exclude module: 'spring-boot-starter-logging' all*.exclude module: 'logback-classic' all*.exclude module: 'log4j-over-slf4j' all*.exclude module: 'snappy-java'}jar { baseName = 'es-server-bootcwenao'}
在application.yml中配置ES信息
application.yml
spring: data: elasticsearch: cluster-name: nini cluster-nodes: 192.168.21.1:9300 local: false repositories: enable: true
创建 ES bean
用来查询获取对象,与mybatis差不多,唯独多了@Document与@Id 注解
AccountInfo.java
/** * @author cwenao * @version $Id AccountInfo.java, v 0.1 2017-02-06 10:28 cwenao Exp $$ */@Document(indexName = "cwenao",type = "accountinfo", shards = 1,replicas = 0, refreshInterval = "-1")public class AccountInfo { @Id private String id; @Field private String accountName; @Field private String nickName; //getter setter ...}
创建 Repository
用于数据查询,需要extends ElasticsearchRepository
ElasticAccountInfoRepository.java
/** * @author cwenao * @version $Id ElasticAccountInfoRepository.java, v 0.1 2017-02-06 10:26 cwenao Exp $$ */@Component("elasticAccountInfoRepository")public interface ElasticAccountInfoRepository extends ElasticsearchRepository<AccountInfo,String> { //TODO define the search AccountInfo findByAccountName(String accountName);}
创建 service
ESAccountInfoService.java
/** * @author cwenao * @version $Id ESAccountInfoService.java, v 0.1 2017-02-06 10:36 cwenao Exp $$ */public interface ESAccountInfoService { AccountInfo queryAccountInfoById(String id); AccountInfo queryAccountInfoByName(String accountName);}
ESAccountInfoServiceImpl.java
/** * @author cwenao * @version $Id ESAccountInfoServiceImpl.java, v 0.1 2017-02-06 10:38 cwenao Exp $$ */@Service("esAccountInfoServiceImpl")public class ESAccountInfoServiceImpl implements ESAccountInfoService { @Autowired private ElasticAccountInfoRepository elasticAccountInfoRepository; public AccountInfo queryAccountInfoById(String id) { return elasticAccountInfoRepository.findOne(id); } @Override public AccountInfo queryAccountInfoByName(String accountName) { return elasticAccountInfoRepository.findByAccountName(accountName); }}
创建controller
ESController.java
/** * @author cwenao * @version $Id ESController.java, v 0.1 2017-02-06 10:44 cwenao Exp $$ */@Controllerpublic class ESController { @Autowired private ESAccountInfoService esAccountInfoServiceImpl; @RequestMapping("/esAccountInfo") public String queryAccountInfo(String id, ModelMap modelMap){ AccountInfo accountInfo = esAccountInfoServiceImpl.queryAccountInfoById(id); modelMap.addAttribute("esAccountInfo",accountInfo); modelMap.addAttribute("test_elastic","Test the elasticsearch"); return "accountInfo"; } @RequestMapping("/esAccountInfoName") public String queryAccountInfoByAccountName(String accountName, ModelMap modelMap){ AccountInfo accountInfo = esAccountInfoServiceImpl.queryAccountInfoByName(accountName); modelMap.addAttribute("esAccountInfo",accountInfo); modelMap.addAttribute("test_elastic","Test the elasticsearch"); return "accountInfo"; }}
在apigateway工程配置path
bootstrap.yml
zuul: routes: esserver: path: /esserver/** serviceId: ESSERVER
apigateway/mybatis 等集成参考前面文章
测试
- 依次启动 discovery、configserver、apigateway、esserver
- 访问 http://localhost:10002/esserver/esAccountInfo?id=a63126d074f04db587cd76a48c817510
更多的查询方式
- ElasticsearchRepository提供了很有用的查询构造器
- 比如查询 去重distinct : findDistinctAccountInfoById(String id)
- 比如条件组合 **AND/OR : findAccountInfoByIdAndAccountname(String id,accountname)
- 查询构造器会忽略 find..By 等前缀
代码
代码请移步 Github参考地址
如有疑问请加公众号(K171),如果觉得对您有帮助请 github start
1 0
- SpringBoot -- 集成Elasticsearch
- SpringBoot -- 集成Elasticsearch
- SpringBoot -- 集成Elasticsearch
- SpringBoot集成ElasticSearch
- springboot集成JestClient连接elasticsearch-5.x
- Elasticsearch查询配置(Elasticsearch+springboot)
- Elasticsearch实践(二)在Springboot微服务中集成搜索服务
- 七、ElasticSearch与SpringBoot的集成与JPA方法的使用
- SpringBoot整合ElasticSearch
- SpringBoot 调用Elasticsearch
- ElasticSearch整合springboot实战
- springboot 集成hibernate 集成struts2
- springboot elasticsearch 简单测试类
- elasticsearch中文分词集成
- elasticsearch 与 hive集成
- elasticsearch spring 集成
- elasticsearch spring 集成
- ElasticSearch集成拼音插件
- SQL server 2008数据库的备份与还原(转)
- url、src与href属性的区别
- lombok介绍及基本使用方法
- C#/.net学习-12-winform基础
- linux下vsftpd的安装与配置说明
- SpringBoot -- 集成Elasticsearch
- eclipse启动错误之一(failed to load the JNI shared library)!
- 一段js面向对象的写法
- ArcGis for JS 利用QueryTask查询实现对地图的定位功能
- ThinkPHP查询数据库时各种条件设置
- Android 6.0中SELinux的TE简介
- poj1922
- 03-S3C2440u-boot学习之u-boot分析之Makefile结构分析
- 自动调光