LoginSignup
0
0

More than 3 years have passed since last update.

Elasticsearch > kotlin > es-kotlin-clinent マニュアル(意訳)4 - CRUD 操作

Last updated at Posted at 2021-01-23

Elasticsearchをサーバーサイドで操作するKotlin版ライブラリ
がよく出来ていたのでその動作確認をしつつマニュアルを日本語にしつつメモしています。

全10回です。

es-kotlin-clinent マニュアル(意訳)1 - 序章
es-kotlin-clinent マニュアル(意訳)2 - Getting Started
es-kotlin-clinent マニュアル(意訳)3 - indexRepository
es-kotlin-clinent マニュアル(意訳)4 - CRUD 操作 <- ここ
es-kotlin-clinent マニュアル(意訳)5 - 楽観的ロック
es-kotlin-clinent マニュアル(意訳)6 - 検索
es-kotlin-clinent マニュアル(意訳)7 - Kotlin Query DSL
es-kotlin-clinent マニュアル(意訳)8 - Asynchronous
es-kotlin-clinent マニュアル(意訳)9 - DSLs
es-kotlin-clinent マニュアル(意訳)10 - Example

3. CRUD 操作

これまでインデックスができたので、CRUD操作を使うことができるようになりました。

1. ドキュメント追加

val id = "first"
println("Object does not exist: ${repo.get(id)}")

// so lets store something
repo.index(id, Thing("A thing", 42))

println("Now we get back our object: ${repo.get(id)}")

結果確認:キャプチャされた出力

Object does not exist: null
Now we get back our object: Thing(name=A thing, amount=42)

2. ドキュメント上書き

上書きされることをオプトインしない限り、オブジェクトに2回インデックスを付けることはできません。

オプトイン = , create = false

val id = "first"
try {
  repo.index(id, Thing("A thing", 40))
} catch (e: ElasticsearchStatusException) {
  println("we already had one of those and es returned ${e.status().status}")
}

// this how you do upserts
repo.index(id, Thing("Another thing", 666), create = false)
println("It was changed: ${repo.get(id)}")

結果確認:キャプチャされた出力

we already had one of those and es returned 409
It was changed: Thing(name=Another thing, amount=666)

3. ドキュメント削除

もちろん、オブジェクトを削除することも可能です。

repo.delete("1")
println(repo.get("1"))

結果確認:キャプチャされた出力

null
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0