ElasticSearch×形態素解析
環境構築してみる
こちらから最新バージョンをDL、適当なディレクトリに解凍する。
Javaのインストールとパスを通しておきましょう。
日本語で検索したいのでkuromoji pluginをインストールします。
私の環境ではproxyを通す必要がありました。
Windowsでは以下のコマンドでOK。公式ドキュメントでは"(ダブルクォート付き)の記述がありましたがうまく行かず、"を消せばインストールできました。
# set ES_JAVA_OPTS=-Dhttp.proxyHost=**.**.**.** -Dhttp.proxyPort=**** Dhttps.proxyHost=**.**.**.** -Dhttps.proxyPort=****
# elasticsearch\bin\elasticsearch-plugin install analysis-kuromoji
起動してみます
# elasticsearch\bin\elasticsearch.bat
http://localhost:9200 にアクセスして以下のようなJsonが取得できれば完了です。
{
"name": "aV33ZuH",
"cluster_name": "elasticsearch",
"cluster_uuid": "-rHtAb19QzKUiJ1_aq_-ig",
"version": {
"number": "5.4.1",
"build_hash": "2cfe0df",
"build_date": "2017-05-29T16:05:51.443Z",
"build_snapshot": false,
"lucene_version": "6.5.1"
},
"tagline": "You Know, for Search"
}
検索してみる
適当にデータを登録してみます
curl -XPOST http://localhost:9200/test/sample -d '{
"id" : "1",
"text" : "夏なのでアイスがおいしい季節になりました"
}'
curl -XPOST http://localhost:9200/test/sample -d '{
"id" : "2",
"text" : "桃アイスを頂いた"
}'
curl -XPOST http://localhost:9200/test/sample -d '{
"id" : "2",
"text" : "昨日桃ちゃんと遊んだよ"
}'
検索してみます
curl -XPOST http://localhost:9200/test/sample/_search -d '{
"query": {
"match": {
"text": "アイスが"
}
}
}
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.5398108,
"hits": [
{
"_index": "test",
"_type": "sample",
"_id": "AVzERcmBSCe8bM-UJRG3",
"_score": 0.5398108,
"_source": {
"id": "1",
"text": "夏なのでアイスがおいしい季節になりました"
}
},
{
"_index": "test",
"_type": "sample",
"_id": "AVzERh6cSCe8bM-UJRG4",
"_score": 0.2824934,
"_source": {
"id": "2",
"name": "桃アイスを頂いた"
}
}
]
}
}
検索ワードの「アイスが」の助詞が省かれていることがわかります。