ElasticsearchのIndices APIsのCreate Indexについて記す。
公式ドキュメント Create Index を参考にした。 www.elastic.co
検証環境: Elasticsearch 6.0.0-rc1
インデックスを作成する。
以下ではインデックス名をtwitter,number_of_shards
を3,number_of_replicas
を2とした。(デフォルトではnumber_of_shards
は5,number_of_replicas
は1である。)
bash-3.2$ curl -XPUT 'localhost:9200/twitter?pretty' -H 'Content-Type: application/json' -d' > { > "settings" : { > "index" : { > "number_of_shards" : 3, > "number_of_replicas" : 2 > } > } > } > ' { "acknowledged" : true, "shards_acknowledged" : true, "index" : "twitter" } bash-3.2$ curl -XGET 'localhost:9200/_cat/indices?v&s=index&pretty' health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open bank yaVaZLiLT2G0RyA-vBn5nw 5 1 1000 0 488.3kb 488.3kb yellow open customer tl6qvdROTfuL380eLOxH0Q 5 1 2 0 8.3kb 8.3kb yellow open indexusertest EO9eYTNoT-i7_L3PBIb7dQ 5 1 1 0 4.7kb 4.7kb yellow open noindextest LseoEEkrSiGuMeWXJNEMrA 5 1 1 0 4.7kb 4.7kb yellow open twitter 3e5dK21MSGOJKO6bKw-9sQ 3 2 0 0 699b 699b
インデックスを作成するときにmappingもできる。
bash-3.2$ curl -XPUT 'localhost:9200/test?pretty' -H 'Content-Type: application/json' -d' > { > "settings" : { > "number_of_shards" : 1 > }, > "mappings" : { > "type1" : { > "properties" : { > "field1" : { "type" : "text" } > } > } > } > } > ' { "acknowledged" : true, "shards_acknowledged" : true, "index" : "test" } bash-3.2$ curl -XGET 'localhost:9200/_cat/indices?v&s=index&pretty' health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open bank yaVaZLiLT2G0RyA-vBn5nw 5 1 1000 0 488.3kb 488.3kb yellow open customer tl6qvdROTfuL380eLOxH0Q 5 1 2 0 8.3kb 8.3kb yellow open indexusertest EO9eYTNoT-i7_L3PBIb7dQ 5 1 1 0 4.7kb 4.7kb yellow open noindextest LseoEEkrSiGuMeWXJNEMrA 5 1 1 0 4.7kb 4.7kb yellow open test gjHIWwJzQh-z0I5gLsP3ug 1 1 0 0 233b 233b yellow open twitter 3e5dK21MSGOJKO6bKw-9sQ 3 2 0 0 699b 699b
また、インデックスを作成するときにaliasesを設定することもできる。
bash-3.2$ curl -XPUT 'localhost:9200/aliastest?pretty' -H 'Content-Type: application/json' -d' > { > "aliases" : { > "alias_1" : {}, > "alias_2" : { > "filter" : { > "term" : {"user" : "kimchy" } > }, > "routing" : "kimchy" > } > } > } > ' { "acknowledged" : true, "shards_acknowledged" : true, "index" : "aliastest" } bash-3.2$ curl -XGET 'localhost:9200/_cat/indices?v&s=index&pretty' health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open aliastest F_l8GmeoRgG99UM5Wdb-Gw 5 1 0 0 466b 466b yellow open bank yaVaZLiLT2G0RyA-vBn5nw 5 1 1000 0 488.3kb 488.3kb yellow open customer tl6qvdROTfuL380eLOxH0Q 5 1 2 0 8.3kb 8.3kb yellow open indexusertest EO9eYTNoT-i7_L3PBIb7dQ 5 1 1 0 4.7kb 4.7kb yellow open noindextest LseoEEkrSiGuMeWXJNEMrA 5 1 1 0 4.7kb 4.7kb yellow open test gjHIWwJzQh-z0I5gLsP3ug 1 1 0 0 233b 233b yellow open twitter 3e5dK21MSGOJKO6bKw-9sQ 3 2 0 0 699b 699b
インデックスの作成は、デフォルトでは各シャードのプライマリコピーが開始されたときまたはリクエストがタイムアウトしたときにクライアントに応答を返す。
{ "acknowledged" : true, "shards_acknowledged" : true, "index" : "aliastest" }
acknowledged
はクラスタ内でインデックスが正常に作成されたかどうかを示す。
shards_acknowledged
はタイムアウトする前にインデックス内の各シャードに対して必要な数のシャードコピーが開始されたかどうかを示す。
acknowledged
またはshards_acknowledged
のどちらかがfalseであってもインデックスの作成には成功する可能性があることに注意する必要がある。