package com.es.common;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.es.entity.FileObj;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.ActionFuture;
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.admin.indices.exists.indices.IndicesExistsRequest;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.admin.indices.exists.types.TypesExistsRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
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.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.ingest.Pipeline;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
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.Component;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.*;
/**
* author: 阿杰
*/
@Component
@Slf4j
public class ElasticsearchUtil {
@Autowired
private RestHighLevelClient restHighLevelClient;
private static RestHighLevelClient levelClient;
@PostConstruct
public void initClient() {
levelClient = this.restHighLevelClient;
}
/**
* 创建索引
*
* @param index
* @return
*/
public static boolean createIndex(String index) throws IOException {
if (!isIndexExist(index)) {
log.info("Index is not exists!");
}
CreateIndexRequest request = new CreateIndexRequest(index);
// 添加 IK 分词器设置 ik分词器一定要存在 不然检索会报错
// request.settings(Settings.builder()
// .put("index.analysis.analyzer.default.type", "ik_max_word")
// .put("index.analysis.analyzer.default.use_smart", "true")
// );
//
// // 添加 IK 分词器设置 ik_smart 分词会更细一点
// request.settings(Settings.builder()
// .put("index.analysis.analyzer.default.type", "ik_smart")
// );
CreateIndexResponse response = levelClient.indices().create(request, RequestOptions.DEFAULT);
log.info("执行建立成功?" + response.isAcknowledged());
return response.isAcknowledged();
}
/**
* 删除索引
*
* @param index
* @return
*/
public static boolean deleteIndex(String index) throws IOException {
if (!isIndexExist(index)) {
log.info("Index is not exists!");
}
DeleteIndexRequest request = new DeleteIndexRequest(index);
AcknowledgedResponse response = levelClient.indices().delete(request, RequestOptions.DEFAULT);
if (response.isAcknowledged()) {
log.info("delete index " + index + " successfully!");
} else {
log.info("Fail to delete index " + index);
}
return response.isAcknowledged();
}
/**
* 判断索引是否存在
*
* @param index
* @return
*/
public static boolean isIndexExist(String index) throws IOException {
GetIndexRequest request = new GetIndexRequest(index);
boolean exists = levelClient.indices().exists(request, RequestOptions.DEFAULT);
if (exists) {
log.info("Index [" + index + "] exists!");
} else {
log.info("Index [" + index + "] does not exist!");
}
return exists;
}
/**
* 判断index下指定type是否存在
*/
// public boolean isTypeExist(String index, String type) throws IOException {
// if (isIndexExist(index)) {
// TypesExistsRequest request = new TypesExistsRequest(new String[]{index}, new String[]{type});
// return levelClient.indices().existsType(request, RequestOptions.DEFAULT);
// } else {
// return false;
// }
// }
/**
* 数据添加,指定ID
*
* @param jsonObject 要增加的数据
* @param index 索引,类似数据库
* @param type 类型,类似表
* @param id 数据ID
* @return
*/
public static String addData(JSONObject jsonObject, String index, String type, String id) throws IOException {
IndexRequest request = new IndexRequest(index, type, id).source(jsonObject);
IndexResponse response = levelClient.index(request, RequestOptions.DEFAULT);
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) throws IOException {
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) throws IOException {
DeleteRequest request = new DeleteRequest(index, type, id);
DeleteResponse response = levelClient.delete(request, RequestOptions.DEFAULT);
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) throws IOException {
UpdateRequest request = new UpdateRequest(index, type, id).doc(jsonObject);
levelClient.update(req
没有合适的资源?快使用搜索试试~ 我知道了~
Attachment 实现 全文检索
共17个文件
java:9个
properties:2个
备注说明:1个
需积分: 0 49 下载量 32 浏览量
2023-11-05
23:04:30
上传
评论
收藏 63KB ZIP 举报
温馨提示
Attachment 插件是 Elasticsearch 中的一种插件,允许将各种二进制文件(如PDF、Word文档等)以及它们的内容索引到 Elasticsearch 中。插件使用 Apache Tika 库来解析和提取二进制文件的内容。通过使用 Attachment 插件,可以轻松地在 Elasticsearch 中建立全文搜索功能,而无需事先转换二进制文件为文本。 优点: 可以将各种类型的二进制文件以原始形式存储在 Elasticsearch 中。这使得保存和访问二进制文件变得更加简单和高效。 插件使用 Apache Tika 库来解析和提取二进制文件的内容,因此可以提取并存储内容、元数据以及格式化的文本数据。这使得 Elasticsearch 可以轻松地对文档执行全文搜索以及文档内容的其他分析操作。 在 Elasticsearch 中使用 Attachment 插件,可以轻松地实现以下一些功能:搜索文档、生成全文搜索报告、自动标记文件、提取数据并进行分析,在文档中查找特定项等。
资源推荐
资源详情
资源评论
收起资源包目录
springboot-elasticsearch.zip (17个子文件)
mvnw.cmd 5KB
pom.xml 4KB
src
test
java
com
es
main
resources
application.properties 104B
备注说明 504B
java
com
es
controller
EsController.java 11KB
SpringbootElasticsearchApplication.java 3KB
service
common
RandomNameGenerator.java 1KB
EsPage.java 2KB
ElasticsearchUtil.java 21KB
entity
FileObj.java 729B
config
FileUploadConfiuration.java 720B
ElasticSearchClientConfig.java 1KB
Swagger2Config.java 4KB
.mvn
wrapper
maven-wrapper.properties 111B
maven-wrapper.jar 46KB
mvnw 7KB
.gitignore 292B
共 17 条
- 1
资源评论
菜鸟小杰子
- 粉丝: 4903
- 资源: 16
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功