8. Elasticsearchの導入

この章では、Senju/QA 全文検索サーバーの稼働に必要なソフトウェアである、Elasticsearchを導入するための手順について説明します。

8.1. Elasticsearchの概要

Elasticsearchは、多様なタイプのデータを蓄積し、検索および分析を行うための、分散型の検索/分析エンジンです。 商用利用可能なオープンソースライセンスであるApache 2.0 licenseで提供されています。

Senju/QAではElasticsearchと連携して、サービスマネジメントプロセスの実現において、各プロセスの高度な検索や、 類似する事例のレコメンドを行えるようにします。

8.2. 対象バージョン

Senju/QA 全文検索サーバーではElasticsearch 7.16.2 を使用します。 ここでは、Elasticsearch 7.16.2を例として導入手順を説明します。

注釈

サポート対象となるElasticsearchのバージョンについては リリースノート を参照してください。

8.3. Elasticsearchのインストール

Windows

  1. インストール用のzipファイルを入手して、インストールするサーバーの任意のフォルダに格納します。

    ダウンロードURL:
    対象ファイル:
    • elasticsearch-7.16.2-windows-x86_64.zip

    (ここでは elasticsearch-7.16.2-x86_64.zipC:\Temp 配下に格納した場合の手順を例として説明します。ほかのフォルダに格納した場合は適宜各コマンドで指定するパスを変更してください。)

  2. インストールするサーバーに管理者権限のアカウントでログインします。

  3. zipファイルをCドライブ直下に展開します。
    PS C:\> Expand-Archive -Path C:\Temp\elasticsearch-7.16.2-windows-x86_64.zip -Destination C:\
    PS C:\> Rename-Item C:\elasticsearch-7.16.2 C:\elasticsearch
    

    注釈

    C:\elasticsearch フォルダが作成されます。

  4. ElasticsearchをWindowsのサービスとして登録します。
    PS C:\> cd C:\elasticsearch\bin
    PS C:\elasticsearch\bin> .\elasticsearch-service.bat install
    
  5. 自動起動の設定を行います。
    PS C:\elasticsearch\bin> Set-Service -Name 'elasticsearch-service-x64' -StartupType 'Automatic'
    PS C:\elasticsearch\bin> Get-Service -Name 'elasticsearch-service-x64' | select -property status,name,starttype
    
     Status Name                      StartType
    ------ ----                      ---------
    Running elasticsearch-service-x64 Automatic
    

    注釈

    Windows管理ツール[サービス]から設定することもできます。
    Elasticsearch 7.16.2 (elasticsearch-service-x64) を右クリックし、コンテキストメニューの[プロパティ]をクリックします。
    [全般]タブで[スタートアップの種類]を 自動 に設定します。

Linux

  1. インストール用のrpmファイルを入手して、インストールするサーバーの任意のディレクトリに格納します。

    ダウンロードURL:
    対象ファイル:
    • contribs/elasticsearch/elasticsearch-7.16.2-x86_64.rpm

    (ここでは contribs/elasticsearch/elasticsearch-7.16.2-x86_64.rpm/tmp/ 配下に格納した場合の手順を例として説明します。ほかのディレクトリに格納した場合は適宜各コマンドで指定するパスを変更してください。)

  2. インストールするサーバーに管理者権限のアカウントでログインします。

  3. rpmコマンドを実行してパッケージをインストールします。
    # rpm -ivh /tmp/elasticsearch-[0-9]*.rpm
    
  4. 自動起動の設定を行います。
    # systemctl daemon-reload
    # systemctl enable elasticsearch
    

8.4. Elasticsearchの設定

  1. Elasticsearchの設定ファイルを編集します。
    対象ファイル:
    • Windows

      • C:\elasticsearch\config\elasticsearch.yml

    • Linux

      • /etc/elasticsearch/elasticsearch.yml

    1. cluster.namesenjuqa-cluster に設定します。
      #cluster.name: my-application
      cluster.name: senjuqa-cluster
      
    2. network.host0.0.0.0 に指定します。
      #network.host: 192.168.0.1
      network.host: 0.0.0.0
      
    3. cluster.initial_master_nodes全文検索サーバーのホスト名 に設定します。
      #cluster.initial_master_nodes: ["node-1", "node-2"]
      cluster.initial_master_nodes: ["senjuqa"]
      

    (ここでは 全文検索サーバーのホスト名senjuqa の場合を例として説明します。ほかの場合はコマンドを変更してください。)

  2. Elasticsearchのログ設定ファイルを編集します。
    対象ファイル:
    • Windows

      • C:\elasticsearch\config\log4j2.properties

    • Linux

      • /etc/elasticsearch/log4j2.properties

    1. 以下の設定を変更します。
      #appender.rolling.strategy.action.condition.nested_condition.type = IfAccumulatedFileSize
      appender.rolling.strategy.action.condition.nested_condition.type = IfLastModified
      #appender.rolling.strategy.action.condition.nested_condition.exceeds = 2GB
      appender.rolling.strategy.action.condition.nested_condition.age = 7D
      

    注釈

    この設定を追加することで、7日間変更がない古いログを削除するようになります。ログ削除の設定を追加しない場合、デフォルトでは古いログファイルが残り続けるため、 /var/log/elasticsearch ディレクトリのディスク容量を圧迫してしまう可能性があります。

8.5. Elasticsearchの日本語アナライシスプラグインのインストール

  1. Elasticsearchの日本語アナライシスプラグインインストール用zipファイルを取得し、インストールするサーバーの任意のフォルダに格納します。

    ダウンロードURL:
    対象ファイル:
    • analysis-kuromoji-7.16.2.zip

    (ここでは analysis-kuromoji-7.16.2.zipC:\Temp (Windows) あるいは /tmp (Linux) 配下に格納した場合の手順を例として説明します。ほかのディレクトリに格納した場合は適宜各コマンドで指定するパスを変更してください。)

  2. インストールする全文検索サーバーに管理者アカウントでログインします。

  3. 以下のコマンドを使用して、 analysis-kuromoji というプラグインをElasticsearchにインストールします。

    Windows

    PS C:\> cmd /c C:\elasticsearch\bin\elasticsearch-plugin install file:///C:/Temp/analysis-kuromoji-7.16.2.zip
    

    Linux

    # /usr/share/elasticsearch/bin/elasticsearch-plugin install file:///tmp/analysis-kuromoji-7.16.2.zip
    
  4. 以下のレスポンスが返ってきており、登録が成功したことを確認します。
    -> Installed analysis-kuromoji
    

8.6. インストール確認

  1. Elasticsearchを起動します。

    Windows

    PS C:\> Start-Service -Name 'elasticsearch-service-x64'
    

    注釈

    Windows管理ツール[サービス]から開始することもできます。
    Elasticsearch 7.16.2 (elasticsearch-service-x64) を右クリックし、コンテキストメニューの[開始]をクリックします。

    Linux

    # systemctl start elasticsearch
    
  2. 以下のコマンドを実行して稼働していることを確認します。

    Windows

    PS C:\> $r = Invoke-RestMethod -Method Get -Uri 'http://localhost:9200'
    PS C:\> $r | ConvertTO-JSON
    {
        "name" : "uFMCqgF",
        "cluster_name":  "senjuqa-cluster",
        "cluster_uuid":  "FnLh0dDaTdCyB93rbMmE0g",
        "version":  {
                        "number":  "7.16.2",
                        "build_flavor":  "default",
                        "build_type":  "zip",
                        "build_hash":  "5b38441b16b1ebb16a27c107a4c3865776e20c53",
                        "build_date":  "2021-12-11T00:29:38.865893768Z",
                        "build_snapshot":  false,
                        "lucene_version":  "8.10.1",
                        "minimum_wire_compatibility_version":  "6.8.0",
                        "minimum_index_compatibility_version":  "6.0.0-beta1"
                    },
        "tagline":  "You Know, for Search"
    }
    

    Linux

    # curl -XGET 'http://localhost:9200/'
    {
    "name" : "uFMCqgF",
    "cluster_name" : "senjuqa-cluster",
    "cluster_uuid" : "b-NloIg9Qu-Nqibstu2eyQ",
    "version" : {
        "number" : "7.16.2",
        "build_flavor" : "default",
        "build_type" : "rpm",
        "build_hash" : "083627f112ba94dffc1232e8b42b73492789ef91",
        "build_date" : "2020-09-01T21:22:21.964974Z",
        "build_snapshot" : false,
        "lucene_version":  "8.10.1",
        "minimum_wire_compatibility_version" : "6.8.0",
        "minimum_index_compatibility_version" : "6.0.0-beta1"
    },
    "tagline" : "You Know, for Search"
    }
    

8.7. Elasticsearchの検索設定

Windows

  1. Invoke-RestMethodコマンドレットを使用して、IndexTemplate(kuromoji_setting.json)をElasticsearchサーバーに登録します。
    PS C:\> cd C:\Temp\Senju_Quick_Assistant\conf\elasticsearch
    PS C:\Temp\Senju_Quick_Assistant\conf\elasticsearch> $r = Invoke-RestMethod -Method Put -Uri 'http://localhost:9200/senjuqa_pages' -ContentType 'application/json' -InFile kuromoji_setting.json
    PS C:\Temp\Senju_Quick_Assistant\conf\elasticsearch> $r | ConvertTO-JSON
    
  2. 以下のレスポンスが返ってきており、登録が成功したことを確認します。
    {
        "acknowledged":  true,
        "shards_acknowledged":  true,
        "index":  "senjuqa_pages"
    }
    
  3. Invoke-RestMethodコマンドレットを使用して、MappingTemplate(senjuqa_index_mapping.json)をElasticsearchサーバーに登録します。
    PS C:\Temp\Senju_Quick_Assistant\conf\elasticsearch> $r = Invoke-RestMethod -Method Put -Uri 'http://localhost:9200/senjuqa_pages/_mapping/type?include_type_name=true' -ContentType 'application/json' -InFile senjuqa_index_mapping.json
    PS C:\Temp\Senju_Quick_Assistant\conf\elasticsearch> $r | ConvertTO-JSON
    
  4. 以下のレスポンスが返ってきており、登録が成功したことを確認します。
    {
      "acknowledged" : true
    }
    

Linux

  1. curlコマンドを使用して、IndexTemplate(senjuqa_index_mapping.jsonkuromoji_setting.json )をElasticsearchサーバーに登録します。
    # curl -H "Content-Type: application/json" -X PUT 'http://localhost:9200/senjuqa_pages' -d @/tmp/Senju_Quick_Assistant/conf/elasticsearch/kuromoji_setting.json
    # curl -H "Content-Type: application/json" -X PUT 'http://localhost:9200/senjuqa_pages/_mapping/type?include_type_name=true' -d @/tmp/Senju_Quick_Assistant/conf/elasticsearch/senjuqa_index_mapping.json
    
  2. 以下のレスポンスが返ってきており、登録が成功したことを確認します。
    {
      "acknowledged" : true
    }