搭建elasticsearch服务并整合springboot+TransportClient

一、安装es服务及配置相关信息

拉取es镜像

docker pull elasticsearch:6.4.3

创建docker容器

docker run -d --name es -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:6.4.3

进入容器修改配置信息 

docker exec -it 14b506b174ff /bin/bash (进入容器命令)

修改config/elasticsearch.yml 文件中的配置信息:

cluster.name: elasticsearch-cluster
node.name: es-node1
network.bind_host: 0.0.0.0
network.publish_host: 139.159.201.124
http.port: 9200
transport.tcp.port: 9300
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
discovery.zen.ping.unicast.hosts: ["139.159.201.124:9300"]
discovery.zen.minimum_master_nodes: 1

重启容器

docker restart 14b506b174ff 

以上是安装es服务及配置信息步骤

二、 springboot+transportClient整合es

application.yml--------------------------------------------

elasticsearch:
ip: 139.159.201.124
port: 9300
pool: 5
cluster-name: elasticsearch-cluster

配置加载类ElasticsearchConfig

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;

@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() {
System.out.println("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);
System.out.println("Elasticsearch初始化成功。。。。。");
} catch (Exception e) {
System.out.println("elasticsearch TransportClient create error!!" + e);
}
return transportClient;
}
}

工具类 ElasticsearchUtil

import com.alibaba.fastjson.JSONObject;
import com.citcc.common.utils.StringUtils;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
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.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;

@Component
public class ElasticsearchUtil {

@Autowired
private TransportClient transportClient;

private static TransportClient client;

/**
* @PostContruct是spring框架的注解
* spring容器初始化的时候执行该方法
*/
@PostConstruct
public void init() {
this.client = this.transportClient;
}

/**
* 创建索引
*
* @param index
* @return
*/
public static boolean createIndex(String index) {
if(!isIndexExist(index)){
System.out.println("Index is not exits!");
}
CreateIndexResponse indexResponse = client.admin().indices().prepareCreate(index).execute().actionGet();
System.out.println("执行建立成功?" + indexResponse.isAcknowledged());
return indexResponse.isAcknowledged();
}

/**
* 删除索引
*
* @param index
* @return
*/
public static boolean deleteIndex(String index) {
if(!isIndexExist(index)) {
System.out.println("Index is not exits!");
}
DeleteIndexResponse dResponse = client.admin().indices().prepareDelete(index).execute().actionGet();
if (dResponse.isAcknowledged()) {
System.out.println("delete index " + index + " successfully!");
} else {
System.out.println("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()) {
System.out.println("Index [" + index + "] is exist!");
} else {
System.out.println("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();
System.out.println("addData response status:" + response.status().getStatus() + ",id:" + 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();
System.out.println("deleteDataById response status:"+response.status().getStatus()+",id:"+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 上执行查询
// LOGGER.info("\n{}", searchRequestBuilder);

// 执行搜索,返回搜索响应信息
SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();

long totalHits = searchResponse.getHits().totalHits;
long length = searchResponse.getHits().getHits().length;

System.out.println("共查询到["+totalHits+"]条数据,处理数据条数["+length+"]");

if (searchResponse.status().getStatus() == 200) {
// 解析对象
List<Map<String, Object>> sourceList = setSearchResponse(searchResponse, highlightField);

return new EsPage(startPage, pageSize, (int) totalHits, 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 上执行查询
// LOGGER.info("\n{}", searchRequestBuilder);

SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();

long totalHits = searchResponse.getHits().totalHits;
long length = searchResponse.getHits().getHits().length;

System.out.println("共查询到[" + totalHits + "]条数据,处理数据条数[" + 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;
}

}

引入的pom依赖:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.4.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</dependency>


@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() {
System.out.println("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);
System.out.println("Elasticsearch初始化成功。。。。。");
} catch (Exception e) {
System.out.println("elasticsearch TransportClient create error!!" + e);
}
return transportClient;
}

控制类

import com.alibaba.fastjson.JSONObject;
import com.citcc.api.utils.DateUtil;
import com.citcc.api.utils.ElasticsearchUtil;
import com.citcc.api.utils.Employee;
import com.citcc.api.utils.EsPage;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("/es")
public class EsController {

/**
* 测试索引
*/
private String indexName = "megacorp";

/**
* 类型
*/
private String esType = "employee";

/**
* 创建索引
* http://127.0.0.1:8080/es/createIndex
* @param request
* @param response
* @return
*/
@RequestMapping("/createIndex")
public String createIndex(HttpServletRequest request, HttpServletResponse response) {
if (!ElasticsearchUtil.isIndexExist(indexName)) {
ElasticsearchUtil.createIndex(indexName);
} else {
return "索引已经存在";
}
return "索引创建成功";
}

/**
* 插入记录
*
* @return
*/
@RequestMapping("/insertJson")
public String insertJson() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", DateUtil.formatDate(new Date()));
jsonObject.put("age", 25);
jsonObject.put("first_name", "j-" + new Random(100).nextInt());
jsonObject.put("last_name", "cccc");
jsonObject.put("about", "i like xiaofeng baby");
jsonObject.put("date", new Date());
String id = ElasticsearchUtil.addData(jsonObject, indexName, esType, jsonObject.getString("id"));
return id;
}

/**
* 插入记录
*
* @return
*/
@RequestMapping("/insertModel")
public String insertModel() {
Employee employee = new Employee();
employee.setId("66");
employee.setFirstName("m-" + new Random(100).nextInt());
employee.setAge("24");
JSONObject jsonObject = (JSONObject) JSONObject.toJSON(employee);
String id = ElasticsearchUtil.addData(jsonObject, indexName, esType, jsonObject.getString("id"));
return id;
}

/**
* 删除记录
*
* @return
*/
@RequestMapping("/delete")
public String delete(String id) {
if (StringUtils.isNotBlank(id)) {
ElasticsearchUtil.deleteDataById(indexName, esType, id);
return "删除id=" + id;
} else {
return "id为空";
}
}

/**
* 更新数据
*
* @return
*/
@RequestMapping("/update")
public String update(String id) {
if (StringUtils.isNotBlank(id)) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", id);
jsonObject.put("age", 31);
jsonObject.put("name", "修改");
jsonObject.put("date", new Date());
ElasticsearchUtil.updateDataById(jsonObject, indexName, esType, id);
return "id=" + id;
} else {
return "id为空";
}
}

/**
* 获取数据
* http://127.0.0.1:8080/es/getData?id=2018-04-25%2016:33:44
*
* @param id
* @return
*/
@RequestMapping("/getData")
public String getData(String id) {
if (StringUtils.isNotBlank(id)) {
Map<String, Object> map = ElasticsearchUtil.searchDataById(indexName, esType, id, null);
return JSONObject.toJSONString(map);
} else {
return "id为空";
}
}

/**
* 查询数据
* 模糊查询
*
* @return
*/
@RequestMapping("/queryMatchData")
public String queryMatchData() {
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolean matchPhrase = false;
if (matchPhrase == Boolean.TRUE) {
//不进行分词搜索
boolQuery.must(QueryBuilders.matchPhraseQuery("first_name", "cici"));
} else {
boolQuery.must(QueryBuilders.matchQuery("last_name", "cici"));
}
List<Map<String, Object>> list = ElasticsearchUtil.
searchListData(indexName, esType, boolQuery, 10, "first_name", null, "last_name");
return JSONObject.toJSONString(list);
}

/**
* 通配符查询数据
* 通配符查询 ?用来匹配1个任意字符,*用来匹配零个或者多个字符
*
* @return
*/
@RequestMapping("/queryWildcardData")
public String queryWildcardData() {
QueryBuilder queryBuilder = QueryBuilders.wildcardQuery("first_name.keyword", "cici");
List<Map<String, Object>> list = ElasticsearchUtil.searchListData(indexName, esType, queryBuilder, 10, null, null, null);
return JSONObject.toJSONString(list);
}

/**
* 正则查询
*
* @return
*/
@RequestMapping("/queryRegexpData")
public String queryRegexpData() {
QueryBuilder queryBuilder = QueryBuilders.regexpQuery("first_name.keyword", "m--[0-9]{1,11}");
List<Map<String, Object>> list = ElasticsearchUtil.searchListData(indexName, esType, queryBuilder, 10, null, null, null);
return JSONObject.toJSONString(list);
}

/**
* 查询数字范围数据
*
* @return
*/
@RequestMapping("/queryIntRangeData")
public String queryIntRangeData() {
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.rangeQuery("age").from(24)
.to(25));
List<Map<String, Object>> list = ElasticsearchUtil.searchListData(indexName, esType, boolQuery, 10, null, null, null);
return JSONObject.toJSONString(list);
}

/**
* 查询日期范围数据
*
* @return
*/
@RequestMapping("/queryDateRangeData")
public String queryDateRangeData() {
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.rangeQuery("age").from("20")
.to("50"));
List<Map<String, Object>> list = ElasticsearchUtil.searchListData(indexName, esType, boolQuery, 10, null, null, null);
return JSONObject.toJSONString(list);
}

/**
* 查询分页
*
* @param startPage 第几条记录开始
* 从0开始
* 第1页 :http://127.0.0.1:8080/es/queryPage?startPage=0&pageSize=2
* 第2页 :http://127.0.0.1:8080/es/queryPage?startPage=2&pageSize=2
* @param pageSize 每页大小
* @return
*/
@RequestMapping("/queryPage")
public String queryPage(String startPage, String pageSize) {
if (StringUtils.isNotBlank(startPage) && StringUtils.isNotBlank(pageSize)) {
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.rangeQuery("age").from("20")
.to("100"));
EsPage list = ElasticsearchUtil.searchDataPage(indexName, esType, Integer.parseInt(startPage), Integer.parseInt(pageSize), boolQuery, null, null, null);
return JSONObject.toJSONString(list);
} else {
return "startPage或者pageSize缺失";
}
}
}
posted on 2020-08-21 17:31  白皓颉  阅读(1238)  评论(0编辑  收藏  举报