5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Officeドキュメントの中身検索をElasticSearch(mapper-attachmentsプラグイン)でやってみた

Posted at

Officeドキュメントの中身検索をElasticSearchで行ってみた

※ 事前にkuromoji もインストールしていくこと

今回ポイントになる、mapper-attachment プラグインをインストール

mapper-attachment とは、
.pdfや.pptxに対応するためのプラグイン
これらに対応させるApache Tikaを扱うためのattachmentタイプを使えるようにしてくれる

./bin/elasticsearch-plugin install mapper-attachments

-> Downloading mapper-attachments 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 mapper-attachments

kibanaでコマンド実行

インストールしたプラグインの確認

get /_nodes/plugins

Index作成
※ フィールドはcontent のみ

delete /search_doc
PUT /search_doc
{
   "settings": {
       "index": {
       "number_of_shards": 1,
       "number_of_replicas": 0,
        "analysis": {
           "tokenizer": {
               "kuromoji_search": {
                   "type": "kuromoji_tokenizer",
                    "mode": "search"
               }
           },
            "analyzer": {
               "default": {
                   "tokenizer": "kuromoji_search",
                   "filter": [
                       "kuromoji_baseform",
                           "kuromoji_part_of_speech",
                           "cjk_width",
                           "stop",
                           "kuromoji_stemmer",
                           "lowercase"
                       ]
                   }
               }
           }
       }
   },
   "mappings": {
       "series" : {
            "properties" : {
                "file" : {
                    "type" : "attachment",
                    "fields" : {
                        "content" : { "type": "text",
                                      "term_vector": "with_positions_offsets",
                                      "store": true
                                    }
                    }
                }
            }
        }
   }
}

シェルスクリプトでファイルとIndexing.

※ 実行場所にtest.pptxを置いておかないとダメですよ。

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

検索実行

GET /search_doc/_search?pretty=true
{
  "query": {
    "match": {
      "file.content": "テスト"
    }
  }
}
5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?