kibana是一个es索引库的可视化工具, 这边我也刚入门也做不了太多介绍,大家感兴趣的可以自行去研究一下!!!
下载完成之后我们就可以解压启动运行了
bin目录下点击elasticsearch.bat 运行
访问"http://localhost:9200/"
这边可以看到es的版本号,
查看已安装的插件http://localhost:9200/_cat/plugins
安装插件有两种方法 , 一个是如上图所示, 将插件直接解压拷贝到 elasticSearch 的plugins 文件夹下, 另一种是进入elasticSearch 的bin目录下
输入命令 : plugin install 插件名
例 : plugin install license
接下来运行kibana 直接进入bin目录,双击kibana运行
启动完毕之后http://localhost:5601/
准备工作做好之后接下来开始跟springboot整合吧
这边我没有使用springboot下的es 依赖 我用的是elasticSearch 高级客户端
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
yml如下
es:
address: 127.0.0.1
port: 9200
接下来是elasticSearch 的配置类
package com.es.esservice.bean;
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;
/**
* @auther:Wangxl
* @Emile:18335844494@163.com
* @Time:2020/11/2 14:35
*/
@Configuration
public class ElasticSearchClientConfig {
@Value("${es.address}")
private String address;
@Value("${es.port}")
private Integer port;
@Bean
public RestHighLevelClient restHighLevelClient() {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost(address, port, "http")
)
);
return client;
}
}
做到这一步我们已经把springboot 和 elasticSearch 整合在一块了
接下来我们可以在java代码中调用es的方法了
package com.es.esservice.service;
import com.alibaba.fastjson.JSONObject;
import com.es.esservice.bean.FileData;
import com.es.esservice.bean.Outcome;
import org.apache.pdfbox.multipdf.Splitter;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
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.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* @auther:Wangxl
* @Emile:18335844494@163.com
* @Time:2020/11/4 15:11
*/
@Service
public class FileDataNewServiceImpl implements FileDataNewService {
@Autowired
private RestHighLevelClient restHighLevelClient;
/**
* 创建索引
*
* @param indexName
* @return
*/
@Override
public Outcome createIndex(String indexName) {
try {
XContentBuilder builder = XContentFactory.jsonBuilder()
.startObject()
.field("properties")
.startObject()
.field("fileName").startObject().field("index", "true").field("type", "text").field("analyzer", "ik_max_word").endObject()
.field("page").startObject().field("index", "true").field("type", "keyword").endObject()
.field("departmentId").startObject().field("index", "true").field("type", "keyword").endObject()
.field("ljdm").startObject().field("index", "true").field("type", "keyword").endObject()
.field("data").startObject().field("index", "true").field("type", "text").field("analyzer", "ik_max_word").endObject()
.endObject()
.endObject();
CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
createIndexRequest.mapping("_doc", builder);
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
boolean acknowledged = createIndexResponse.isAcknowledged();
if (acknowledged) {
return Outcome.ok("新增成功");
} else {
return Outcome.error("新增失败");
}
} catch (Exception e) {
e.printStackTrace();
return Outcome.error("新增失败");
}
}
/**
* 新增文档
*
* @param indexName
* @param multipartFile
* @return
*/
@Override
public Outcome addFileDataPageNew(String indexName, String departmentId, String ljdm, MultipartFile multipartFile) {
try {
BASE64Encoder base64Encoder = new BASE64Encoder();
File file = multipartFileToFile(multipartFile);
PDDocument document = PDDocument.load(file);
Splitter splitter = new Splitter();
List<PDDocument> pages = splitter.split(document);
ListIterator<PDDocument> iterator = pages.listIterator();
int i = 1;
while (iterator.hasNext()) {
PDDocument currentDocument = iterator.next();
//获取一个PDFTextStripper文本剥离对象
PDFTextStripper stripper = new PDFTextStripper();
//获取文本内容
String content = stripper.getText(currentDocument);
FileData fileData = new FileData();
fileData.setDepartmentId(departmentId);
fileData.setLjdm(ljdm);
fileData.setFileName(multipartFile.getOriginalFilename());
fileData.setData(content);
fileData.setPage(i);
IndexRequest source = new IndexRequest(indexName).source(JSONObject.toJSONString(fileData), XContentType.JSON);
IndexResponse index = restHighLevelClient.index(source, RequestOptions.DEFAULT);
i++;
}
// return index.toString();
return Outcome.ok("新增成功");
} catch (Exception e) {
e.printStackTrace();
return Outcome.error("新增失败");
}
}
/**
* 删除文档
*
* @param indexName
* @param fileName
* @return
*/
@Override
public Outcome deleteFile(String indexName, String fileName) {
try {
DeleteByQueryRequest filedata = new DeleteByQueryRequest(indexName);
filedata.setQuery(new MatchQueryBuilder("fileName", fileName));
restHighLevelClient.deleteByQuery(filedata, RequestOptions.DEFAULT);
return Outcome.ok("删除成功");
} catch (Exception e) {
e.printStackTrace();
return Outcome.error("删除失败");
}
}
/**
* 根据文件名查询 并且高亮显示
* @param fileName
* @return
*/
@Override
public List queryFileByFileName(String fileName) {
ArrayList fileNames = new ArrayList();
try {
//创建搜索请求
SearchRequest searchRequest = new SearchRequest();
//构造搜索参数
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
MatchQueryBuilder builderByFileName = QueryBuilders.matchQuery("fileName", fileName);
searchSourceBuilder.query(builderByFileName);
searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
searchSourceBuilder.sort("page", SortOrder.ASC);
//高亮
HighlightBuilder highlightBuilder = new HighlightBuilder();
//设置高亮字段
highlightBuilder.field("fileName");
//如果要多个字段高亮,这项要为false
highlightBuilder.requireFieldMatch(true);
highlightBuilder.preTags("<span style='color:red'>");
highlightBuilder.postTags("</span>");
highlightBuilder.fragmentSize(800000); //最大高亮分片数
highlightBuilder.numOfFragments(0); //从第一个分片获取高亮片段
searchSourceBuilder.highlighter(highlightBuilder);
searchRequest.source(searchSourceBuilder);
SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
// List<Map<String, Object>> list = new ArrayList<>();
for (SearchHit hit : response.getHits().getHits()) {
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
FileData fileData = new FileData();
fileData.setFileName((String) sourceAsMap.get("fileName"));
fileData.setPage((Integer) sourceAsMap.get("page"));
fileData.setData((String) sourceAsMap.get("data"));
//解析高亮字段
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
HighlightField field1 = highlightFields.get("fileName");
if (field1 != null) {
Text[] fragments = field1.fragments();
String n_field = "";
for (Text fragment : fragments) {
n_field += fragment;
}
//高亮标题覆盖原标题
fileData.setFileName(n_field);
// sourceAsMap.put("data",n_field);
}
fileNames.add(fileData);
// list.add(hit.getSourceAsMap());
}
return fileNames;
} catch (Exception e) {
e.printStackTrace();
}
return fileNames;
}
/**
* 删除索引
*
* @param indexName
* @return
*/
@Override
public Outcome removeIndex(String indexName) {
try {
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indexName);
restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
return Outcome.ok("删除索引成功");
} catch (Exception e) {
e.printStackTrace();
return Outcome.error("删除索引失败");
}
}
/**
* 查询索引是否存在
*
* @param indexName
* @return
*/
@Override
public Outcome selectIndex(String indexName) {
try {
GetIndexRequest indexRequest = new GetIndexRequest(indexName);
boolean exists = restHighLevelClient.indices().exists(indexRequest, RequestOptions.DEFAULT);
if (exists) {
return Outcome.ok("索引已存在");
} else {
return Outcome.error("索引不存在");
}
} catch (Exception e) {
e.printStackTrace();
return Outcome.error("查询索引出错");
}
}
public static File multipartFileToFile(MultipartFile file) throws Exception {
File toFile = null;
if (file.equals("") || file.getSize() <= 0) {
file = null;
} else {
InputStream ins = null;
ins = file.getInputStream();
toFile = new File(file.getOriginalFilename());
inputStreamToFile(ins, toFile);
ins.close();
}
return toFile;
}
//获取流文件
private static void inputStreamToFile(InputStream ins, File file) {
try {
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
} catch (Exception e) {
e.printStackTrace();
}
}
以上的业务代码大家可以参考一下
感 谢 阅 读 !