elasticsearch
文書の過去の版を表示しています。
目次
elasticsearch
Luceneをベースにした全文検索エンジン。
インストール
macOS
brew install elasticsearch
コマンド
httpリクエストを投げて操作するのでcurlなどを使うと良い
インデックス(RDBで言うテーブル)一覧
curl -XGET 'localhost:9200/_cat/indices?v'
適当に1件取得
curl -XGET 'localhost:9200/<インデックス名>/_search?pretty&size=1'
prettyはjsonをニンゲンが見やすくフォーマットする指定。
id指定取得
curl -XGET 'localhost:9200/<インデックス名>/_doc/<_id>?pretty'
日本語形態素解析エンジンkuromoriを使う
日本語のような分かち書きされない言語を全文検索するには、ngramや形態素解析での前処理が必要。
インストール
プラグインとして提供されている
/usr/local/Cellar/elasticsearch/5.6.3/bin/elasticsearch-plugin install analysis-kuromoji
curl -XPOST http://localhost:9200/_analyze --data '{"tokenizer":"kuromoji_tokenizer","text": "オープンソースの全文検索エ ンジン"}' 2>/dev/null | jq . { "tokens": [ { "token": "オープン", "start_offset": 0, "end_offset": 4, "type": "word", "position": 0 }, { "token": "ソース", "start_offset": 4, "end_offset": 7, "type": "word", "position": 1 }, { "token": "の", "start_offset": 7, "end_offset": 8, "type": "word", "position": 2 }, { "token": "全文", "start_offset": 8, "end_offset": 10, "type": "word", "position": 3 }, { "token": "検索", "start_offset": 10, "end_offset": 12, "type": "word", "position": 4 }, { "token": "エンジン", "start_offset": 12, "end_offset": 16, "type": "word", "position": 5 } ] }
ユーザ辞書を使う
形態素解析の欠点は、エンジンが知らない謎の単語に対応できないこと。謎単語はユーザ辞書を追加することで対処可能。
辞書なしの場合
curl -XPOST http://localhost:9200/_analyze --data '{"tokenizer":"kuromoji_tokenizer","text": "にゃんにゃんわんわん"}' 2>/dev/null | jq . { "tokens": [ { "token": "に", "start_offset": 0, "end_offset": 1, "type": "word", "position": 0 }, { "token": "ゃんにゃんわんわん", "start_offset": 1, "end_offset": 10, "type": "word", "position": 1 } ] }
辞書ありの場合
text.txtを作成し、/usr/local/etc/elasticsearch/ に設置
にゃん,にゃん,ニャン,名詞 わん,わん,ワン,名詞
curl -XPOST http://localhost:9200/_analyze --data '{"tokenizer":{"type":"kuromoji_tokenizer","user_dictionary": "test.txt"},"text": "にゃんにゃんわんわん"}' 2>/dev/null | jq . { "tokens": [ { "token": "にゃん", "start_offset": 0, "end_offset": 3, "type": "word", "position": 0 }, { "token": "にゃん", "start_offset": 3, "end_offset": 6, "type": "word", "position": 1 }, { "token": "わん", "start_offset": 6, "end_offset": 8, "type": "word", "position": 2 }, { "token": "わん", "start_offset": 8, "end_offset": 10, "type": "word", "position": 3 } ] }
elasticsearch.1621577868.txt.gz · 最終更新: 2021/05/21 06:17 by nullpon