Elasticsearch

提供:Tsubopedia
2018年2月13日 (火) 08:04時点におけるAdmin (トーク | 投稿記録)による版 (→‎WordPressでElasticsearchを利用する設定)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)

Elasticsearchは、Elasticのオープンソースソリューションの1つで、Elasticsearchをはじめ、Logstash、Kibana、Beatsなどの、オープンソースプロジェクトを開発・支援している。これらのオープンソースソリューションは、あらゆる業界で課題とされている検索、ログ分析、解析に関する課題を解決する。

AWS(Amazon Linux)へのインストール

  • yumリポジトリの設定
rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
  • yumリポジトリの確認
vi /etc/yum.repos.d/elasticsearch.repo
[elasticsearch-6.x]
name=Elasticsearch repository for 6.x packages
baseurl=https://artifacts.elastic.co/packages/6.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=0
autorefresh=0
type=rpm-md

[elasticsearch-5.x]
name=Elasticsearch repository for 5.x packages
baseurl=https://artifacts.elastic.co/packages/5.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=0
autorefresh=0
type=rpm-md

[elasticsearch-2.x]
name=Elasticsearch repository for 2.x packages
baseurl=http://packages.elastic.co/elasticsearch/2.x/centos
gpgcheck=1
gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch
enabled=1

上記は、Elasticsearch 2.xをインストールする場合の例。現在、MediawikiのExtensionは1.28系が2.x以下をサポートし、1.29系は5.x以上をサポートしている。利用しているExtensionのバージョンとElasticsearchのバージョンを合わせる必要がある。

  • yumを利用したインストール
yum install elasticsearch
  • Elasticsearchの起動
service elasticsearch start
  • Elasticsearchの起動確認
curl 'http://localhost:9200/'
  • Kuromojiのインストール

Elasticsearch 2.xの場合

cd /usr/share/elasticsearch/
bin/plugin install analysis-kuromoji

Elasticsearch 5.xの場合

cd /usr/share/elasticsearch/
bin/elasticsearch-plugin install analysis-kuromoji

MediawikiでElasticsearchを利用する設定

  • 使用するExtensions
CirrusSearch
Elastica
  • Indexの作成

上記Extensionsをインストールする過程で、LocalSettings.phpに以下の設定を追加する。

Add this to LocalSettings.php:
 require_once( "$IP/extensions/Elastica/Elastica.php" );
 require_once( "$IP/extensions/CirrusSearch/CirrusSearch.php" );
 $wgDisableSearchUpdate = true;
  • 設定を保存したら、以下のコマンドを実行し、実行後、$wgDisableSearchUpdate = true;を削除する。
Now run this script to generate your elasticsearch index:
 php $MW_INSTALL_PATH/extensions/CirrusSearch/maintenance/updateSearchIndexConfig.php
  • 次に、以下のコマンドを実行し、実行後、$wgSearchType = 'CirrusSearch';をLocalSettings.phpへ追加する。
 php $MW_INSTALL_PATH/extensions/CirrusSearch/maintenance/forceSearchIndex.php --skipLinks --indexOnSkip
 php $MW_INSTALL_PATH/extensions/CirrusSearch/maintenance/forceSearchIndex.php --skipParse

Once that is complete add this to LocalSettings.php to funnel queries to ElasticSearch:
 $wgSearchType = 'CirrusSearch';
  • Index定期更新のためのcron設定(この例では30分おきに実行される)
vi /etc/crontab
0,30 * * * * root /usr/bin/php $mediawikiroot/maintenance/runJobs.php > /var/run/log/runJobs.log 2>&1

WordPressでElasticsearchを利用する設定

  • analyzer設定の変更
src/elasticsearch/Indexer.phpの'analyzer' => 'english'を'kuromoji'へ変更する。この変更はプラグインがバージョンアップされたらリセットされるため注意が必要。
if($props['type'] == 'string' && $props['index'] == 'analyzed'){
    $lang = Config::apply_filters('string_language', 'english');
    $props = array(
        'type' => 'multi_field',
        'fields' => array(
         $field => $props,
         $lang => array_merge($props,array(
        'analyzer' => 'kuromoji' /* $lang から'kuromoji'へ変更 */
                )
            )
        )
    );
}

Fantastic Elasticsearchが古いバージョンの場合

if($props['type'] == 'string' && $props['index'] == 'analyzed'){
    $props = array(
        'type' => 'multi_field',
        'fields' => array(
         $field => $props,
        'english' => array_merge($props,array(
        'analyzer' => 'kuromoji' /* 'english'から'kuromoji'へ変更 */
                )
            )
        )
    );
}

関連項目

検索の使い方