弱いエンジニアの備忘録

自分的に気になった技術に関するメモや備忘録です。Elasticsearchに関する記事が多くなりそうです。

elasticsearchにoffice系ファイルやPDFを入れる

概要

elasticsearchにpdfやpptx,xlsxなどのファイルを入れる方法についてメモ。
ingest-attachment-pluginを使います。

バージョン情報など

macOS Sierra 10.12.5
elasticsearch-5.4.1

手順

1.ingest-attachment-pluginをインストールする。

$ bin/elasticsearch-plugin install ingest-attachment
-> Downloading ingest-attachment from elastic
[=================================================] 100%   
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@     WARNING: plugin requires additional permissions     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
* java.lang.RuntimePermission getClassLoader
* java.lang.reflect.ReflectPermission suppressAccessChecks
* java.security.SecurityPermission createAccessControlContext
* java.security.SecurityPermission insertProvider
* java.security.SecurityPermission putProviderProperty.BC
See http://docs.oracle.com/javase/8/docs/technotes/guides/security/permissions.html
for descriptions of what these permissions allow and the associated risks.

Continue with installation? [y/N]y
-> Installed ingest-attachment

2. インストールできたか確認。

$ bin/elasticsearch-plugin list                     
analysis-kuromoji
ingest-attachment ← ある
ingest-geoip
x-pack

3. elasticsearchを起動する。

$ bin/elasticsearch

4. ingest-nodeのpipelineを登録する。

f:id:shin0higuchi:20170606012308p:plain
ここでは"attachment"という名前のpipelineを登録しています。
"message" fieldにbase64エンコードされたファイル情報が入っていれば、ファイルの内容が抽出されます。

5. logstashを使ってファイルをelasticsearchに入れる。

今回使う設定ファイルは下記の通り。

$ cat attachment.conf 
input{
  stdin{}
}
output{
  elasticsearch{
    hosts => ["localhost:9200"]
    index => "attachment_test"
    pipeline => "attachment" #ここにpipelineの名前入れる。
  }
}

Logstashを実行してみましょう。

$ cat ../elasticsearch-5.4.0/base64.pptx| bin/logstash -f attachment.conf

6. kibanaからドキュメントを確認する。

f:id:shin0higuchi:20170606013457p:plain
検索してみると、しっかり内容が抽出されていることがわかります。

まとめ

  • base64エンコードして、ingest-attachment pluginを使えばofficeなども検索できる。
  • pptxでの例をあげたが、pdfやxlsxなども同様の手順でindexing可能。
  • 本当はbase64エンコードされたfieldは別のprocessorでremoveしたほうが良さそう。
  • 日本語検索をしっかりかけたい場合などはkuromoji辞書をattachment.contentに適用すると良い。