上云无忧 > 文档中心 > 百度智能云Elasticsearch - ingest attachment插件
Elasticsearch
百度智能云Elasticsearch - ingest attachment插件

文档简介:
简介: Elasticsearch的ingest attachment插件可以将常用格式的文件作为附件写入Index。ingest attachment插件通过使用Apache Tika来提取文件,支持的文件格式有TXT、DOC、PPT、XLS和PDF等。 注意:源字段必须是base64编码的二进制。
*此产品及展示信息均由百度智能云官方提供。免费试用 咨询热线:400-826-7010,为您提供专业的售前咨询,让您快速了解云产品,助您轻松上云! 微信咨询
  免费试用、价格特惠

简介

Elasticsearch的ingest attachment插件可以将常用格式的文件作为附件写入Index。ingest attachment插件通过使用Apache Tika来提取文件,支持的文件格式有TXT、DOC、PPT、XLS和PDF等。 注意:源字段必须是base64编码的二进制。

ingest attachment的pipeline参数含义

Name 是否必须 Default Description
field yes - 从这个字段中获取base64编码
target_field no attachment 用于保留attachment信息,主要用于多附件的情况
indexed_chars no 100000 限制字段的最大保存字符数。-1为无限制。
indexed_chars_field no - 可以从数据中设定的字段取到indexed_chars限制的值。
properties no 全属性 选择需要存储的属性。例如 content, title, name, author, keywords, date, content_type, content_length, language
ignore_missing no false 如果使用true,并且 field 不存在, 则会忽略附件直接写入doc;否则则会报错。

使用方法:

单附件

1、创建pipeline

PUT _ingest/pipeline/attachment
{
  "description" : "Extract attachment information",
  "processors" : [
    {
      "attachment" : {
        "field" : "data"
      }
    }
  ]
}

2、向Elasticsearch中写入数据

我们可以通过两种方式将附件写入Elasticsearch:

(1)直接写入附件的base64编码

POST my_index/_doc?pipeline=attachment
{
  "filename": "test.docx",
  "data": "5rWL6K+V5paH5Lu25pWw5o2udGVzdA=="
}

其中,5rWL6K+V5paH5Lu25pWw5o2udGVzdA==是附件内容转化为base64的结果。

(2)通过Linux的curl命令直接将附件写入Elasticsearch

curl -u user:pwd -H 'Content-Type: application/json' -XPOST IP:PORT/my_index/_doc?pipeline=attachment -d'
{
  "filename": "test.docx",
  "data": "'`base64 -w 0 /test_path/test.docx | perl -pe 's/\n/\\n/g'`'"
}'

通过这种方式,test.docx文件可以直接写入Elasticsearch。

3、查询数据

GET my_index/_search
{
   "query": {
      "match": {
         "attachment.content": "test"
      }
   }
}

得到结果如下:

{
   "took": 292,
   "timed_out": false,
   ...
         {
            "_index": "my_index",
            "_type": "_doc",
            "_id": "RL_1CXMByfR8Yao01Qs9",
            "_score": 1.9692057,
            "_source": {
               "filename": "test.docx",
               "data": "5rWL6K+V5paH5Lu25pWw5o2udGVzdA==",
               "attachment": {
                  "content_type": "text/plain; charset=UTF-8",
                  "language": "lt",
                  "content": "测试文件数据test",
                  "content_length": 10
               }
            }
         }
   ...
}

限制附件内容长度

1、创建pipeline

PUT _ingest/pipeline/attachment
{
  "description" : "Extract attachment information",
  "processors" : [
    {
      "attachment" : {
        "field" : "data",
        "indexed_chars" : 6,
        "indexed_chars_field" : "my_field_size"
      }
    }
  ]
}

2、写入数据

POST /my_index/_doc?pipeline=attachment
{
  "filename": "test.docx",
  "data": "5rWL6K+V5paH5Lu25pWw5o2udGVzdA=="
}
POST /my_index/_doc?pipeline=attachment
{
  "filename": "test2.docx",
  "my_field_size": 2
  "data": "5rWL6K+V5paH5Lu25pWw5o2udGVzdA=="
}

我们写入两个带有附件的doc,附件内容都是:测试文件数据test。其中第二个doc,我们将indexed_chars_field设定的字段my_field_size的值设定为2

注:这里只列举了直接写入base64的方式。

3、查询数据

GET my_index/_search
{
   "query": {
      "match_all": {}
   }
}

得到结果如下:

{
  "took": 292,
  "timed_out": false,
  ...
       {
        "_index": "my_index",
        "_type": "_doc",
        "_id": "RL_1CXMByfR8Yao01Qs9",
        "_score": 1.9692057,
        "_source": {
          "filename": "test.docx",
          "data": "5rWL6K+V5paH5Lu25pWw5o2udGVzdA==",
          "attachment": {
            "content_type": "text/plain; charset=UTF-8",
            "language": "lt",
            "content": "测试文件数据",
            "content_length": 6
          }
        }
      },
      {
        "_index": "my_index",
        "_type": "_doc",
        "_id": "RL_1CXMByfR8Yao01Qs9",
        "_score": 1.9692057,
        "_source": {
          "filename": "test2.docx",
          "my_field_size": 2,
          "data": "5rWL6K+V5paH5Lu25pWw5o2udGVzdA==",
          "attachment": {
            "content_type": "text/plain; charset=UTF-8",
            "language": "lt",
            "content": "测试",
            "content_length": 2
          }
        }
      }
  ...
}

可以看到,第一个doc,附件内容保留了6个字符,而第二个文档,保留了2个字符。

多附件

1、创建pipeline

PUT _ingest/pipeline/attachment
{
  "description" : "Extract attachment information from arrays",
  "processors" : [
    {
      "foreach": {
        "field": "attachments",
        "processor": {
          "attachment": {
            "target_field": "_ingest._value.attachment",
            "field": "_ingest._value.data"
          }
        }
      }
    }
  ]
}

需要注意的是,多附件的情况下,field和target_field必须要写成_ingest._value.*,否则不能匹配正确的字段。

2、写入多附件数据

POST /my_index/_doc?pipeline=attachment
{
  "attachments" : [
    {
      "filename" : "ipsum.txt",
      "data" : "dGhpcyBpcwpqdXN0IHNvbWUgdGV4dAo="
    },
    {
      "filename" : "test.txt",
      "data" : "VGhpcyBpcyBhIHRlc3QK"
    }
  ]
}

注:这里只列举了直接写入base64的方式。

3、查询数据

GET my_index/_search
{
   "query": {
      "match_all": {}
   }
}

得到结果如下:

{
  "took": 292,
  "timed_out": false,
  ...
      {
        "_index": "my_index",
        "_id": "RL_1CXMByfR8Yao01Qs9",
        "_version": 1,
        "_seq_no": 50,
        "_primary_term": 1,
        "found": true,
        "_source": {
          "attachments": [
            {
              "filename": "ipsum.txt",
              "data": "dGhpcyBpcwpqdXN0IHNvbWUgdGV4dAo=",
              "attachment": {
                "content_type": "text/plain; charset=ISO-8859-1",
                "language": "en",
                "content": "this is\njust some text",
                "content_length": 24
              }
            },
            {
              "filename": "test.txt",
              "data": "VGhpcyBpcyBhIHRlc3QK",
              "attachment": {
                "content_type": "text/plain; charset=ISO-8859-1",
                "language": "en",
                "content": "This is a test",
                "content_length": 16
              }
            }
          ]
        }
      }
  ...
}
相似文档
  • 背景: 词干提取是通过简化他们的词根形式来扩大搜索的范围,同义词通过相关的观念和概念来扩大搜索范围。 也许没有文档匹配查询 “英国女王“ ,但是包含 “英国君主” 的文档可能会被认为是很好的匹配。用户搜索 “美国” 并且期望找到包含 美利坚合众国 、 美国 、 美洲 、或者 USA 的文档。
  • 插件介绍: 简繁体转换插件可以帮助用户完成简体中文与繁体中文的转换。用户可以通过该插件的转换功能,使用中文繁体关键字搜索出包含对应中文简体的索引数据,同样也可以使用中文简体关键字搜索出包含对应中文繁体的索引数据。
  • 当用户需要使用自研插件或百度智能云Elasticsearch默认插件中不包含的开源插件时,可通过百度智能云Elasticsearch的自定义插件上传与安装功能,安装并使用插件。本文介绍具体的操作方法。
  • 百度智能云Elasticsearch 提供监控指标、报警和实例组功能。 监控指标:为运行中的集群提供多项监控指标,用户可根据监控指标,实时了解集群的运行状况,及时处理潜在风险,保障集群稳定运行。
  • 用户在接入百度智能云Elasticsearch服务时,如需要百度智能云Elasticsearch服务间进行数据迁移或自建Elasticsearch服务数据迁移至百度智能云 Elasticsearch,可以根据自己的业务需求选择合适的迁移方案。本文介绍各迁移方案适用的场景,帮助您根据业务选择合适的场景进行迁移。
官方微信
联系客服
400-826-7010
7x24小时客服热线
分享
  • QQ好友
  • QQ空间
  • 微信
  • 微博
返回顶部