1.安装插件
#预处理
./bin/elasticsearch-plugin install ingest-attachment
#分词
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/...这里找你的版本
2.定义文本抽取管道
PUT /_ingest/pipeline/attachment
{
"description": "Extract attachment information",
"processors": [
{
"attachment": {
"field": "content",
"ignore_missing": true
}
},
{
"remove": {
"field": "content"
}
}
]
}
在attachment中指定要过滤的字段为content,所以写入Elasticsearch时需要将文档内容放在content字段
3.建立文档结构映射
PUT /docwrite
{
"mappings": {
"properties": {
"id":{
"type": "keyword"
},
"name":{
"type": "text",
"analyzer": "ik_max_word"
},
"type":{
"type": "keyword"
},
"attachment": {
"properties": {
"content":{
"type": "text",
"analyzer": "ik_smart"
}
}
}
}
}
}
在 ElasticSearch 中增加了attachment字段,这个字段是attachment命名pipeline抽取文档附件中文本后自动附加的字段。这是一个嵌套字段,其包含多个子字段,包括抽取文本 content 和一些文档信息元数据。
同是对文件的名字name指定分析器analyzer为 ik_max_word,以让 ElasticSearch在建立全文索引时对它们进行中文分词。
4.上传
将文件流转化为base64后提交到es