内容へ移動
Cat Paw Software
ユーザ用ツール
ログイン
サイト用ツール
検索
ツール
文書の表示
以前のリビジョン
バックリンク
最近の変更
メディアマネージャー
サイトマップ
ログイン
>
最近の変更
メディアマネージャー
サイトマップ
トレース:
elasticsearch
この文書は読取専用です。文書のソースを閲覧することは可能ですが、変更はできません。もし変更したい場合は管理者に連絡してください。
====== elasticsearch ====== Luceneをベースにした全文検索エンジン。 ===== インストール ===== macOS <code> brew install elasticsearch </code> ===== コマンド ===== httpリクエストを投げて操作するのでcurlなどを使うと良い ==== インデックス(RDBで言うテーブル)一覧 ==== curl 'http://localhost:9200/_cat/indices?v' ==== 適当に1件取得 ==== curl 'http://localhost:9200/<インデックス名>/_search?pretty&size=1' prettyはjsonをニンゲンが見やすくフォーマットする指定。 ==== id指定取得 ==== curl 'http://localhost:9200/<インデックス名>/_doc/<_id>?pretty' ==== valueの最大値を取得 ==== curl -XPOST -H 'Content-type: application/json' -d '{ "aggs": { "max_value": { "max": { "field": "value" } } } }' 'http://localhost:9200/<インデックス名>/_search?size=0' ==== フィールドの存在有無検索 ==== curl -XPOST -H 'Content-type: application/json' -d '{ "query": { "exists": { "field": "field_name" } } }' 'http://localhost:9200/<インデックス名>/_search?pretty&size=1' ==== 範囲検索 ==== curl -XPOST -H 'Content-type: application/json' -d '{ "query": { "range": { "date": { "gt": "2020-01-01T00:00:00+09:00" } } } }' 'http://localhost:9200/<インデックス名>/_search?pretty&size=1' ==== and条件検索 ==== <code> curl -XPOST -H 'Content-type: application/json' -d '{ "query": { "bool": { "must": [ { "range": { "date": { "gt": "2020-01-01T00:00:00+09:00" } } }, { "exists": { "field": "field_bame" } } ] } } }' 'http://localhost:9200/<インデックス名>/_search?pretty&size=1' </code> ===== 日本語形態素解析エンジンkuromojiを使う ===== 日本語のような分かち書きされない言語を全文検索するには、ngramや形態素解析での前処理が必要。 * [[https://www.atilika.com/ja/products/kuromoji.html|kuromoji]] * [[https://www.elastic.co/guide/en/elasticsearch/plugins/current/analysis-kuromoji-tokenizer.html|kuromoji tokenizer]] ==== インストール ==== プラグインとして提供されている <code> /usr/local/Cellar/elasticsearch/5.6.3/bin/elasticsearch-plugin install analysis-kuromoji </code> <code> 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 } ] } </code> ==== ユーザ辞書を使う ==== 形態素解析の欠点は、エンジンが知らない謎の単語に対応できないこと。謎単語はユーザ辞書を追加することで対処可能。 辞書なしの場合 <code> 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 } ] } </code> 辞書ありの場合 text.txtを作成し、/usr/local/etc/elasticsearch/ に設置 <code> にゃん,にゃん,ニャン,名詞 わん,わん,ワン,名詞 </code> <code> 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 } ] } </code> ===== エイリアス ===== インデックスに別名を付ける機能、インデックス名の代わりにエイリアス名を指定して検索ができる 一覧 <code> $ curl http://localhost:9200/_cat/aliases?v </code> エイリアス作成 <code> $ curl -X POST -H 'Content-Type: application/json' -d '{ "actions" : [ { "remove" : { "index" : "hoge_old", "alias" : "hoge" } },{ "add" : { "index" : "hoge_new", "alias" : "hoge" } } ] }' http://localhost:9200/_aliases </code> エイリアス削除 <code> curl -X POST -H 'Content-Type: application/json' -d '{ "actions" : [ { "remove" : { "index" : "hoge_old", "alias" : "hoge" } } } ] }' http://localhost:9200/_aliases </code> 別のインデックスに変更 <code> curl -X POST -H 'Content-Type: application/json' -d '{ "actions" : [ { "remove" : { "index" : "hoge_old", "alias" : "hoge" } },{ "add" : { "index" : "hoge_new", "alias" : "hoge" } } ] }' http://localhost:9200/_aliases </code>
elasticsearch.txt
· 最終更新:
2021/09/29 10:15
by
nullpon
ページ用ツール
文書の表示
以前のリビジョン
バックリンク
文書の先頭へ