索引附件文件进行弹性搜索 [英] Indexing Attachment file to elastic search

查看:442
本文介绍了索引附件文件进行弹性搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经输入了这个命令来在Elasticsearch中索引文档

I have typed this command to index a document in 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"
}'

所有这三个命令都是非常商品。
但是当我输入这个命令时:

All these three commands are very goods. But when I type this command:

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
}

如果我输入



if I type

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"
}'

我收到错误,我可以理解它

I receive error. I can understand it

{
  "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可以索引?

How can I use attach files to ES so that ES can index it?

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

Thanks for your answer. That attachment plugin I have already installed when I type these commands. The content of the text file is encoded in Base64, so I don't encode it anymore. If I don't use the file's path but directly use its contents in Base 64, ex.

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"
}'

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

all is good, I have already succeeded in posting file and searching its content later.

但是,如果我用路径的文件替换它,我获得了否定的结果,所以我想知道如何在命令行中编码Base64一个文件,在命令o f ES索引(当然,我不想键入base64命令来编码一个文件,然后再输入第二个命令在ES中进行索引)。作为您的答案,我必须安装像Perl库这样的执行命令?

But if I replace it with path's file, I obtained negative results. So I want to know how to encode Base64 a file in command line,in the command of ES indexing (of course, I don't want to type base64 command to encode a file before typing 2nd command to indexing it in ES). As your answer, do I have to installed something like "Perl library" to execute your command?

推荐答案

http://es-cn.medcl.net/tutorials/ 2011/07/18 / attachment-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

这篇关于索引附件文件进行弹性搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆