前言:现在公司有一个项目要用到检索功能,检索上面现在最常用的是Solr/ES,最后经过对比选择了ElasticSearch开源组件包,因为这个是公司的一个产品项目,技术版本当然要用最新的啦,最后完全确定的技术是SpringBoot2.2.2+es7.5.1.好了废话不多说;上硬菜.
材料:
1: SpringBoot 2.2.2快速脚手架
2: ElasticSearch7.5.1 for Linux
1 安装ElasticSearch
安装就不说了,安装文档一大堆,网上百度去吧。
2 项目的pom.xml
org.springframework.boot spring-boot-starter-data-elasticsearch org.elasticsearch elasticsearch org.elasticsearch.client elasticsearch-rest-client org.elasticsearch.client elasticsearch-rest-high-level-client org.elasticsearch elasticsearch 7.5.1 org.elasticsearch.client elasticsearch-rest-high-level-client 7.5.1 org.springframework.boot spring-boot-starter-data-redis org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.1 com.alibaba druid-spring-boot-starter 1.1.21 mysql mysql-connector-java 5.1.48 runtime
3: 集成开始
ElasticSearch的发展历史,简单的说就是渐渐的淘汰了以前类似于redis-client那样类似的客户端API模式,现在基本上都是基于Restful规范的APIS,根据官方文档说明的推荐就是这样的Http请求方式,它分为两个版本的rest-client,第一个是高级版本、第二个是普通版本,对于我们程序员来说,毋庸置疑高级版本啦。
3.1 集成第一篇
集成之前确保ElasticSearch的服务是启动的,不然会发生链接超时保存.
第一种方式:
使用工具类的方式集成:
新建一个ElasticServerUtils类,代码如下:
@Component public class ElasticServiceUtils { /** *logger :SLF4J日志 */ private final static Logger logger = LoggerFactory.getLogger(ElasticServiceUtils.class); private RestHighLevelClient restHighLevelClient; /** *Description: 在Servlet容器初始化前执行 */ @PostConstruct private void init() { try { if (restHighLevelClient != null) { restHighLevelClient.close(); } //节点1和2 HttpHost node1 = new HttpHost("192.168.10.40", 9200, "http"); HttpHost node2 = new HttpHost("192.168.10.95", 9200, "http"); RestClientBuilder builder = RestClient.builder(node1,node2); restHighLevelClient = new RestHighLevelClient(builder); } catch (IOException e) { e.printStackTrace(); logger.error(e.getMessage()); } } //省略创建索引更新索引等代码,官网有具体的例子. //官网地址: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-index.html }
第二种方式:
这个方式就是和Spring框架深度集成,类似将RestHighLevelClient类的生命周期交给Spring区管理, 优点是使用方便直接使用@Autowired注解就可以获取到对象.缺点是相对的复杂.
第一步: 需要编写Bean的托管和创建配置.
代码:
/** *hosts :配置的值 */ @Value("${elasticsearch.hosts}") private String[] hosts; /** *restHighLevelClient :restHighLevel客户端 */ private RestHighLevelClient restHighLevelClient; /** * 返回实例 * @return RestHighLevelClient * @throws Exception 异常信息 */ @Override public RestHighLevelClient getObject() throws Exception { return this.restHighLevelClient; } /** * 反射 * * @return RestHighLevelClient.class */ @Override public Class> getObjectType() { return RestHighLevelClient.class; } /** * 客户端是否单例 * @return true */ @Override public boolean isSingleton() { return true; } /** * 客户端实例的销毁 * @throws Exception 异常信息 */ @Override public void destroy() throws Exception { if (restHighLevelClient != null) { restHighLevelClient.close(); } } /** * 注入参数 * @throws Exception 异常信息 */ @Override public void afterPropertiesSet() throws Exception { restHighLevelClient = buildClient(); } /** *Description: 自定义的构造方法 * * @return RestHighLevelClient */ private RestHighLevelClient buildClient() { try { //这里的builder方法有两个方式,第一个是传入Node(包含了多个节点,需要密码这些,我们没有配置,就暂时不需要),第二个就是传入HttpHost restHighLevelClient = new RestHighLevelClient(RestClient.builder(HttpHost.create(hosts[0]), HttpHost.create(hosts[1]))); } catch (Exception e) { logger.error(e.getMessage()); } return restHighLevelClient; }
配置代码:
elasticsearch: hosts: 192.168.10.40:9200,192.168.10.95:9200
这样就算是集成完了.