LoginSignup
3
4

More than 5 years have passed since last update.

OfficeドキュメントやPDFをクロールして、中身検索ができるようにする

Posted at

OfficeドキュメントやPDFをクロールして、中身検索ができるようにする

プラグインインストール

OfficeドキュメントやPDFを扱えるようにするためのプラグインをインストール

./bin/elasticsearch-plugin install mapper-attachments

日本語を扱えるようにするプラグインも入っていない方は、インストール

./bin/elasticsearch-plugin install analysis-kuromoji

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

get /_nodes/plugins

インデックス作成

※ 何度も繰り替えしていたので、DeleteInsert形式にしています。
  まずは、インデックス削除し、その後登録するコマンドです。

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",
                                   "store": "true",
                                   "term_vector":"with_positions_offsets"
                                  },
                      "title" : {"type" : "text", "store" : "true", "copy_to": "title"},
                      "date" : {"type" : "text", "store" : "true", "copy_to": "date"},
                      "author" : {"store" : "true"},
                      "keywords" : {"type" : "text", "store" : "true"},
                      "content_type" : {"type" : "text", "store" : "true", "copy_to": "content_type"},
                      "content_length" : {"type" : "text", "store" : "true", "copy_to": "content_length"},
                      "language" : {"store" : "true"}
                    }
                },
                "title":{
                  "type": "text",
                  "store" : "true"
                },
                "date":{
                  "type": "text",
                  "store" : "true"
                },
                "content_type":{
                  "type": "text",
                  "store" : "true"
                },
                "content_length":{
                  "type": "text",
                  "store" : "true"
                }
            }
        }
   }
}

ファイルクローリング

crowling.sh の名称でシェルスクリプトを作成し、
どうディレクトリにクローリングしたいファイルを配置する

#!/bin/sh

for file in `\find . -maxdepth 1 -type f ! -name '.DS_Store' ! -name 'crowling.sh' ! -name 'json.file'`; do

    coded=`cat $file | base64`
    filename=`echo $file | sed 's/\.[\/]//g'`
    json="{\"file\":\"${coded}\", \"title\":\"${filename}\"}"
    echo "$json" > json.file
    curl -X POST "localhost:9200/search_doc/series/" -d @json.file
    rm -rf json.file
done

クローリングされたファイル件数を確認

GET /search_doc/_count

ファイル検索をしてみる

GET /search_doc/_search
{
  "stored_fields": ["file.content_length","title", "file.title"], 
  "query": {
    "match": {
      "file.content": "test"
    }
  }
}

検索できた!!

3
4
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
3
4