一、Attachment?介紹
Attachment 插件是 Elasticsearch 中的一種插件,允許將各種二進制文件(如PDF、Word文檔等)以及它們的內(nèi)容索引到 Elasticsearch 中。插件使用 Apache Tika 庫來解析和提取二進制文件的內(nèi)容。通過使用 Attachment 插件,可以輕松地在 Elasticsearch 中建立全文搜索功能,而無需事先轉(zhuǎn)換二進制文件為文本。
優(yōu)點:
可以將各種類型的二進制文件以原始形式存儲在 Elasticsearch 中。這使得保存和訪問二進制文件變得更加簡單和高效。
插件使用 Apache Tika 庫來解析和提取二進制文件的內(nèi)容,因此可以提取并存儲內(nèi)容、元數(shù)據(jù)以及格式化的文本數(shù)據(jù)。這使得 Elasticsearch 可以輕松地對文檔執(zhí)行全文搜索以及文檔內(nèi)容的其他分析操作。
在 Elasticsearch 中使用 Attachment 插件,可以輕松地實現(xiàn)以下一些功能:搜索文檔、生成全文搜索報告、自動標記文件、提取數(shù)據(jù)并進行分析,在文檔中查找特定項等。
缺點:
Attachment 插件對性能有一定的影響,因為執(zhí)行全文搜索需要解析和提取二進制文件的內(nèi)容。如果處理大量的二進制文件,可能會影響搜索性能。
Attachment 插件有一些限制,例如插件不支持對二進制文件進行過濾或排除,因此如果文件內(nèi)容包含敏感信息,則不應使用 Attachment 插件進行索引。
二、初始化?ingest-attachment
1、windows安裝
?1、先在ES的bin目錄下執(zhí)行命令?安裝?ngest-attachment插件
elasticsearch-plugin install ingest-attachment
作者已經(jīng)安裝過了?所以不能重復安裝,插件下載過程中會出現(xiàn)
2、Liunx安裝?
通過官網(wǎng)下載,找到對應的版本號:attachment下載網(wǎng)站
下載好后上傳到服務(wù)器,進入elasticsearch安裝目下的bin目錄下。
執(zhí)行sudo ./elasticsearch-plugin install file:///home/ingest-attachment-7.9.0.zip 即可
重啟ES??打印 [apYgDEl] loaded plugin [ingest-attachment] 表示安裝成功
3、小結(jié)
安裝完成后需要重新啟動ES
接下來我們需要創(chuàng)建一個關(guān)于ingest-attachment的文本抽取管道
PUT /_ingest/pipeline/attachment
{
"description": "Extract attachment information",
"processors": [
{
"attachment": {
"field": "content",
"ignore_missing": true
}
},
{
"remove": {
"field": "content"
}
}
]
}
后續(xù)我們的文件需要base64后儲存到?attachment.content?索引字段中
三、如何應用?
1、通過命令語句簡易檢索
# 創(chuàng)建一個ES 索引 并且添加一些測試數(shù)據(jù)
POST /pdf_data/_doc?pretty
{
"id": "3",
"name": "面試題文件1.pdf",
"age": 18,
"type": "file",
"money": 1111,
"createBy": "阿杰",
"createTime": "2022-11-03T10:41:51.851Z",
"attachment": {
"content": "面試官:如何保證消息不被重復消費???如何保證消費的時候是冪等的啊?Kafka、ActiveMQ、RabbitMQ、RocketMQ 都有什么區(qū)別,以及適合哪些場景?",
"date": "2022-11-02T10:41:51.851Z",
"language": "en"
}
}
# 通過插入的文檔內(nèi)容為條件進行檢索
# 簡單 單條件查詢 文檔內(nèi)容檢索
GET /pdf_data/_search
{
"query": {
"match": {
"attachment.content": "面試官:如何保證消息不被重復消費???如何保證消費的時候是冪等的啊?"
}
}
}
2、整合java代碼實現(xiàn)ES通過ingest-attachment進行全文檢索
?1、首先將文件轉(zhuǎn)為BASE64進行ES數(shù)據(jù)插入
/**
* 將文件 文檔信息儲存到數(shù)據(jù)中
* @param file
* @return
*/
@PostMapping("/insertFile")
@ApiOperation(value="創(chuàng)建索引ES-傳入ES索引-傳入文件", notes="創(chuàng)建索引ES-傳入ES索引-傳入文件")
public IndexResponse insertFile(@RequestAttribute("file") MultipartFile file,@RequestParam("indexName")String indexName){
FileObj fileObj = new FileObj();
fileObj.setId(String.valueOf(System.currentTimeMillis()));
fileObj.setName(file.getOriginalFilename());
fileObj.setType(file.getName().substring(file.getName().lastIndexOf(".") + 1));
fileObj.setCreateBy(RandomNameGenerator.generateRandomName());
fileObj.setCreateTime(String.valueOf(System.currentTimeMillis()));
fileObj.setAge(RandomNameGenerator.getAge());
fileObj.setMoney(RandomNameGenerator.getMoney());
// 文件轉(zhuǎn)base64
byte[] bytes = new byte[0];
try {
bytes = file.getBytes();
//將文件內(nèi)容轉(zhuǎn)化為base64編碼
String base64 = Base64.getEncoder().encodeToString(bytes);
fileObj.setContent(base64);
IndexResponse indexResponse= ElasticsearchUtil.upload(fileObj,indexName);
if (0==indexResponse.status().getStatus()){
// 索引創(chuàng)建并插入數(shù)據(jù)成功
System.out.println("索引創(chuàng)建并插入數(shù)據(jù)成功");
}
return indexResponse;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
?2、創(chuàng)建索引、插入數(shù)據(jù),并且將文檔數(shù)據(jù)抽取到管道中
@Autowired
private RestHighLevelClient restHighLevelClient;
private static RestHighLevelClient levelClient;
@PostConstruct
public void initClient() {
levelClient = this.restHighLevelClient;
}
/**
* 創(chuàng)建索引并插入數(shù)據(jù)
* @param file
* @param indexName
* @return
* @throws IOException
*/
public static IndexResponse upload(FileObj file,String indexName) throws IOException {
// TODO 創(chuàng)建前需要判斷當前文檔是否已經(jīng)存在
if (!isIndexExist(indexName)) {
CreateIndexRequest request = new CreateIndexRequest(indexName);
// 如果需要ik分詞器就添加配置,不需要就注釋掉
// 添加 IK 分詞器設(shè)置 ik_max_word
// request.settings(Settings.builder()
// .put("index.analysis.analyzer.default.type", "ik_max_word")
// .put("index.analysis.analyzer.default.use_smart", "true")
// );
// 添加 IK 分詞器設(shè)置 ik_smart
request.settings(Settings.builder()
.put("index.analysis.analyzer.default.type", "ik_smart")
);
CreateIndexResponse response = levelClient.indices().create(request, RequestOptions.DEFAULT);
log.info("執(zhí)行建立成功?" + response.isAcknowledged());
}
IndexRequest indexRequest = new IndexRequest(indexName);
//上傳同時,使用attachment pipline進行提取文件
indexRequest.source(JSON.toJSONString(file), XContentType.JSON);
indexRequest.setPipeline("attachment");
IndexResponse indexResponse= levelClient.index(indexRequest,RequestOptions.DEFAULT);
System.out.println(indexResponse);
return indexResponse;
}
? 3、其他代碼補充
? ?ES Config 配置類?
/**
* ES配置類
* author: 阿杰
*/
@Configuration
public class ElasticSearchClientConfig {
/**
* ES 地址:127.0.0.1:9200
*/
@Value("${es.ip}")
private String hostName;
@Bean
public RestHighLevelClient restHighLevelClient() {
String[] points = hostName.split(",");
HttpHost[] httpHosts = new HttpHost[points.length];
for (int i = 0; i < points.length; i++) {
String point = points[i];
httpHosts[i] = new HttpHost(point.split(":")[0], Integer.parseInt(point.split(":")[1]), "http");
}
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(httpHosts));
return client;
}
@Bean
public ElasticsearchUtil elasticSearchUtil() {
return new ElasticsearchUtil();
}
}
數(shù)據(jù)插入使用的實體類
/**
* author: 阿杰
*/
@Data
public class FileObj {
/**
* 用于存儲文件id
*/
String id;
/**
* 文件名
*/
String name;
/**
* 文件的type,pdf,word,or txt
*/
String type;
/**
* 數(shù)據(jù)插入時間
*/
String createTime;
/**
* 當前數(shù)據(jù)所屬人員
*/
String createBy;
/**
* 當前數(shù)據(jù)所屬人員的年齡
*/
int age;
/**
* 當前數(shù)據(jù)所屬人員的資產(chǎn)
*/
int money;
/**
* 文件轉(zhuǎn)化成base64編碼后所有的內(nèi)容。
*/
String content;
}
?四、補充一點
QueryBuilders.matchPhraseQuery
?和?QueryBuilders.matchQuery
?都是 Elasticsearch Java API 中用于構(gòu)建查詢的方法,它們在使用上有以下區(qū)別:
-
匹配方式不同:
-
matchPhraseQuery
?是短語匹配查詢,它會將輸入的文本作為一個短語進行匹配。短語匹配要求查詢的字段包含輸入的短語且順序一致。 -
matchQuery
?是多詞項匹配查詢,它會將輸入的文本根據(jù)分詞器進行分詞,并對分詞結(jié)果進行匹配。匹配結(jié)果是包含輸入的任意詞項的文檔。
-
-
查詢方式不同:
-
matchPhraseQuery
?使用短語查詢方式,它會對輸入的短語進行關(guān)鍵詞匹配,精確匹配所有詞項并保留順序。 -
matchQuery
?使用與布爾查詢相似的查詢方式,它會將輸入的文本進行分詞,并生成與分詞結(jié)果匹配的查詢條件。
-
-
分詞不同:
-
matchPhraseQuery
?不會對輸入的短語進行分詞,而是將輸入的短語作為整個短語進行匹配。 -
matchQuery
?會對輸入的文本進行分詞,并將分詞結(jié)果作為關(guān)鍵詞進行匹配。
-
下面是使用示例:
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryBuilder;
// 使用 matchPhraseQuery 進行短語匹配查詢
QueryBuilder matchPhraseQueryBuilder = QueryBuilders.matchPhraseQuery("fieldName", "input phrase");
// 使用 matchQuery 進行多詞項匹配查詢
QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("fieldName", "input text");
根據(jù)實際需求,選擇合適的查詢方式來構(gòu)建你的查詢條件。如果需要精確匹配全部詞項且保留順序,使用?matchPhraseQuery
;如果需要簡單的多詞項匹配,使用?matchQuery
。
完整代碼可通過:?完整代碼包下載
制作不易,給個小贊!文章來源:http://www.zghlxwxcb.cn/news/detail-744960.html
????????? ? ? ? ??文章來源地址http://www.zghlxwxcb.cn/news/detail-744960.html
到了這里,關(guān)于ElasticSearch 實現(xiàn) 全文檢索 支持(PDF、TXT、Word、HTML等文件)通過 ingest-attachment 插件實現(xiàn) 文檔的檢索的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!