📈
Elasticsearchでミニマムな登録・検索・削除をやってみた
はじめに
雰囲気でElasticsearchを使っていたので、公式サイトを見ながら基本操作から登録・検索・削除を行う具体的な方法をまとめてみました。
前提
- ローカルにElasticsearchが起動している必要があります
- Kibanaを利用してクエリを発行します
まとめ
- POST /<index>/_doc APIでdocumentをindexに登録できる
- POST /<index>/_bulk APIで複数documentを一括でindexに登録できる
- GET /<index>/_search APIでdocumentを検索できる
- DELETE /<index>/_doc/<_id>でデータを検索できる
indexの作成からデータ登録・削除まで一括で試せるクエリのセットはこちら
クエリ
# // 事前準備(既にemployeesがあれば削除)
# // ===========================================
DELETE /employees/
# // 登録
# // ===========================================
# employeesというインデックスに対して1件documentを登録する
# See: Elastic https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html
{
"name":"kato",
"occupation":{
"id":"1",
"name":"エンジニア"
},
"tag": "子持ち"
}
# employeesというインデックスに対して4件documentを登録する
# See:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html
POST /employees/_bulk
{ "index":{} }
{"name":"lato","occupation":{"id":"2","name":"デザイナー" }}
{ "index":{} }
{"name":"mato","occupation":{"id":"3","name":"ディレクター"}}
{ "index":{} }
{"name":"nato","occupation":{"id":"3","name":"ディレクター"}}
{ "index":{} }
{"name":"oato","occupation":{"id":"1","name":"エンジニア"}}
# // 検索
# // ===========================================
# employeesの全documentを取得する
# See: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html
GET /employees/_search/
# nameがkatoのdocumentを取得する
# See: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html
GET /employees/_search
{
"query": {
"match": {
"name": "kato"
}
}
}
# // 削除
# // ===========================================
# katoさん(_id=vcqaj3sBqdbv6KNHvDUD)を削除
# See: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html
DELETE /employees/_doc/vcqaj3sBqdbv6KNHvDUD
# nameがkatoのdocumentを取得する(DELETEの後だと出ない)
# See: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html
GET /employees/_search
{
"query": {
"match": {
"name": "kato"
}
}
}
やってみたこと
documentを追加してみる
- 検索に必要なdocumentをindexに追加します。
このような社員情報を登録します。
name | occupation | tag |
---|---|---|
kato | エンジニア | 子持ち |
lato | デザイナー | (指定なし) |
mato | ディレクター | (指定なし) |
nato | ディレクター | (指定なし) |
oato | エンジニア | Elasticsearchが得意 |
JSONが扱えることを確認するために、occupationにはオブジェクトを入れます。
occupationは3種類あって、それぞれidを持つものの想定です
|id|value|
|:----|:----|:----|
|1|エンジニア|
|2|デザイナー|
|3|ディレクター|
-
POST /<index>/_doc APIでindexにdocumentを追加してみます。
- リクエスト
# employeesというインデックスに対して1件documentを登録する # See: Index API | Elasticsearch Guide [7.14] | Elastic https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html POST /employees/_doc { "name":"kato", "occupation":{ "id":"1", "name":"エンジニア" }, "tag": "子持ち" }
- レスポンス
{ "_index" : "employees", "_type" : "_doc", "_id" : "vcqaj3sBqdbv6KNHvDUD", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1 }
-
次に POST /<target>/_bulk APIで4つのデータをまとめて追加します
- リクエスト
POST /employees/_bulk { "index":{} } {"name":"lato","occupation":{"id":"2","name":"デザイナー" }} { "index":{} } {"name":"mato","occupation":{"id":"3","name":"ディレクター"}} { "index":{} } {"name":"nato","occupation":{"id":"3","name":"ディレクター"}} { "index":{} } {"name":"oato","occupation":{"id":"1","name":"エンジニア"}}
- レスポンス
{ "took" : 23, "errors" : false, "items" : [ { "index" : { "_index" : "employee", "_type" : "_doc", "_id" : "wsqnj3sBqdbv6KNHGDVH", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 4, "_primary_term" : 1, "status" : 201 } }, { "index" : { "_index" : "employee", "_type" : "_doc", "_id" : "w8qnj3sBqdbv6KNHGDVH", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 5, "_primary_term" : 1, "status" : 201 } }, { "index" : { "_index" : "employee", "_type" : "_doc", "_id" : "xMqnj3sBqdbv6KNHGDVH", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 6, "_primary_term" : 1, "status" : 201 } }, { "index" : { "_index" : "employee", "_type" : "_doc", "_id" : "xcqnj3sBqdbv6KNHGDVH", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 7, "_primary_term" : 1, "status" : 201 } } ] }
indexを確認してみる
- リクエスト
# employeesの全documentを取得する
# See: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html
GET /employees/_search/
- レスポンス
{
"took" : 597,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "employees",
"_type" : "_doc",
"_id" : "vcqaj3sBqdbv6KNHvDUD",
"_score" : 1.0,
"_source" : {
"name" : "kato",
"occupation" : {
"id" : "1",
"name" : "エンジニア",
"tag" : "子持ち"
}
}
},
{
"_index" : "employees",
"_type" : "_doc",
"_id" : "xsqvj3sBqdbv6KNHWjVi",
"_score" : 1.0,
"_source" : {
"name" : "lato",
"occupation" : {
"id" : "2",
"name" : "デザイナー"
}
}
},
{
"_index" : "employees",
"_type" : "_doc",
"_id" : "x8qvj3sBqdbv6KNHWjVi",
"_score" : 1.0,
"_source" : {
"name" : "mato",
"occupation" : {
"id" : "3",
"name" : "ディレクター"
}
}
},
{
"_index" : "employees",
"_type" : "_doc",
"_id" : "yMqvj3sBqdbv6KNHWjVi",
"_score" : 1.0,
"_source" : {
"name" : "nato",
"occupation" : {
"id" : "3",
"name" : "ディレクター"
}
}
},
{
"_index" : "employees",
"_type" : "_doc",
"_id" : "ycqvj3sBqdbv6KNHWjVi",
"_score" : 1.0,
"_source" : {
"name" : "oato",
"occupation" : {
"id" : "1",
"name" : "エンジニア"
}
}
}
]
}
}
-
nameがkatoのdocumentだけ取得してみます
- リクエスト
# nameがkatoのdocumentを取得する # See: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html GET /employees/_search { "query": { "match": { "name": "kato" } } }
- レスポンス
{ "took" : 0, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.3862944, "hits" : [ { "_index" : "employees", "_type" : "_doc", "_id" : "vcqaj3sBqdbv6KNHvDUD", "_score" : 1.3862944, "_source" : { "name" : "kato", "occupation" : { "id" : "1", "name" : "エンジニア", "tag" : "子持ち" } } } ] } }
削除
-
DELETE /<index>/_doc/<_id>で検索にヒットしたemploeeを削除して、再度検索して出てこないことを確認してみます。
- リクエスト
# katoさん(_id=vcqaj3sBqdbv6KNHvDUD)を削除 # See: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html DELETE /employees/_doc/vcqaj3sBqdbv6KNHvDUD
- レスポンス
{ "_index" : "employees", "_type" : "_doc", "_id" : "vcqaj3sBqdbv6KNHvDUD", "_version" : 2, "result" : "deleted", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 5, "_primary_term" : 1 }
-
削除したkatoさんを検索してみます
- リクエスト
# See: <https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html> GET /employees/_search { "query": { "match": { "name": "katoaki" } } }
- レスポンス
{ "took" : 1076, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 0, "relation" : "eq" }, "max_score" : null, "hits" : [ ] } }
- 期待通り、レスポンスにデータが入りませんでした。
参考
- Document APIs | Elasticsearch Guide [7.14] | Elastic https://www.elastic.co/guide/en/elasticsearch/reference/current/docs.html
Discussion