1.ES的背景:
ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。官方客户端在Java、.NET(C#)、PHP、Python、Apache Groovy、Ruby和许多其他语言中都是可用的。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr,也是基于Lucene。
2.注意事项:ElasticSearch的版本跟ElasticSearch的客户端要保持一致的版本,另外不同版本的客户端,他们的编码风格也不一致。ElasticSearch7.0的版本跟以往的版本风格是不一样的,详细的可以查看官方文档。
3.这里用到的工具有idea,ElasticSearch7.2,跟es的Java High Level REST Client,要了解Java High Level REST Client客户端的api,可以到看这个文档。基于ssm框架来实现一个简单的,结合mysql和es的增删改查。
4.可以用ES官网中的Kibana工具,来实现对ES API的操作。如果需要配置多个ES;
5.ES中的conf/elasticsearch.yml对其修改,在下面添加修改:
这是一个主节点的配置
http.cors.enabled: true http.cors.allow-origin: "*" cluster.name: xiao node.name: slave node.master: true network.host: 127.0.0.1
配置第二个节点
cluster.name: xiao node.name: slave1 network.host: 127.0.0.1 http.port: 8200
配置第三个节点
cluster.name: xiao node.name: slave2 network.host: 127.0.0.1 http.port: 7200 discovery.zen.ping.unicast.hosts: ["127.0.0.1"]
6.也可以配一个Elasticsearch-head来看集群中的内容:
1.pom:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zhongmin</groupId> <artifactId>zhongDemo</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.1.1</version> <scope>provided</scope> </dependency> <!--实现slf4j接口并整合--> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc</artifactId> <version>6</version> </dependency> <!--druid==>阿里巴巴数据库连接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.25</version> </dependency> <!--2.dao框架:MyBatis依赖--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.0</version> </dependency> <!--mybatis自身实现的spring整合依赖--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> </dependency> <!--3.Servlet web相关依赖--> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.4</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!--4:spring依赖--> <!--1)spring核心依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.14.RELEASE</version> </dependency> <!--2)spring dao层依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.3.14.RELEASE</version> </dependency> <!--3)springweb相关依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.14.RELEASE</version> </dependency> <!--4)spring test相关依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.35</version> <scope>runtime</scope> </dependency> <!--Aop要导入的包--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.9</version> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20170516</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.8</version> </dependency> <!-- ElasticSearch核心包 --> <!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch --> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>7.2.0</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.2.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <includeEmptyDirectories>true</includeEmptyDirectories> </configuration> </plugin> </plugins> </build> </project>
2.es客户端连接集群:
package com.xiaoqiang.servic.impl; import com.xiaoqiang.dao.CompanyDao; import com.xiaoqiang.entiy.Company; import com.xiaoqiang.servic.search.CompanyIndexservic; import com.xiaoqiang.util.CompanyKey; import com.xiaoqiang.util.InitDemo; import org.apache.lucene.search.TotalHits; import org.elasticsearch.action.DocWriteResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.TermQueryBuilder; import org.elasticsearch.index.reindex.BulkByScrollResponse; import org.elasticsearch.index.reindex.DeleteByQueryRequest; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.IOException; import java.util.HashMap; import java.util.Map; @Service public class CompanyIndexservicImpl implements CompanyIndexservic { private static final Logger logger = LoggerFactory.getLogger(CompanyIndexservic.class); private static final String INDEX_NAME = "mycompany"; private RestHighLevelClient client = InitDemo.getClient(); @Autowired private CompanyDao companyDao; @Override public void index(Long companyId) { Company company = companyDao.findOne(companyId); if (company == null) { logger.error("Index Company {} does not exist", companyId); return; } SearchRequest searchRequest = new SearchRequest(INDEX_NAME); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.termQuery(CompanyKey.companyId,company.getId())); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = null; try { searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); } catch (IOException e) { e.printStackTrace(); } logger.debug(searchRequest.toString()); SearchHits hits = searchResponse.getHits(); TotalHits totalHits = hits.getTotalHits(); long numHits = totalHits.value; boolean success; if(numHits==0) { success=create(company); } else if(numHits==1){ success=update(company); } else { success=deleteAndCreate(numHits,company); } if(success) { logger.debug("创建索引成功"+company.getId()); } } private boolean create(Company company) { Map<String, Object> jsonMap = new HashMap<>(); jsonMap.put(CompanyKey.companyName, company.getCompanyname()); jsonMap.put(CompanyKey.companyId, company.getId()); IndexRequest indexRequest = new IndexRequest("mycompany") .id(String.valueOf(company.getId())).source(jsonMap); logger.debug("获取公司id=" + company.getId()); IndexResponse indexResponse = null; try { indexResponse = client.index(indexRequest, RequestOptions.DEFAULT); } catch (IOException e) { logger.error("创建失败失败Id=" + company.getId(), e); } if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) { return true; } else { return false; } } private boolean update(Company company) { Map<String, Object> jsonMap = new HashMap<>(); jsonMap.put("companyname", company.getCompanyname()); jsonMap.put("id", company.getId()); UpdateRequest request = new UpdateRequest(INDEX_NAME, company.getId().toString()).doc(jsonMap); UpdateResponse updateResponse = null; try { updateResponse = client.update( request, RequestOptions.DEFAULT); } catch (IOException e) { logger.error("更新失败失败Id=" + company.getId(), e); } if (updateResponse.getResult() == DocWriteResponse.Result.UPDATED) { return true; } else { return false; } } private boolean deleteAndCreate(long total, Company company) { DeleteByQueryRequest request = new DeleteByQueryRequest(INDEX_NAME); request.setQuery(new TermQueryBuilder(CompanyKey.companyId, company.getId())); BulkByScrollResponse bulkResponse = null; try { bulkResponse = client.deleteByQuery(request, RequestOptions.DEFAULT); logger.debug("delete company " + bulkResponse); } catch (IOException e) { e.printStackTrace(); } long deleted = bulkResponse.getDeleted(); if (deleted != total) { logger.warn("应该要删除{},但是只删除了{}", total, deleted); return false; } else { return create(company); } } @Override public void remvoe(Long companyId) { DeleteByQueryRequest request = new DeleteByQueryRequest(INDEX_NAME); request.setQuery(new TermQueryBuilder(CompanyKey.companyId,companyId)); BulkByScrollResponse bulkResponse = null; try { bulkResponse = client.deleteByQuery(request, RequestOptions.DEFAULT); logger.debug("delete company " + bulkResponse); } catch (IOException e) { e.printStackTrace(); } long deleted=bulkResponse.getDeleted(); logger.debug("删除了{}条",deleted); } }
3.获取连接服务的代码:
package com.xiaoqiang.util; import org.apache.http.HttpHost; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; /** * * @Description: 获取Java High Level REST Client客户端 * @author lgs * @date * */ public class InitDemo { public static RestHighLevelClient getClient() { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("localhost", 9200, "http"))); return client; } }