elasticsearch7向け。curlでインデックスの定義、フィールドのマッピング、ドキュメントの登録
概要
elasticserachが7にアップデータされタイプレスとなりました。
にも関わらず未だにtypeありきの記事ばかりがヒットしてしまうので、触りの部分だけでもelasticserach7向けのものを用意しました。
とっちらかって流のでまだまだ修正予定ですがとりあえず現段階で公開しておきます。
操作
インデックスを作成
curl -XPUT 'localhost:9200/card'
「{"acknowledged":true,"shards_acknowledged":true,"index":"card"}」というようなメッセージが出たら成功です。
インデックスを確認
$ curl 'localhost:9200/_cat/indices?v'
cardというインデックスが生成されていることがわかります。
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open .kibana_task_manager_1 EMvLA0mGSz6teRxy8cdZxg 1 0 2 0 30.5kb 30.5kb
green open .apm-agent-configuration --tJVwgLRyC7uKjOkSyRXg 1 0 0 0 283b 283b
green open .kibana_1 oAsBJhsPTZaIeR5Ethtzqw 1 0 5 0 21.6kb 21.6kb
yellow open card HTKwBphgSKaftEnEPpTOSw 1 1 0 0 230b 230b
インデックスの中身も確認
$ curl -X GET 'localhost:9200/card?pretty'
{
"card" : {
"aliases" : { },
"mappings" : { },
"settings" : {
"index" : {
"creation_date" : "1573818625817",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "HTKwBphgSKaftEnEPpTOSw",
"version" : {
"created" : "7040099"
},
"provided_name" : "card"
}
}
}
}
マッピングの作成
card2というインデックスのに「title」という「keyword」タイプのフィールドを追加します。
※事情がありcard2というインデックスを作り直しています。
$ curl -X PUT "localhost:9200/card2/_mapping?pretty" -H 'Content-Type: application/json' -d'
{
"properties": {
"title": {
"type": "keyword"
}
}
}
出来上がった後のインデックスをもう一度見てみると"mappings"の中のpropatyの中にtitleがkeywordとして登録されています。
$ curl -X GET 'localhost:9200/card2?pretty'
{
"card2" : {
"aliases" : { },
"mappings" : {
"properties" : {
"title" : {
"type" : "keyword"
}
}
},
"settings" : {
"index" : {
"creation_date" : "1573995034195",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "2XZ4VZZYSyWY4bJn0OLskA",
"version" : {
"created" : "7040099"
},
"provided_name" : "card2"
}
}
}
}
ちなみにフィールド名を変更することで、追加としても利用できるようです。
ドキュメントを追加する
データの本体であるドキュメントを追加します。
$ curl -XPUT "localhost:9200/card2/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
"title": "TestTitle1"
}'
結果
{
"_index" : "card2",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
ドキュメント数が増えたか確認します。
$ curl 'localhost:9200/_cat/indices?v'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open card2 2XZ4VZZYSyWY4bJn0OLskA 1 1 0 0 283b 283b
green open .kibana_task_manager_1 EMvLA0mGSz6teRxy8cdZxg 1 0 2 0 21.7kb 21.7kb
green open .apm-agent-configuration --tJVwgLRyC7uKjOkSyRXg 1 0 0 0 283b 283b
green open .kibana_1 oAsBJhsPTZaIeR5Ethtzqw 1 0 5 0 21.6kb 21.6kb
yellow open card HTKwBphgSKaftEnEPpTOSw 1 1 0 0 283b 283b
ドキュメントの内容を確認する。
$ curl -X GET 'localhost:9200/card2/_doc/1?pretty'
{
"_index" : "card2",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"title" : "TestTitle1"
}
}
備考
mappingしていないフィールドを既存ドキュメントに対して作ってみたら、ちゃんとデータができていた。しかし指定していなかった既存フィールドがなくなってしまっていたのでデータの更新方法などはもう少し調査が必要。
参考サイト
公式サイトを参考にしました。
https://www.elastic.co/guide/en/elasticsearch/reference/7.4/indices-put-mapping.html
https://mahimahironron.hateblo.jp/entry/2019/11/17/elasticsearch%EF%BC%97%E5%90%91%E3%81%91%E3%80%82curl%E3%81%A7%E3%82%A4%E3%83%B3%E3%83%87%E3%83%83%E3%82%AF%E3%82%B9%E3%81%AE%E5%AE%9A%E7%BE%A9%E3%80%81%E3%83%95%E3%82%A3%E3%83%BC%E3%83%AB%E3%83%89
Author And Source
この問題について(elasticsearch7向け。curlでインデックスの定義、フィールドのマッピング、ドキュメントの登録), 我々は、より多くの情報をここで見つけました https://qiita.com/MaxyGen/items/d1d47aea08c36f02baa1著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .