当前位置: 代码迷 >> 综合 >> 1、searchResult.getTotal()查询es7中 报java.lang.UnsupportedOperationException: JsonObject错误
  详细解决方案

1、searchResult.getTotal()查询es7中 报java.lang.UnsupportedOperationException: JsonObject错误

热度:62   发布时间:2024-01-14 21:26:12.0

1、searchResult.getTotal()查询es7中 报java.lang.UnsupportedOperationException: JsonObject

利用jestclient查询es7中数据改方法会出现bug,这是源码的问题

因为es7返回json串的格式出现了一些改变

ES V7 以前格式
hits: {
total: 16561096
}ES7 现在返回格式
hits: {
total: {
value: 49
}
}

jestClient的SearchResult的getTotal()方法还在使用
public static final String[] PATH_TO_TOTAL = “hits/total”.split("/");
解析,因此在getTotal()不会解析成一个数值
其实应该变成
public static final String[] PATH_TO_TOTAL = “hits/total/value”.split("/");
解决方法

SearchResult searchResult = jestClient.execute(search);
JsonObject jsonObject = searchResult.getJsonObject();
JsonElement jsonElement = jsonObject.get("hits").getAsJsonObject().get("total").getAsJsonObject().get("value");
return  jsonElement.getAsLong();
  相关解决方案