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

これら 3 つのコマンドはすべて非常に優れています。しかし、このコマンドを入力すると:

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でエンコードする方法を知りたいです(もちろん、2番目のコマンドを入力してESでインデックスを作成する前に、base64コマンドを入力してファイルをエンコードしたくありません)。あなたの答えとして、あなたのコマンドを実行するために「Perlライブラリ」のようなものをインストールする必要がありますか?

4

4 に答える 4

4

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
于 2013-11-18T11:15:43.333 に答える
3

attachmentまず、プラグインがインストールされているかどうかを指定しません。そうでない場合は、次の方法で行うことができます。

./bin/plugin -install mapper-attachments

プラグインをロードするには、ElasticSearch を再起動する必要があります。

次に、上記と同様に、フィールドを type にマッピングしますattachment

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

ドキュメントのインデックスを作成しようとするときは、ファイルのコンテンツを Base64 でエンコードする必要があります。base64これは、コマンド ライン ユーティリティを使用してコマンド ラインで実行できます。ただし、正当な JSON にするためには、新しい行をエンコードする必要もあります。これは、base64Perlからの出力をパイプすることで実行できます。

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 添付ファイルの種類を参照してください。

于 2011-08-20T10:49:01.007 に答える
0

これは完全なシェル スクリプトの実装です。

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'"
}'
于 2013-02-02T03:50:18.490 に答える
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}
于 2015-10-12T11:35:55.157 に答える