2011-08-18 32 views
3

我在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: [[email protected]; 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: [[email protected]; line: 1, column: 132]]; ", 
    "status": 400 
} 

如果I型

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: [[email protected]; line: 1, column: 137]]; ", 
    "status": 400 
} 

如何將附加文件用於ES以便ES可以對其進行索引?


感謝您的回答。我輸入這些命令時已經安裝的附件插件。文本文件的內容使用Base64編碼,因此我不再編碼它。如果我不使用該文件的路徑,但直接使用它的內容在基地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庫」之類的東西來執行您的命令?

+0

是你可以做的。使用您正在編程的任何語言的Base64編碼器。我使用命令行版本,因爲您的示例使用來自命令行的curl – DrTech

回答

3

首先,您不指定是否安裝了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瞭解更多信息。

+0

當我輸入命令時: curl -XPOST'http:// localhost:9200/test_idx_1x/test_mapping_1x/1bis4'-d'{「post_date」:「 2009-11-15T14:12:12「,」message「:」嘗試彈性搜索「,」name「:」N2「,」my_attachments「:''base64 /inf/bd/tran_chi/test4.txt |我已經收到這條消息: {「error」:「MapperParsingException [Failed to parse];嵌套:JsonParseException [意外的字符('''')'' T'(code 84)):預計在[Source:[B @ 8aeedc; line:1,column:1]處有一個有效值(數字,字符串,數組,對象,'true','false'或'null' 134]];「,」status「:400} –

+0

此時,我必須始終使用base64命令在Base64中對文件的內容進行編碼。然後我輸入2nd commnand以便將此base64編碼的內容發佈到彈性搜索中的字段my_attachment。 我不知道如何在命令行中只鍵入一個命令來爲ES中的附件編制索引。 無論如何感謝您 –

+0

我在試圖運行post命令時遇到'''-bash:/ usr/bin/curl:參數列表太長'''錯誤。 '''curl'''命令將單引號的文本當作兩個不同的參數來處理。 –

0

這是一個完整的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'" 
}' 
0

有一個替代的解決方案 - 插件在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}