ユーザ用ツール

サイト用ツール


elasticsearch

elasticsearch

Luceneをベースにした全文検索エンジン。

インストール

macOS

brew install elasticsearch

コマンド

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条件検索

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'

日本語形態素解析エンジンkuromojiを使う

日本語のような分かち書きされない言語を全文検索するには、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
    }
  ]
}

エイリアス

インデックスに別名を付ける機能、インデックス名の代わりにエイリアス名を指定して検索ができる

一覧

$ curl http://localhost:9200/_cat/aliases?v

エイリアス作成

$ 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

エイリアス削除

curl -X POST -H 'Content-Type: application/json' -d '{ "actions" : [ { "remove" : { "index" : "hoge_old", "alias" : "hoge" } } } ] }' http://localhost:9200/_aliases

別のインデックスに変更

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
elasticsearch.txt · 最終更新: 2021/09/29 10:15 by nullpon