1、安装es 和kibana
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" \ -v /var/es/conf/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \ -v /var/es/data:/usr/share/elasticsearch/data \ docker.elastic.co/elasticsearch/elasticsearch:7.17.10 docker run -d --name kibana -p 5601:5601 \ -e "ELASTICSEARCH_HOSTS=http://192.168.3.22:9200" \ -v /var/kibana/data:/usr/share/kibana/data \ docker.elastic.co/kibana/kibana:7.17.10
附件es配置文件
cluster.name: "docker-cluster" network.host: 0.0.0.0 discovery.type: single-node
2、springboot集成es
引包
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.17.10</version>
</dependency>
配置
elasticsearch: host: ${ES_SERVER:192.168.3.22} port: ${ES_PORT:9200} scheme: ${ES_SCHEME:http}
java配置
package jnpf.buryingpoint.config;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ElasticsearchConfig {
@Value("${elasticsearch.host}")
private String host;
@Value("${elasticsearch.port}")
private int port;
@Value("${elasticsearch.scheme}")
private String scheme;
@Bean(destroyMethod = "close")
public RestHighLevelClient client() {
return new RestHighLevelClient(
RestClient.builder(
new HttpHost(host, port, scheme)
)
);
}
}
es发送服务
package jnpf.buryingpoint.service;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import jnpf.buryingpoint.contant.BuryingPointConstant;
import jnpf.model.buryingpoint.dto.BuryingPointMsg;
import jnpf.model.buryingpoint.dto.EsBuryingPointMsg;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
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.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Service
@Slf4j
public class ElasticsearchService {
@Autowired
private RestHighLevelClient client;
@PostConstruct
public void init() throws IOException {
this.createIndexWithMapping(BuryingPointConstant.index);
}
public void createIndexWithMapping(String indexName) throws IOException {
GetIndexRequest existsRequest = new GetIndexRequest(indexName);
boolean exists = client.indices().exists(existsRequest, RequestOptions.DEFAULT);
if (exists) {
return;
}
// 创建索引和指定mapping
CreateIndexRequest request = new CreateIndexRequest(indexName);
// 定义映射
XContentBuilder mapping = JsonXContent.contentBuilder();
mapping.startObject();
{
mapping.startObject("properties");
{
mapping.startObject("terminal");
{
mapping.field("type", "keyword");
}
mapping.endObject();
mapping.startObject("appName");
{
mapping.field("type", "keyword");
}
mapping.endObject();
mapping.startObject("blockName");
{
mapping.field("type", "keyword");
}
mapping.endObject();
mapping.startObject("pageName");
{
mapping.field("type", "keyword");
}
mapping.endObject();
mapping.startObject("buttonName");
{
mapping.field("type", "keyword");
}
mapping.endObject();
mapping.startObject("source");
{
mapping.field("type", "text");
}
mapping.endObject();
mapping.startObject("time");
{
mapping.field("type", "date");
mapping.field("format", "yyyy-MM-dd HH:mm:ss Z");//配置日期支持的时间格式
}
mapping.endObject();
mapping.startObject("eventType");
{
mapping.field("type", "keyword");
}
mapping.endObject();
mapping.startObject("stayTime");
{
mapping.field("type", "integer");
}
mapping.endObject();
mapping.startObject("params");
{
mapping.field("type", "object");
mapping.field("enabled", false); // 如果不需要对 params 进行索引
}
mapping.endObject();
}
mapping.endObject();
}
mapping.endObject();
request.mapping(mapping);
CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
// 检查索引是否成功创建
if (createIndexResponse.isAcknowledged()) {
log.info("Index created es successfully,indexName=" + indexName);
} else {
log.error("Failed to create es index,indexName=" + indexName);
}
boolean exists = client.indices().exists(existsRequest, RequestOptions.DEFAULT);
if (exists) {
return;
}
// 创建索引和指定mapping
CreateIndexRequest request = new CreateIndexRequest(indexName);
// 定义映射
XContentBuilder mapping = JsonXContent.contentBuilder();
mapping.startObject();
{
mapping.startObject("properties");
{
mapping.startObject("terminal");
{
mapping.field("type", "keyword");
}
mapping.endObject();
mapping.startObject("appName");
{
mapping.field("type", "keyword");
}
mapping.endObject();
mapping.startObject("blockName");
{
mapping.field("type", "keyword");
}
mapping.endObject();
mapping.startObject("pageName");
{
mapping.field("type", "keyword");
}
mapping.endObject();
mapping.startObject("buttonName");
{
mapping.field("type", "keyword");
}
mapping.endObject();
mapping.startObject("source");
{
mapping.field("type", "text");
}
mapping.endObject();
mapping.startObject("time");
{
mapping.field("type", "date");
mapping.field("format", "yyyy-MM-dd HH:mm:ss Z");//配置日期支持的时间格式
}
mapping.endObject();
mapping.startObject("eventType");
{
mapping.field("type", "keyword");
}
mapping.endObject();
mapping.startObject("stayTime");
{
mapping.field("type", "integer");
}
mapping.endObject();
mapping.startObject("params");
{
mapping.field("type", "object");
mapping.field("enabled", false); // 如果不需要对 params 进行索引
}
mapping.endObject();
}
mapping.endObject();
}
mapping.endObject();
request.mapping(mapping);
CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
// 检查索引是否成功创建
if (createIndexResponse.isAcknowledged()) {
log.info("Index created es successfully,indexName=" + indexName);
} else {
log.error("Failed to create es index,indexName=" + indexName);
}
}
// Create
public void indexDocument(String index, String id, String jsonString) throws IOException {
AppBuryingPointMsgReq buryingPointMsg = JSONUtil.toBean(jsonString, AppBuryingPointMsgReq.class);
jsonString = JSONUtil.toJsonStr(buryingPointMsg,JSONConfig.create().setDateFormat("yyyy-MM-dd HH:mm:ss Z"));//增加时区,要不然在kibana中数据会有时区差8个小时
log.debug("EsBuryingPointMsg JSON: {}", jsonString);
IndexRequest request = new IndexRequest(index)
.id(id)
.source(jsonString, XContentType.JSON);
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
log.info("插入数据到ES中:" + jsonString + "," + id + ",res=" + response.getResult());
jsonString = JSONUtil.toJsonStr(buryingPointMsg,JSONConfig.create().setDateFormat("yyyy-MM-dd HH:mm:ss Z"));//增加时区,要不然在kibana中数据会有时区差8个小时
log.debug("EsBuryingPointMsg JSON: {}", jsonString);
IndexRequest request = new IndexRequest(index)
.id(id)
.source(jsonString, XContentType.JSON);
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
log.info("插入数据到ES中:" + jsonString + "," + id + ",res=" + response.getResult());
}
// Read
public EsBuryingPointMsg getDocument(String index, String id) throws IOException {
GetRequest request = new GetRequest(index, id);
GetResponse response = client.get(request, RequestOptions.DEFAULT);
if (response.isExists()) {
String sourceAsString = response.getSourceAsString();
EsBuryingPointMsg msg = JSONUtil.toBean(sourceAsString, EsBuryingPointMsg.class);
return msg;
} else {
return null;
}
}
//根据自定义条件查询
public List<EsBuryingPointMsg> getDocumentByCondition(String index, BuryingPointMsg msg, int from, int size) throws IOException {
if (StringUtils.isBlank(index)) {
throw new IllegalArgumentException("Index cannot be null or empty");
}
if (msg == null) {
throw new IllegalArgumentException("BuryingPointMsg cannot be null");
}
SearchRequest searchRequest = new SearchRequest(index);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
// 添加查询条件
if (StringUtils.isNotBlank(msg.getAppCode())) {
boolQueryBuilder.must(QueryBuilders.matchQuery("appCode", msg.getAppCode()).analyzer("ik_smart"));
// boolQueryBuilder.must(QueryBuilders.termQuery("appCode", msg.getAppCode()));
}
if (StringUtils.isNotBlank(msg.getBlockCode())) {
boolQueryBuilder.must(QueryBuilders.matchQuery("blockCode", msg.getBlockCode()).analyzer("ik_smart"));
// boolQueryBuilder.must(QueryBuilders.termQuery("blockCode", msg.getBlockCode()));
}
sourceBuilder.query(boolQueryBuilder);
// 设置返回所有字段
String[] includeFields = new String[]{};
String[] excludeFields = new String[]{};
FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includeFields, excludeFields);
sourceBuilder.fetchSource(fetchSourceContext);
// 设置排序
sourceBuilder.sort("createTime", SortOrder.DESC);
// 分页支持
sourceBuilder.from(from);
sourceBuilder.size(size);
searchRequest.source(sourceBuilder);
try {
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
List<EsBuryingPointMsg> result = new ArrayList<>();
searchResponse.getHits().forEach(hit -> {
EsBuryingPointMsg msgResult = JSONUtil.toBean(hit.getSourceAsString(), EsBuryingPointMsg.class);
result.add(msgResult);
});
if (result.isEmpty()) {
log.info("No results found for index: {}, with message: {}", index, msg);
}
return result;
} catch (IOException e) {
log.error("Error occurred while searching in index: {}", index, e);
throw e;
}
}
// Update
public void updateDocument(String index, String id, String jsonString) throws IOException {
UpdateRequest request = new UpdateRequest(index, id)
.doc(jsonString, XContentType.JSON);
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
System.out.println(response.getResult());
}
// Delete
public void deleteDocument(String index, String id) throws IOException {
DeleteRequest request = new DeleteRequest(index, id);
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
System.out.println(response.getResult());
}
}
调用demo
@Autowired ElasticsearchService elasticsearchService; public void add(String message) { try { elasticsearchService.indexDocument(BuryingPointConstant.index, UUID.randomUUID().toString(), message); } catch (IOException e) { throw new RuntimeException(e); } }
浙公网安备 33010602011771号