【问题标题】:How to index a pdf file using Elasticsearch ingest-attachment plugin?如何使用 Elasticsearch 摄取附件插件索引 pdf 文件?
【发布时间】:2017-06-25 21:13:29
【问题描述】:

我必须使用 Elasticsearch 摄取插件在 pdf 文档中实现基于全文的搜索。当我尝试在 pdf 文档中搜索单词 someword 时,我得到一个空的命中数组。

//Code for creating pipeline

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

//Code for creating the index

PUT my_index/my_type/my_id?pipeline=attachment
{
   "filename" : "C:\\Users\\myname\\Desktop\\bh1.pdf",
   "title" : "Quick",
   "data": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0="

}

//Code for searching the word in pdf 

GET /my_index/my_type/_search
{
    "query": {
    "match": {
      "data" : {
        "query" : "someword"
    }
 }
}

【问题讨论】:

  • 如果您在 PDF 查看器中打开 PDF,是否可以在其中搜索“someword”并找到匹配项?
  • @Alcanzar 是的,它会搜索这个词。
  • 这看起来像 stackoverflow.com/questions/37861279/… 的副本——请注意,您的 PUT 语句正在为文件放置一个特定的“数据”。您需要使用 curl 或类似的东西来传递特定的文件数据。你输入的“数据”是Lorem ipsum dolor sit amet——如果你搜索 Lorem,你会找到一个结果
  • @Alcanzar 我通过在 Kibana 仪表板上运行 GET 搜索 Lorem 进行了验证。但仍然没有命中。
  • @Alcanzar 你能告诉我弹性搜索索引非结构化数据(如 pdf 文件)背后的理论吗?

标签: elasticsearch full-text-search elasticsearch-plugin


【解决方案1】:

当您使用第二个命令通过传递 Base64 编码的内容来索引文档时,文档将如下所示:

        {
           "filename": "C:\\Users\\myname\\Desktop\\bh1.pdf",
           "data": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0=",
           "attachment": {
              "content_type": "application/rtf",
              "language": "ro",
              "content": "Lorem ipsum dolor sit amet",
              "content_length": 28
           },
           "title": "Quick"
        }

所以您的查询需要查看 attachment.content 字段而不是 data 字段(仅用于在索引期间发送原始内容)

将您的查询修改为此,它将起作用:

POST /my_index/my_type/_search
{
   "query": {
      "match": {
         "attachment.content": {         <---- change this
            "query": "lorem"
         }
      }
   }
}

PS:发送有效载荷时使用POST 而不是GET

【讨论】:

  • 关于如何使用弹性搜索将 pdf 文件转换为 base64 编码文件的任何想法?
  • 我认为这应该是一个新问题,因为它与这个问题无关。
  • 你为什么使用 POST 而不是 GET ?后者对我来说很好
  • 这取决于您使用的 HTTP 客户端,但您应该绝不通过 GET 发送有效负载(= 不符合 HTTP)。在此处查看更详细的示例:stackoverflow.com/questions/34795053/…
  • @Ashley afaik 在数据发送到弹性搜索之前完成,因此您可以使用您正在使用的编程语言中存在的任何方法进行转换。
猜你喜欢
  • 2016-10-18
  • 2018-08-15
  • 2018-06-03
  • 2017-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-18
  • 1970-01-01
相关资源
最近更新 更多