1、什么是ElasticSearch?
ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。
2、整合ElasticSearch常用的方式
2.1基于spring-boot-starter-data-elasticsearch整合
2.2基于原生的TransportClient整合
为什么不用spring-boot-starter-data-elasticsearch整合呢?因为太麻烦,用它需要考虑es的版本,springboot的版本,Spring Data Elasticsearch的版本。由于本人下载的最新版es,暂时没找到兼容的版本。而且使用原生的好处就是自己可以随意封装。缺点就是必须是json格式数据。
3、代码实现
3.1、pom.xml
<dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>7.1.0</version></dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>transport</artifactId><version>7.1.0</version></dependency><!-- https://mvnrepository.com/artifact/org.elasticsearch.plugin/transport-netty4-client --><dependency><groupId>org.elasticsearch.plugin</groupId><artifactId>transport-netty4-client</artifactId><version>7.1.0</version></dependency>
es是当前最新版本7.1.0,所引用的jar也是采用最新的。
3.2 、yml文件
server:port: 8080servlet:context-path: /elasticsearch-demo
elasticsearch:cluster-name: elasticsearchip: 127.0.0.1port: 9300pool: 5
这里是我们的配置pool是连接池大小,其他的不用说都懂得,cluster-name这个可以自己去conf个文件中修改,默认是elasticsearch
3.3、ElasticSearchConfig.java文件,
package com.cxw.elasticsearchdemo.config;import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.net.InetAddress;/*** @Author: cxw* @CreateDate: 2019-05-23 14:22* @Description:*/
@Slf4j
@Configuration
public class ElasticSearchConfig {@Value("${elasticsearch.ip}")private String hostName;/*** 端口*/@Value("${elasticsearch.port}")private String port;/*** 集群名称*/@Value("${elasticsearch.cluster-name}")private String clusterName;/*** 连接池*/@Value("${elasticsearch.pool}")private String poolSize;/*** Bean name default 函数名字* @return*/@Bean(name = "transportClient")public TransportClient transportClient() {log.info("Elasticsearch初始化开始。。。。。");TransportClient transportClient = null;try {// 配置信息Settings esSetting = Settings.builder().put("cluster.name", clusterName) //集群名字.put("client.transport.sniff", true)//增加嗅探机制,找到ES集群.put("thread_pool.search.size", Integer.parseInt(poolSize))//增加线程池个数,暂时设为5.build();//配置信息Settings自定义transportClient = new PreBuiltTransportClient(esSetting);TransportAddress transportAddress = new TransportAddress(InetAddress.getByName(hostName), Integer.valueOf(port));transportClient.addTransportAddresses(transportAddress);} catch (Exception e) {log.error("elasticsearch TransportClient create error!!", e);}return transportClient;}}
3.4、ElasticsearchUtil文件
package com.cxw.elasticsearchdemo.util;import com.alibaba.fastjson.JSONObject;
import com.cxw.elasticsearchdemo.model.EsPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.lucene.search.TotalHits;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;/*** @Author: cxw* @CreateDate: 2019-05-23 14:31* @Description:*/
@Component
@Slf4j
public class ElasticsearchUtil {@Autowiredprivate TransportClient transportClient;private static TransportClient client;/*** @PostContruct是spring框架的注解* spring容器初始化的时候执行该方法*/@PostConstructpublic void init() {client = this.transportClient;}/*** 创建索引** @param index* @return*/public static boolean createIndex(String index) {if(!isIndexExist(index)){log.info("Index is not exits!");}CreateIndexResponse indexResponse = client.admin().indices().prepareCreate(index).execute().actionGet();log.info("执行建立成功?" + indexResponse.isAcknowledged());return indexResponse.isAcknowledged();}/*** 删除索引** @param index* @return*/public static boolean deleteIndex(String index) {if(!isIndexExist(index)) {log.info("Index is not exits!");}AcknowledgedResponse dResponse = client.admin().indices().prepareDelete(index).execute().actionGet();if (dResponse.isAcknowledged()) {log.info("delete index " + index + " successfully!");} else {log.info("Fail to delete index " + index);}return dResponse.isAcknowledged();}/*** 判断索引是否存在** @param index* @return*/public static boolean isIndexExist(String index) {IndicesExistsResponse inExistsResponse = client.admin().indices().exists(new IndicesExistsRequest(index)).actionGet();if (inExistsResponse.isExists()) {log.info("Index [" + index + "] is exist!");} else {log.info("Index [" + index + "] is not exist!");}return inExistsResponse.isExists();}/*** 数据添加,正定ID** @param jsonObject 要增加的数据* @param index 索引,类似数据库* @param type 类型,类似表* @param id 数据ID* @return*/public static String addData(JSONObject jsonObject, String index, String type, String id) {IndexResponse response = client.prepareIndex(index, type, id).setSource(jsonObject).get();log.info("addData response status:{},id:{}", response.status().getStatus(), response.getId());return response.getId();}/*** 数据添加** @param jsonObject 要增加的数据* @param index 索引,类似数据库* @param type 类型,类似表* @return*/public static String addData(JSONObject jsonObject, String index, String type) {return addData(jsonObject, index, type, UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());}/*** 通过ID删除数据** @param index 索引,类似数据库* @param type 类型,类似表* @param id 数据ID*/public static void deleteDataById(String index, String type, String id) {DeleteResponse response = client.prepareDelete(index, type, id).execute().actionGet();log.info("deleteDataById response status:{},id:{}", response.status().getStatus(), response.getId());}/*** 通过ID 更新数据** @param jsonObject 要增加的数据* @param index 索引,类似数据库* @param type 类型,类似表* @param id 数据ID* @return*/public static void updateDataById(JSONObject jsonObject, String index, String type, String id) {UpdateRequest updateRequest = new UpdateRequest();updateRequest.index(index).type(type).id(id).doc(jsonObject);client.update(updateRequest);}/*** 通过ID获取数据** @param index 索引,类似数据库* @param type 类型,类似表* @param id 数据ID* @param fields 需要显示的字段,逗号分隔(缺省为全部字段)* @return*/public static Map<String,Object> searchDataById(String index, String type, String id, String fields) {GetRequestBuilder getRequestBuilder = client.prepareGet(index, type, id);if (StringUtils.isNotEmpty(fields)) {getRequestBuilder.setFetchSource(fields.split(","), null);}GetResponse getResponse = getRequestBuilder.execute().actionGet();return getResponse.getSource();}/*** 使用分词查询,并分页** @param index 索引名称* @param type 类型名称,可传入多个type逗号分隔* @param startPage 当前页* @param pageSize 每页显示条数* @param query 查询条件* @param fields 需要显示的字段,逗号分隔(缺省为全部字段)* @param sortField 排序字段* @param highlightField 高亮字段* @return*/public static EsPage searchDataPage(String index, String type, int startPage, int pageSize, QueryBuilder query, String fields, String sortField, String highlightField) {SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index);if (StringUtils.isNotEmpty(type)) {searchRequestBuilder.setTypes(type.split(","));}searchRequestBuilder.setSearchType(SearchType.QUERY_THEN_FETCH);// 需要显示的字段,逗号分隔(缺省为全部字段)if (StringUtils.isNotEmpty(fields)) {searchRequestBuilder.setFetchSource(fields.split(","), null);}//排序字段if (StringUtils.isNotEmpty(sortField)) {searchRequestBuilder.addSort(sortField, SortOrder.DESC);}// 高亮(xxx=111,aaa=222)if (StringUtils.isNotEmpty(highlightField)) {HighlightBuilder highlightBuilder = new HighlightBuilder();//highlightBuilder.preTags("<span style='color:red' >");//设置前缀//highlightBuilder.postTags("</span>");//设置后缀// 设置高亮字段highlightBuilder.field(highlightField);searchRequestBuilder.highlighter(highlightBuilder);}//searchRequestBuilder.setQuery(QueryBuilders.matchAllQuery());searchRequestBuilder.setQuery(query);// 分页应用searchRequestBuilder.setFrom(startPage).setSize(pageSize);// 设置是否按查询匹配度排序searchRequestBuilder.setExplain(true);//打印的内容 可以在 Elasticsearch head 和 Kibana 上执行查询log.info("\n{}", searchRequestBuilder);// 执行搜索,返回搜索响应信息SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();TotalHits totalHits = searchResponse.getHits().getTotalHits();long length = searchResponse.getHits().getHits().length;log.debug("共查询到[{}]条数据,处理数据条数[{}]", totalHits.value, length);if (searchResponse.status().getStatus() == 200) {// 解析对象List<Map<String, Object>> sourceList = setSearchResponse(searchResponse, highlightField);return new EsPage(startPage, pageSize, (int) totalHits.value, sourceList);}return null;}/*** 使用分词查询** @param index 索引名称* @param type 类型名称,可传入多个type逗号分隔* @param query 查询条件* @param size 文档大小限制* @param fields 需要显示的字段,逗号分隔(缺省为全部字段)* @param sortField 排序字段* @param highlightField 高亮字段* @return*/public static List<Map<String, Object>> searchListData(String index, String type, QueryBuilder query, Integer size, String fields, String sortField, String highlightField) {SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index);if (StringUtils.isNotEmpty(type)) {searchRequestBuilder.setTypes(type.split(","));}if (StringUtils.isNotEmpty(highlightField)) {HighlightBuilder highlightBuilder = new HighlightBuilder();// 设置高亮字段highlightBuilder.field(highlightField);searchRequestBuilder.highlighter(highlightBuilder);}searchRequestBuilder.setQuery(query);if (StringUtils.isNotEmpty(fields)) {searchRequestBuilder.setFetchSource(fields.split(","), null);}searchRequestBuilder.setFetchSource(true);if (StringUtils.isNotEmpty(sortField)) {searchRequestBuilder.addSort(sortField, SortOrder.DESC);}if (size != null && size > 0) {searchRequestBuilder.setSize(size);}//打印的内容 可以在 Elasticsearch head 和 Kibana 上执行查询log.info("\n{}", searchRequestBuilder);SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();TotalHits totalHits = searchResponse.getHits().getTotalHits();long length = searchResponse.getHits().getHits().length;log.info("共查询到[{}]条数据,处理数据条数[{}]", totalHits.value, length);if (searchResponse.status().getStatus() == 200) {// 解析对象return setSearchResponse(searchResponse, highlightField);}return null;}/*** 高亮结果集 特殊处理** @param searchResponse* @param highlightField*/private static List<Map<String, Object>> setSearchResponse(SearchResponse searchResponse, String highlightField) {List<Map<String, Object>> sourceList = new ArrayList<Map<String, Object>>();StringBuffer stringBuffer = new StringBuffer();for (SearchHit searchHit : searchResponse.getHits().getHits()) {searchHit.getSourceAsMap().put("id", searchHit.getId());if (StringUtils.isNotEmpty(highlightField)) {System.out.println("遍历 高亮结果集,覆盖 正常结果集" + searchHit.getSourceAsMap());Text[] text = searchHit.getHighlightFields().get(highlightField).getFragments();if (text != null) {for (Text str : text) {stringBuffer.append(str.string());}//遍历 高亮结果集,覆盖 正常结果集searchHit.getSourceAsMap().put(highlightField, stringBuffer.toString());}}sourceList.add(searchHit.getSourceAsMap());}return sourceList;}
}
3.5测试案例
package com.cxw.elasticsearchdemo.test;import com.alibaba.fastjson.JSONObject;
import com.cxw.elasticsearchdemo.model.GoodsInfo;
import com.cxw.elasticsearchdemo.util.ElasticsearchUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.DateUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;/*** @Author: cxw* @CreateDate: 2019-05-22 14:44* @Description:*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class GoodsResiportyTest {/*** 类型*/private String esType="external";/*** 索引*/private String indexName="test_index";/*** 指定索引插入数据*/@Testpublic void insertJson(){JSONObject jsonObject = new JSONObject();jsonObject.put("id", DateUtils.formatDate(new Date()));jsonObject.put("age", 25);jsonObject.put("name", "j-" + new Random(100).nextInt());jsonObject.put("createTime", new Date());String id = ElasticsearchUtil.addData(jsonObject, indexName, esType, jsonObject.getString("id"));}/*** 创建索引*/@Testpublic void createIndex(){if(!ElasticsearchUtil.isIndexExist(indexName)) {ElasticsearchUtil.createIndex(indexName);}else{System.out.print("索引已经存在");}System.out.print("索引创建成功");}@Testpublic void delete() {String id="12";if(StringUtils.isNotBlank(id)) {ElasticsearchUtil.deleteDataById(indexName, esType, id);System.out.print("删除id=" + id);}else{System.out.print("id为空");}}@Testpublic void queryMatchData() {BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();boolean matchPhrase = false;if (matchPhrase == Boolean.TRUE) {boolQuery.must(QueryBuilders.matchPhraseQuery("name", "j"));} else {boolQuery.must(QueryBuilders.matchQuery("name", "j"));}List<Map<String, Object>> list = ElasticsearchUtil.searchListData(indexName, esType, boolQuery, 10, null, null, null);System.out.print(JSONObject.toJSONString(list));}
}
这样整合es的代码就完成了,基本上符合绝大数需求了,每一个大的项目都是一个个小模块堆积成的。当然这只是个demo优化的地方肯定还是有的,比如inde索引,key可以设置为动态.代码都是一步步优化。
源码:https://gitee.com/wangzaiwork/elasticsearch-demo.git
-----------------------------------写的不好,仅供参考-----------------------------------------