3
0

ElasticSearchでompressor detection can only be called on some xcontent bytes or compressed xcontent bytesエラーが出る

Posted at

はじめに

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に慣れてきて楽しくなってきました

参考

3
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
3
0