Elassticsearch試してみた


業務でElasticsearchに触ったので復習。

環境

Windows10

ダウンロード

ElasticsearchとKibanaは事前に以下からダウンロードして解凍する。
https://www.elastic.co/jp/start

起動

それぞれ以下をダブルクリックで起動できる。

  • Elasticsearch

C:\~~\elasticsearch-7.10.0-windows-x86_64\elasticsearch-7.10.0\bin\elasticsearch.bat

  • Kibana

C:\~~\kibana-7.10.0-windows-x86_64\bin\kibana.bat

接続確認

以下URLで接続できれば、起動成功!

  • Elasticsearch

http://localhost:9200/
cluster_nameとかversionとか書いてるJSONが表示されるはず

  • Kibana

Kibanaで触ってみる

KibanaのDev Toolsから作成してみる。

↓こんな感じの画面で、左にqueryを記述して実行すると、右側にレスポンスが表示される。
query画面の一番右ので実行できる。(実行対象行を選択し、ctrl + Enterでも実行可能)

index作成

インデックスはDBでいうデータベースのようなもの・・・?
test_indexというインデックスを作成してみる。

PUT test_index

作成できたことを確認。

GET _cat/indices?v

補足

yellow open test_index lBZ1wx4vQyKcU8danKLkRQ 1 1 0 0 208b 208b
healthyellowとなっていますが、これはレプリカ(複製)が作成できていないため。
データが破損したとき等や負荷分散に使用されるレプリカは、デフォルトで1つ用意するように設定されている。
(レプリカは別ノードに作成する必要がある)
ローカル環境はデータノードは1台なので、レプリカを作成できないよーのyellowステータス。
そのままで問題ないが、どうしてもgreenにしたい場合は、一度インデックスを削除してレプリカ1台でindexを再作成すればOK。

#インデックス削除
DELETE test_index

#レプリカ数0でインデックス再作成
PUT test_index
{
  "settings": {
    "number_of_replicas": 0
  }
}

#設定確認(test_index.settings.number_of_replicasが0になっていること)
GET test_index/_settings

type作成

DBでいうテーブルのようなもの・・・?
↓でタイプを定義しようとしましたが、

POST test_index/test_type/
{
    "mappings" : {
        "qq" : {
            "properties" : {
                "id" : {
                  "type" : "integer"
                  },
                "name" : {
                  "type" : "string"
                }
            }
        }
    }
}

_#! Deprecation: [types removal] Specifying types in document index requests is deprecated, use the typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}).

いつの間にか非推奨になっていたので、test_index/_docで登録してみます。

POST test_index/_doc
{
  "test_id": 1,
  "test_name": "てすとねーむ"
}

登録できたので、mappingを確認。

GET test_index/_mapping
#GET test_index/_mapping 実行結果
{
  "test_index" : {
    "mappings" : {
      "properties" : {
        "test_id" : {
          "type" : "long"
        },
        "test_name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

先ほどのデータは以下のような定義で作成されていました。
(定義せずともデータ登録可能な点は変わっておらず、typeという概念がなくなるっぽいですね。)

インデックスを作成するときにmapping(DBでいうCREATE TABLE?)を指定を指定するには以下でOK!のはず。。。

PUT test_index2
{
  "settings": {
    "number_of_replicas": 0               // レプリカ数指定
  },
  "mappings": {
    "properties": {
      "test_id": {             // test_id フィールド
        "type":"integer"
      },
      "test_name": {                      // test_nameフィールド
        "type": "keyword"         
      }
    }
  }
}

また、mappingは追加は可能ですが、削除・変更はできないです。

# NG
# 既存フィールド(test_id)のtypeをinteger→long
PUT test_index2/_mapping
{
  "properties": {
    "test_id": {
      "type": "long"
    }
  }
}

# OK
# new_field追加(元々あったフィールドは残る)
PUT test_index/_mapping
{
  "properties": {
    "new_field": {
      "type": "text"
    }
  }
}

所感

Elasticsearchを実際に業務で使ってたのは約1年半くらい前なので、記憶がうっすらしてて手探りでした。
色々変わっている点もあるので、公式ドキュメントもう一度さらっと読んでみます。
Elasticsearchは公式サイトにかなり詳しく説明が載っているのでおすすめです。
https://www.elastic.co/guide/jp/elasticsearch/reference/current/index.html