Elasticsearchを構成する概念
ドキュメント
Elasticsearchに格納するデータの単位。RDBでいうところのレコードの概念。
primary keyとなるIDで管理され、ドキュメント作成時に指定可能。指定しなければ自動で採番される。
フィールド
- text型 格納する際、単語ごとに分割され、単語ごとの検索が可能
- keyword型 格納した文字列を「完全一致」で検索する用途で使用する。
- 数値型 (long, short, integer, float)
- date型
- boolean型
- object, array JSONフォーマット
インデックス
データを格納する場所を指す。RDBでいうところのテーブルに近い。
ドキュメントタイプ
インデックスのフィールド構造に名称をつけたものを指す。が、Elasticsearch7 からタイプレスとなり、_docで意識せず使用したほうがよい。
1インデックスにつき1ドキュメントタイプ。よって、テーブル定義に近い概念。
https://www.elastic.co/jp/blog/moving-from-types-to-typeless-apis-in-elasticsearch-7-0
マッピング
ドキュメントタイプに対する構造定義を記載したもの。
記述をしない場合、Elasticsearchが推測してマッピングを作成する。
keyword型などElasticsearchが推測し難いフィールドに対して予め定義しておく。
シャード
インデックスを分割する単位。インデックス作成時に指定。後から分割できない。
分割したインデックスは異なるノードで分散保持される。
デフォルトのシャード数は5
目安として、20〜30GBのインデックス容量を1シャード賄えるらしい。
レプリカ
ノード障害時の可用性、及び検索分散としてシャードのレプリケーションを持つことができる。
こちらは後からでもレプリカ数を変更できる.
Elasticsearchの基本操作
ドキュメントの作成
$ curl -XPUT 'http://localhost:9200/my_index/_doc/1' -H 'content-Type: application/json' -d'
{
"user_name": "John Smith",
"date": "2017-10-15T15:09:45",
"message": "Hello Elasticsearch world."
}
'
ドキュメントの取得
$ curl -XGET http://localhost:9200/my_index/_doc/1
{"_index":"my_index","_type":"_doc","_id":"1","_version":1,"_seq_no":0,"_primary_term":1,"found":true,"_source":
{
"user_name": "John Smith",
"date": "2017-10-15T15:09:45",
"message": "Hello Elasticsearch world."
}
}
$ curl -XGET http://localhost:9200/my_index/_doc/1/_source
{
"user_name": "John Smith",
"date": "2017-10-15T15:09:45",
"message": "Hello Elasticsearch world."
}
ドキュメントの検索
$ curl -XGET http://localhost:9200/my_index/_search?pretty -H 'Content-Type: application/json' -d '
{
"query": {
"match": {
"message": "Elasticsearch"
}
}
}
'
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"user_name" : "John Smith",
"date" : "2017-10-15T15:09:45",
"message" : "Hello Elasticsearch world."
}
}
]
}
}
ドキュメントの更新
$ curl -XPUT 'http://localhost:9200/my_index/_doc/1' -H 'Content-Type: application/json' -d '
{
"user_name": "Mike Stuart",
"date": "2017-11-04T16:36:12",
"message": "This message was updated."
}
'
ドキュメントの一部更新
$ curl -XPOST 'http://localhost:9200/my_index/_doc/1/_update' -H 'Content-Type: application/json' -d '
{
"doc": {
"message": "Only message was updated."
}
}
'
ドキュメントの削除
$ curl -XDELETE 'http://localhost:9200/my_index/_doc/1'
インデックスの作成
$ curl -XPUT 'http://localhost:9200/my_index' -H 'Content-Type: application/json' -d '
{
"settings": {
"number_of_shards": "3",
"number_of_replicas": "2"
}
}
'
インデックスの確認
$ curl -XGET 'http://localhost:9200/my_index'
レプリカ数の変更
$ curl -XPUT 'http://localhost:9200/my_index/_settings' -H 'Content-Type: application/json' -d '
{
"index": {
"number_of_replicas": "1"
}
}
'
インデックスの削除
$ curl -XDELETE 'http://localhost:9200/my_index'
マッピングの作成
$ curl -XPUT 'http://localhost:9200/my_index' -H 'Content-Type: application/json' -d '
{
"mappings": {
"properties": {
"user_name": { "type": "keyword" },
"date": {"type": "date"},
"message": {"type": "text"}
}
}
}
'
マッピング定義の確認
$ curl -XGET 'http://localhost:9200/my_index/_mapping?pretty'
リンク
