目录
windows下安装eshttps://blog.csdn.net/ytyDaMoTou/article/details/129773165
1 简介
Elasticsearch 是一个分布式可扩展的实时搜索和分析引擎,一个建立在全文搜索引擎 Apache Lucene(TM) 基础上的搜索引擎.当然 Elasticsearch 并不仅仅是 Lucene 那么简单,它不仅包括了全文搜索功能,还可以进行以下工作:
- 分布式实时文件存储,并将每一个字段都编入索引,使其可以被搜索。
- 实时分析的分布式搜索引擎。
- 可以扩展到上百台服务器,处理PB级别的结构化或非结构化数据。
2 集成springboot
2.1 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!-- ES高级Rest Client -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.4.3</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.4.3</version>
</dependency>
server: port: 8180 spring: data: elasticsearch: cluster-nodes: ip1:9300,203.ip2:9300,ip3:9300 cluster-name: my-application
注入:
@Autowired
private RestHighLevelClient restHighLevelClient;
2.2 删除索引库(彻底删除所有数据)
indices:索引库ID
restHighLevelClient.indices().delete(new DeleteIndexRequest(indices), RequestOptions.DEFAULT);
2.2 更新索引
@Override
public void updateDocuments(String indices, Object object) {
try {
JSONObject json = (JSONObject) JSONObject.toJSON(object);
if (StringUtils.isEmpty(json.getString("id"))){
throw new RuntimeException("id为空!");
}
UpdateRequest updateRequest = new UpdateRequest(indices,json.getString("id")).doc(JSONObject.toJSONString(object), XContentType.JSON);
updateRequest.docAsUpsert(true);
restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
} catch (IOException ioException) {
log.error("添加索引{}异常:{}", indices,ioException.getMessage(), ioException);
}
2.3 删除数据
@Override
public void deleteIndexById(String indices, String id) {
try {
DeleteRequest request = new DeleteRequest(indices, id);
restHighLevelClient.delete(request, RequestOptions.DEFAULT);
} catch (IOException ioException) {
log.error("删除索引{}异常:{}", indices,ioException.getMessage(), ioException);
}
}
2.4 添加数据到索引库
@Override
public void addDocuments(String indices, Object object) {
try {
BulkRequest bulkRequest = new BulkRequest();
IndexRequest request = new IndexRequest(indices);
JSONObject json = (JSONObject) JSONObject.toJSON(object);
request.source(json);
request.id(json.getString("id"));
bulkRequest.add(request);
restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
} catch (IOException ioException) {
log.error("添加索引{}异常:{}", indices,ioException.getMessage(), ioException);
}
}
2.5 判断索引中是否存在
/**
* 判断索引是否存在
* @return
* @throws IOException
*/
public boolean checkIndex (String index) {
try {
return restHighLevelClient.indices().exists(new GetIndexRequest(index), RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
return Boolean.FALSE ;
}
查询操作:
查询总数:
package com.example.ytyproject.component;
import com.alibaba.fastjson.JSONObject;
import com.example.ytyproject.entity.Logs;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
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.core.CountRequest;
import org.elasticsearch.client.core.CountResponse;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* es工具类
*/
@Component
@Slf4j
public class ElasticsearchService {
@Autowired
private RestHighLevelClient restHighLevelClient;
private static final String indices = "indices";
/**
* 判断索引是否存在
* index:索引id
*/
public boolean checkIndex(String index) {
try {
return restHighLevelClient.indices().exists(new GetIndexRequest(index), RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
return Boolean.FALSE ;
}
/**
* 添加索引
*
* @param indices 索引id
* @throws IOException
*/
public void createIndex(String indices) throws IOException {
// 定义索引名称
CreateIndexRequest request = new CreateIndexRequest(indices);
// 发送请求到ES
CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
// 处理响应结果
System.out.println("添加索引是否成功:" + response.isAcknowledged());
}
/**
* 删除索引
*/
public void deleteIndex(String indices)throws IOException {
// 定义索引名称
DeleteIndexRequest request = new DeleteIndexRequest(indices);
// 发送请求到ES
AcknowledgedResponse response = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
// 处理响应结果
System.out.println("删除索引是否成功:" + response.isAcknowledged());
}
/**
* 添加数据到es
* @param logs
*/
public void insertToEs(Logs logs) {
try {
if (!checkIndex(indices)) {
createIndex(indices);
}
BulkRequest bulkRequest = new BulkRequest();
IndexRequest request = new IndexRequest(indices);
JSONObject json = (JSONObject) JSONObject.toJSON(logs);
request.source(json);
request.id(json.getString("id"));
bulkRequest.add(request);
restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println("------------------------------------数据添加成功------------------------------------");
} catch (IOException ioException) {
log.error("添加索引{}异常:{}", "indices",ioException.getMessage(), ioException);
}
}
/**
* 更新es数据
* @param logs
*/
public void updateEsData(Logs logs) {
try {
String data = JSONObject.toJSONString(logs);
JSONObject json = (JSONObject) JSONObject.toJSON(logs);
if (StringUtils.isEmpty(json.getString("id"))){
throw new RuntimeException("id为空!");
}
UpdateRequest updateRequest = new UpdateRequest(indices, json.getString("id")).doc(data, XContentType.JSON);
updateRequest.docAsUpsert(true);
UpdateResponse response = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
String id = response.getId();
} catch (IOException ioException) {
log.error("添加索引{}异常:{}", indices,ioException.getMessage(), ioException);
}
}
/**
* 查询全部数据
*/
public List<Map<String,Object>> list() {
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 组装条件
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
// queryBuilder.must(QueryBuilders.termQuery("createTime", 1611378102795L));
// queryBuilder.mustNot(QueryBuilders.termQuery("name","北京-李四"));
sourceBuilder.query(queryBuilder);
SearchRequest searchRequest = new SearchRequest(indices);
searchRequest.source(sourceBuilder);
try {
SearchResponse searchResp = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
List<Map<String,Object>> data = new ArrayList<>() ;
SearchHit[] searchHitArr = searchResp.getHits().getHits();
for (SearchHit searchHit:searchHitArr){
Map<String,Object> temp = searchHit.getSourceAsMap();
temp.put("id",searchHit.getId()) ;
data.add(temp);
}
return data;
} catch (Exception e) {
e.printStackTrace();
}
return null ;
}
public long queryTotal() {
// 指定创建时间
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
// 按照条件查询
// queryBuilder.must(QueryBuilders.termQuery("createTime", 1611378102795L));
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(queryBuilder);
CountRequest countRequest = new CountRequest(indices);
countRequest.source(sourceBuilder);
try {
CountResponse countResponse = restHighLevelClient.count(countRequest, RequestOptions.DEFAULT);
return countResponse.getCount();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}