Elasticsearchドキュメント操作の基本
ドキュメントの登録
PUT
IDを指定して登録します。
IDに使える文字は半角英数っぽい
API形式
http://ホスト:9200/インデックス名/ドキュメントタイプ名/ID
例)
curl -XPUT http://localhost:9200/my_index/my_type/1 \
-H 'Content-Type: application/json' -d'
{
"name": "kozo",
"address": "japan",
"message": "hello world"
}
'
POST
IDはランダムな英数で自動採番される。
他にPUTとの違いがあるのかな?
API形式
http://ホスト:9200/インデックス名/ドキュメントタイプ名/
例)
curl -XPOST http://localhost:9200/my_index/my_type \
-H 'Content-Type: application/json' -d'
{
"name": "kozo",
"address": "japan",
"message": "hello world"
}
'
ドキュメントの取得
ID指定での取得方法です。
API形式
http://ホスト:9200/インデックス名/ドキュメントタイプ名/ID
例)
curl -XGET http://localhost:9200/my_index/my_type/1 curl -XGET http://localhost:9200/my_index/my_type/1?pretty curl -XGET http://localhost:9200/my_index/my_type/1/_source?pretty
?prettyを指定することでレスポンスを見やすくフォーマットしてくれます_sourceを指定すると登録内容のみが取得できます
ドキュメントの検索
とりあえず完全一致
APII形式
http://ホスト:9200/インデックス名/ドキュメントタイプ名/_search
例)
curl -XGET http://localhost:9200/my_index/my_type/_search?pretty -H 'Content-Type: application/json' -d'
{
"query" : {
"match" : {
"name": "kozo"
}
}
}'
検索範囲
# my_typeの中から検索 http://localhost:9200/my_index/my_type/_search?pretty # my_indexの中から検索 http://localhost:9200/my_index/_search?pretty # 複数indexの中から検索 http://localhost:9200/my_index,my_index2/_search?pretty # ワイルドカード指定 http://localhost:9200/my_index*/_search?pretty # 全体から検索 http://localhost:9200/_search?pretty
ドキュメントの全体更新
PUTでドキュメントの全体を更新します
API形式
http://ホスト:9200/インデックス名/ドキュメントタイプ名/ID
例)
curl -XPUT http://localhost:9200/my_index/my_type/1 \
-H 'Content-Type: application/json' -d'
{
{
"name": "kozo",
"address": "japan",
"message": "hello world"
}
}
'
ドキュメントの一部更新
更新だけど、 POST を発行するみたい。
内部的には削除 => 再登録なのかな?
doc を指定して送信します
API形式
http://ホスト:9200/インデックス名/ドキュメントタイプ名/ID/_update
例)
curl -XPOST 'http://192.168.10.11:9200/my_index/my_type/1/_update?pretty' -H 'Content-Type: application/json' -d'
{
"doc": {
"user_name": "kozo(update test)"
}
}
'
ドキュメントの削除
ID指定で削除する
API形式
http://ホスト:9200/インデックス名/ドキュメントタイプ名/ID
例)
curl -XDELETE http://192.168.10.11:9200/my_index/my_type/1?pretty
インデックスの作成
インデクスを作成せずにドキュメントの登録を行うとElasticsearchが自動的にインデックスを作成している。
インデックスを明示的に作成する必要があるタイミングは以下ののような デフォルト値 でインデックスを作成したくない場合となる
- レプリカ数を変更したい場合
- シャード数を変更したい場合(特にシャード数はインデックス作成時のみにしか変更することは出来ない)
API形式
http://ホスト:9200/インデックス名
例)
curl -XPUT "http://192.168.10.11:9200/my_index3" -H 'Content-Type: application/json' -d'
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
}
}'
例2) すべてを書略した場合
curl -XPUT "http://192.168.10.11:9200/my_index3"
インデックスの設定情報取得
API形式
http://ホスト:9200/インデックス名/_settings
例)
curl -XGET "http://192.168.10.11:9200/my_index3/_settings?pretty"
インデックス設定の更新
API形式
http://ホスト:9200/インデックス名/_settings
例)
curl -XPUT "http://192.168.10.11:9200/my_index3/_settings" -H 'Content-Type: application/json' -d'
{
"index": {
"number_of_replicas": 3
}
}'
インデックスの削除
API形式
http://ホスト:9200/インデックス名
例)
curl -XDELETE "http://192.168.10.11:9200/my_index3"