基于TransportClient整合

news/2024/11/29 0:53:38/

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
-----------------------------------写的不好,仅供参考-----------------------------------------


http://www.ppmy.cn/news/676609.html

相关文章

Java避免过多的if else

避免过多的if else 在Java 代码里会遇见很多的if else&#xff0c; 最近在坐企业微信回调处理的时候&#xff0c; 根据不同事件状态进行不同的业务&#xff0c; 状态大概有8个&#xff0c; 所以if else 也有8个&#xff0c; 特别不优雅&#xff0c; 所以整理一下常见的方法&…

字节跳动出品的 Flutter 应用内调试工具 UME 正式开源

2020 年 11 月&#xff0c;西瓜 Flutter 团队在字节跳动技术团队公众号上发布了 UME 工具的功能介绍和预览&#xff0c;今年 3 月 25 日的 Flutter Engage China 活动上&#xff0c;字节跳动 Flutter 技术负责人袁辉辉在 介绍 Flutter 业务的大规模落地的演讲 中也到了 UME 工具…

ArrayList使用详解

概述 ArrayList是Java中使用率很高的集合容器&#xff0c;面试频率也很高&#xff0c;本篇文章主要对ArrayList这个容器从功能到源码做一个深入解析&#xff0c;使用的是jdk8来讲解。 功能介绍 ArrayList是一个有顺序的容器&#xff0c;底层是一个数组&#xff0c;不过它是会…

CVPR2022车道线检测Efficient Lane Detection via Curve Modeling

分享前段时间看的一篇车道线检测方向的新工作&#xff0c;也是中了最近公开结果的2022CVPR&#xff0c;是上海交大、华东师大、香港城市大学和商汤科技合作完成的&#xff0c;代码已经开源。关于车道线检测任务&#xff0c;我之前也分享过几篇文章&#xff1a; 相关链接&#…

docker将容器制作成镜像并上传到阿里云镜像仓库

我们有时候服务需要拷贝到多个服务器&#xff0c;或者备份版本&#xff0c;这个时候就可以将服务容器制作成镜像上传私有仓库&#xff0c;需要的时候去私有仓库下载即可。 制作镜像 -a 作者 -m 备注 78e2274b24d3容器CONTAINER_ID tomcat02新的镜像名 v1为镜像tag docker comm…

学习线程池原理从手写一个线程池开始

概述 线程池技术想必大家都不陌生把&#xff0c;相信在平时的工作中没有少用&#xff0c;而且这也是面试频率非常高的一个知识点&#xff0c;那么大家知道它的实现原理和细节吗&#xff1f;如果直接去看jdk源码的话&#xff0c;可能有一定的难度&#xff0c;那么我们可以先通过…

Android 中RxPermissions 的使用

Android 中RxPermissions 的使用方法详解, 以请求拍照、读取位置权限为例 第一步&#xff0c;在module的build.gradle中的 dependencies { // RxPermissions 的使用方法详解implementation com.github.tbruyelle:rxpermissions:0.10.2implementation io.reactivex.rxjava2…

MySQL 表的操作命令合集(二)

目录 操作表的约束1、设置非空约束&#xff08;NOT NULL&#xff0c;NK&#xff09;2、设置字段的默认值&#xff08;DEFAULT&#xff09;3、设置唯一约束&#xff08;UNIQUE, UK&#xff09;4、设置主键约束&#xff08;PRIMARY KEY&#xff0c;PK&#xff09;4.1单字段主键4.…