はじめに
ES6からES8に変わったことで躓いたことがあったので、まとめます
問題
以下のようにKotlinでインデックスに対してドキュメントを追加するようなコードを書きました
fun putToIndex(index: String, id: String, json: String) {
client.index { it.index(index).id(id).document(json) }
}
そしてこのjsonには文字列のjsonが渡されています。
すると以下のエラーが発生しました
Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes
おそらく文字列のjsonをjsonだと判断できていないようです。(es6ではjsonであることを第二引数で渡していたけどなくなったみたい)
解決方法
ドキュメントをよくみると渡すのはオブジェクトになっていました
GetResponse<Product> response = esClient.get(g -> g
.index("products")
.id("bk-1"),
Product.class
);
ということでオブジェクトを渡せるようにして、その手前でjson文字列をオブジェクトにするように工夫しました
fun <T> putToIndex(index: String, id: String, obj: T) {
client.index { it.index(index).id(id).document(obj) }
}
fun insertDocument(index: String, id: String, json: String) {
val company = Gson().fromJson(json, SampleJson::class.java)
ElasticsearchV8Client.putToIndex(index, id, company)
}
おわりに
徐々にESに慣れてきて楽しくなってきました
参考