elasticsearchプラグイン入門
-
Upload
shinsuke-sugaya -
Category
Technology
-
view
2.795 -
download
6
description
Transcript of elasticsearchプラグイン入門
elasticsearchプラグイン入門Solr APIプラグインでSolrを入れ替えてみよう
名前: 菅谷信介 所属: N2SM, Inc. オープンソース活動:
– Apache Portals、codehaus、Seasarプロジェクトなど– Fessプロジェクト運営などなど・・・
Twitter: @shinsuke_sugaya ブログ: http://www.chazine.com/
自己紹介
2
elasticsearch plugin入門 elasticsearch-solr-apiとは elasticsearch-solr-apiのデモ
アジェンダ
3
elasticsearchの機能をカスマイズする方法 プラグインのzipはjarをまとめたもの pluginコマンドでインストールする
● plugin -install ... unzipコマンドでpluginsディレクトリに展開でも良
い(と思う) プラグインを作ればいろいろとできる
● 基本的なところは簡単に作れる● いろいろとやるには情報が足りない…● すごいことしたいならコード読むしかない
eleasticsearch pluginとは
5
Analysis: Analyzer関連の変換系 River: データを取り込む系 Transport: 他とやりとりする系 Site: 統計情報などサイト表示系(作り方はちょっと例外的) Script: Pythonとか多言語実行系 その他: いろいろ…
プラグインの種類
6
es-plugin.propertiesを作成する Pluginクラスを作成する Moduleクラスを作成する 必要なコンポーネントを作る assembly用のxmlファイルを作る
(maven-assembly-pluginでzip化する)
・・・以上
プラグインの作り方
7
ディレクトリ構成図例
8
├── pom.xml└── src └── main ├── assemblies │ └── plugin.xml ├── java │ └── <package name> │ ├── plugin │ │ ├── ...Module.java │ │ └── ...Plugin.java │ └── rest │ └── ...RestAction.java └── resources └── es-plugin.properties ①
②
モジュールやサービスの登録や設定など行うPluginクラス
10
public class SolrPlugin extends AbstractPlugin { @Override public String name() { return "SolrPlugin"; }
@Override public String description() { return "This plugin provides Solr interface on the elasticsearch."; }
@Override public void processModule(final Module module) { if (module instanceof RestModule) { ((RestModule) module).addRestAction(SolrUpdateRestAction.class); ((RestModule) module).addRestAction(SolrSearchRestAction.class); } }
@Override public Collection<Class<? extends Module>> indexModules() { final Collection<Class<? extends Module>> modules = new ArrayList<Class<? extends Module>>(); modules.add(SolrIndexModule.class); return modules; }}
AbstractPluginを継承するAbstractPluginを継承する
Pluginクラスの例
11
@Overridepublic void processModule(final Module module) { if (module instanceof RestModule) { ((RestModule) module) .addRestAction(SolrUpdateRestAction.class); ((RestModule) module) .addRestAction(SolrSearchRestAction.class); }}
@Overridepublic Collection<Class<? extends Module>> indexModules() { Collection<Class<? extends Module>> modules = new ArrayList<Class<? extends Module>>(); modules.add(SolrIndexModule.class); return modules;}
HTTP等でリクエストを処理したい場合RestActionを登録
HTTP等でリクエストを処理したい場合RestActionを登録
サービス等のモジュールを作りコンポーネントを登録する
サービス等のモジュールを作りコンポーネントを登録する
コンポーネント登録を行うModuleクラス
12
public class SolrIndexModule extends AbstractModule { @Override protected void configure() { this.bind(RegisterSolrDateType.class) .asEagerSingleton(); }}
@Injectを付けたコンストラクタで他コンポーネントを取得可能
各種コンポーネント
13
public class RegisterSolrDateType extends AbstractIndexComponent {
@Inject public RegisterSolrDateType(final Index index, @IndexSettings final Settings indexSettings, final MapperService mapperService) { super(index, indexSettings);
…略… }}
Mavenでzip成果物を生成するための設定ファイルassemblyのXMLファイル
14
<?xml version="1.0"?><assembly> <id></id> <formats><format>zip</format></formats> <includeBaseDirectory>false</includeBaseDirectory> <dependencySets> <dependencySet> <outputDirectory>/</outputDirectory> <useProjectArtifact>true</useProjectArtifact> <includes> <include>org.codelibs:elasticsearch-solr-api</include> <include>org.apache.solr:solr-core</include> ... 略 ... </includes> </dependencySet> </dependencySets></assembly> 含めるjarファイルを記述する含めるjarファイルを記述する
maven-shade-pluginによりパッケージ名が変えられている 例: org.joda→org.elasticsearch.common.joda
RestRequestから複数の値を持つリクエストパラメータが取得できない …/hoge?a=1&a=2&a=3 → a=[1,2,3]ではとれない
pom.xmlに記述した依存関係で成果物ができないので、zipにjarを入れ忘れたり…
非同期なので、デバッグを注意する
作る上でのポイント
15
Solrのインターフェースでelasticsearchを利用できるプラグイン
mocksolrpluginからフォーク SolrのXMLとJavaBin形式に対応 SolrJとか使って、検索と更新ができる ファセットやハイライトも可能 Solrのファンクションクエリーは非対応
Solr APIプラグインとは
17
elasticsearchのインストールtar zxvf elasticsearch-0.90.3.tar.gz
プラグインのインストール./bin/plugin -install elasticsearch-solr-api -url http://maven.codelibs.org/org/codelibs/elasticsearch-solr-api/1.2.1/elasticsearch-solr-api-1.2.1.zip
elasticsearchの実行./bin/elasticsearch
以下のURLでSolrのようにアクセスできる:http://hostname:9200/[index]/[type]/_solr
導入手順
18
簡単に導入できるOSS全文検索システム Apache Solrを検索エンジンとして利用 Apacheライセンスで提供 Webサイトやファイルシステムのクロール データベースもクロール可能 ブラウザによる管理画面 MS Office、PDF、圧縮ファイルもサポート ログイン状態で検索結果の出し分け可能 登録したカテゴリごとに検索可能 ・・・などなど機能多数
Fessとは
19
solrlib.diconでSolrのURLを変更するhttp://localhost:8080/solr/core1/↓http://localhost:9200/solr/core1/_solr
elasticserchにmappingを作成する
・・・以上
Fessでの導入手順
20
curl -XPUT 'http://127.0.0.1:9200/solr/core1/_mapping' -d '…'
Fessで利用するmappingを作成
21
"properties" : { "id" : {"type" : "string", "store" : "yes", "index" : "not_analyzed"}, "parentId" : {"type" : "string", "store" : "yes", "index" : "not_analyzed"}, "segment" : {"type" : "string", "store" : "yes", "index" : "not_analyzed"}, "digest" : {"type" : "string", "store" : "yes"}, "boost" : {"type" : "float", "store" : "yes", "null_value" : 1.0}, "host" : {"type" : "string", "store" : "yes"}, "site" : {"type" : "string", "store" : "yes"}, "url" : {"type" : "string", "store" : "yes", "index" : "not_analyzed"}, "content" : {"type" : "string", "store" : "yes"}, "title" : {"type" : "string", "store" : "yes"}, "cache" : {"type" : "string", "store" : "yes", "index" : "not_analyzed"}, "tstamp" : {"type" : "solr_date", "store" : "yes"}, "anchor" : {"type" : "string", "store" : "yes", "index" : "not_analyzed"}, "contentLength" : {"type" : "long", "store" : "yes"}, "lastModified" : {"type" : "solr_date", "store" : "yes"}, "lang" : {"type" : "string", "store" : "yes"}, "mimetype" : {"type" : "string", "store" : "yes", "index" : "not_analyzed"}, "type" : {"type" : "string", "store" : "yes", "index" : "not_analyzed"}, "label" : {"type" : "string", "store" : "yes", "index" : "not_analyzed"}, "role" : {"type" : "string", "store" : "yes", "index" : "not_analyzed"}}
Fessでelasticsearch-solr-api経由でelasticsearchを利用してみる
Fess管理URL: http://127.0.0.1:8080/fess/admin
Fess検索URL: http://127.0.0.1:8080/fess/search?query=fess
elasticsearch検索URL: http://127.0.0.1:9200/solr/core1/_search?q=fess&pretty=true
※ 差分クロールはファンクションクエリーを利用しているので利用できない
デモ
22