【问题标题】:Indexing Attachment file to elastic search将附件文件索引到弹性搜索
【发布时间】:2011-10-30 00:24:57
【问题描述】:

我已键入此命令来索引 Elasticsearch 中的文档

创建索引

curl -X PUT "localhost:9200/test_idx_1x"

创建映射

curl -X PUT "localhost:9200/test_idx_1x/test_mapping_1x/_mapping" -d '{
  "test_mapping_1x": {
    "properties": {
      "my_attachments": {
        "type": "attachment"
      }
    }
  }
}'

索引此文档

curl -XPUT 'http://localhost:9200/test_idx_1x/test_mapping_1x/4' -d '{
  "post_date": "2009-11-15T14:12:12",
  "message": "test Elastic Search",
  "name": "N1"
}'

这三个命令都非常好。 但是当我输入这个命令时:

curl -XPOST 'http://localhost:9200/test_idx_1x/test_mapping_1x/1' -d '{
  "post_date": "2009-11-15T14:12:12",
  "message": "trying out Elastic Search",
  "name": "N2",
  "my_attachments": {
    "type": "attachment",
    "_content_type": "text/plain",
    "file": "http://localhost:5984/my_test_couch_db_7/ID2/test.txt"
  }
}'

我收到此错误消息:

{
  "error": "NullPointerException[null]",
  "status": 500
}

我把它改成;

curl -XPOST 'http://localhost:9200/test_idx_1x/test_mapping_1x/1bis' -d '{
  "post_date": "2009-11-15T14:12:12",
  "message": "trying out Elastic Search",
  "name": "N2",
  "my_attachments": {
    "type": "attachment",
    "_content_type": "text/plain",
    "_name": "/inf/bd/my_home_directory/test.txt"
  }
}'

curl -XPUT 'http://localhost:9200/test_idx_1x/test_mapping_1x/1' -d '{
  "post_date": "2009-11-15T14:12:12",
  "message": "trying out Elastic Search",
  "name": "N2",
  "my_attachments": {
    "file": "http://localhost:5984/my_test_couch_db_7/ID2/test.txt"
  }
}'

curl -XPUT 'http://localhost:9200/test_idx_1x/test_mapping_1x/1' -d '{
  "post_date": "2009-11-15T14:12:12",
  "message": "trying out Elastic Search",
  "name": "N2",
  "my_attachments": {
    "file": "http://localhost:5984/my_test_couch_db_7/ID2/test.txt",
    "_content_type": "text/plain"
  }
}'

输出是同样的错误。

我就这样改

curl -XPUT 'http://localhost:9200/test_idx_1x/test_mapping_1x/1' -d '{
  "user": "kimchy",
  "post_date": "2009-11-15T14:12:12",
  "message": "trying out Elastic Search",
  "name": "N2",
  "my_attachments": {
    "file": "http://localhost:5984/my_test_couch_db_7/ID2/test.txt",
    "_content_type": "text/plain",
    "content": "... base64 encoded attachment ..."
  }
}'

错误是

{
  "error": "MapperParsingException[Failed to parse]; nested: JsonParseException[Failed to decode VALUE_STRING as base64 (MIME-NO-LINEFEEDS): Illegal character '.' (code 0x2e) in base64 content\n at [Source: [B@159b3; line: 1, column: 241]]; ",
  "status": 400
}

curl -XPUT 'http://localhost:9200/test_idx_1x/test_mapping_1x/1' -d '{
  "post_date": "2009-11-15T14:12:12",
  "message": "trying out Elastic Search",
  "name": "N2",
  "my_attachments": "http://localhost:5984/my_test_couch_db_7/ID2/test.txt"
}'

我收到此错误消息:

{
  "error": "MapperParsingException[Failed to parse]; nested: JsonParseException[Unexpected character ('h' (code 104)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: [B@1ae9565; line: 1, column: 132]]; ",
  "status": 400
}

如果我输入

curl -XPUT 'http://localhost:9200/test_idx_1x/test_mapping_1x/1' -d '{
  "post_date": "2009-11-15T14:12:12",
  "message": "trying out Elastic Search",
  "name": "N2",
  "my_attachments": "http://localhost:5984/my_test_couch_db_7/ID2/test.txt"
}'

我收到错误消息。我能理解

{
  "error": "MapperParsingException[Failed to parse]; nested: JsonParseException[Failed to decode VALUE_STRING as base64 (MIME-NO-LINEFEEDS): Illegal character ':' (code 0x3a) in base64 content\n at [Source: [B@1ffb7d4; line: 1, column: 137]]; ",
  "status": 400
}

如何使用附加文件到 ES 以便 ES 对其进行索引?


感谢您的回答。当我键入这些命令时,我已经安装了那个附件插件。文本文件的内容是用Base64编码的,所以我不再编码了。如果我不使用文件的路径,而是直接使用 Base 64 中的内容,例如

curl -XPUT 'http://localhost:9200/test_idx_1x/test_mapping_1x/' -d '{
  "post_date": "2009-11-15T14:12:12",
  "message": "trying out Elastic Search",
  "name": "N2",
  "my_attachments": "file's content string encoded in base64"
}'

一切都很好,我已经成功发布文件并稍后搜索其内容。

但是如果我用路径的文件替换它,我会得到否定的结果。所以我想知道如何在命令行中,在 ES 索引命令中对文件进行 Base64 编码(当然,我不想在输入第二个命令在 ES 中对文件进行索引之前输入 base64 命令来编码文件)。作为您的回答,我是否必须安装“Perl 库”之类的东西才能执行您的命令?

【问题讨论】:

  • 是的,你可以做到。使用您正在编程的任何语言的 Base64 编码器。我使用了命令行版本,因为您的示例使用命令行中的 curl

标签: indexing attachment elasticsearch


【解决方案1】:

首先,您没有指定是否安装了attachment 插件。如果没有,您可以这样做:

./bin/plugin -install mapper-attachments

您需要重新启动 ElasticSearch 才能加载插件。

然后,正如您在上面所做的那样,您将一个字段映射为具有attachment 类型:

curl -XPUT 'http://127.0.0.1:9200/foo/?pretty=1'  -d '
{
   "mappings" : {
      "doc" : {
         "properties" : {
            "file" : {
               "type" : "attachment"
            }
         }
      }
   }
}
'

当您尝试为文档编制索引时,您需要使用 Base64 对文件内容进行编码。您可以使用base64 命令行实用程序在命令行上执行此操作。但是,要成为合法的 JSON,您还需要对新行进行编码,您可以通过 Perl 将 base64 的输出管道化:

curl -XPOST 'http://127.0.0.1:9200/foo/doc?pretty=1'  -d '
{
   "file" : '`base64 /path/to/file | perl -pe 's/\n/\\n/g'`'
}
'

现在您可以搜索您的文件了:

curl -XGET 'http://127.0.0.1:9200/foo/doc/_search?pretty=1'  -d '
{
   "query" : {
      "text" : {
         "file" : "text to look for"
      }
   }
}
'

请参阅ElasticSearch attachment type 了解更多信息。

【讨论】:

  • 当我输入你的命令时: curl -XPOST 'localhost:9200/test_idx_1x/test_mapping_1x/1bis4' -d '{ "post_date" : "2009-11-15T14:12:12", "message" : "试用 Elastic Search", "name" : "N2","my_attachments" : 'base64 /inf/bd/tran_chi/test4.txt | perl -pe 's/\n/\\n/g''}' 我已经收到这条消息:{"error":"MapperParsingException[Failed to parse]; 嵌套:JsonParseException[Unexpected character (' T'(代码 84)):在 [来源:[B@8aeedc;行:1,列: 134]]; ","状态":400}
  • 此时,我必须始终使用base64命令对文件内容进行Base64编码。然后我键入第二个命令,以便将此 base64 编码的内容发布到 Elastic 搜索中的 my_attachment 字段。我不知道如何在命令行中只键入一个命令来索引 ES 中的附件。还是谢谢你
  • 我在尝试运行 post 命令时收到 -bash: /usr/bin/curl: Argument list too long 错误。 curl 命令将单引号 ' 中的文本视为两个不同的参数。
【解决方案2】:

这是一个完整的shell脚本实现:

file_path='/path/to/file'
file=$(base64 $file_path | perl -pe 's/\n/\\n/g')
curl -XPUT "http://eshost.com:9200/index/type/" -d '{
    "file" : "content" : "'$file'"
}'

【讨论】:

    【解决方案3】:

    http://es-cn.medcl.net/tutorials/2011/07/18/attachment-type-in-action.html

    #!/bin/sh
    
    coded=`cat fn6742.pdf | perl -MMIME::Base64 -ne 'print encode_base64($_)'`
    json="{\"file\":\"${coded}\"}"
    echo "$json" > json.file
    curl -X POST "localhost:9200/test/attachment/" -d @json.file
    

    【讨论】:

      【解决方案4】:

      有一个替代解决方案 - http://elasticwarehouse.org 的插件。您可以使用 _ewupload? 上传二进制文件,读取新生成的 ID 并使用此参考更新您的不同索引。

      安装插件:

      plugin -install elasticwarehouseplugin -u http://elasticwarehouse.org/elasticwarehouse/elasticsearch-elasticwarehouseplugin-1.2.2-1.7.0-with-dependencies.zip
      

      重启集群,然后:

      curl -XPOST "http://127.0.0.1:9200/_ewupload?folder=/myfolder&filename=mybinaryfile.bin" --data-binary @mybinaryfile.bin
      

      示例响应:

      {"id":"nWvrczBcSEywHRBBBwfy2g","version":1,"created":true}
      

      【讨论】:

        猜你喜欢
        • 2021-03-18
        • 2015-12-18
        • 2014-10-11
        • 1970-01-01
        • 2016-10-08
        • 2016-10-24
        相关资源
        最近更新 更多