ElasticSearch版本升级,JestClient适配

1 篇文章 0 订阅

背景:es原版本为6.6.2,现场项目要求升级至es7.17.1,项目使用jest实现es调用

问题点 1:创建mapping

升级前代码

public static boolean createIndexMapping(String indexName, String typeName, String source) throws Exception {

        PutMapping putMapping = new PutMapping.Builder(indexName, typeName, source).build();
        JestResult jr = jestClientImpl.execute(putMapping);

        logger.info(typeName + " mapping result : " + jr.getJsonString());
        return jr.isSucceeded();
    }

升级后代码

public static boolean createIndexMapping(String indexName, String typeName, String source) throws Exception {

        PutMapping putMapping = new PutMapping.Builder(indexName, typeName, source).setParameter("include_type_name", true).build();
        JestResult jr = jestClientImpl.execute(putMapping);

        logger.info(typeName + " mapping result : " + jr.getJsonString());
        return jr.isSucceeded();
    }

问题2:查询问题

升级前代码

SearchResult result = jestClient.execute(search);

searchResult.getTotal()

此处,由于es6系与7系返回json结构不一致,导致版本兼容问题,此处使用的jest的版本6.3.1,getTotal方法解析有误

源代码

public Long getTotal() {
        Long total = null;
        JsonElement obj = this.getPath(PATH_TO_TOTAL); //String[] PATH_TO_TOTAL = "hits/total".split("/")
        if (obj != null) {
            total = obj.getAsLong();
        }

        return total;
    }

解决思路,通过继承SearchResult,重写getTotal方法,解决报错

public class LocalSearchResult extends SearchResult {

    @Override
    public Long getTotal() {
        return this.getJsonObject().getAsJsonObject("hits").getAsJsonObject("total").get("value").getAsLong();
    }

    public AiccSearchResult(SearchResult searchResult) {
        super(searchResult);
    }

    public AiccSearchResult(Gson gson) {
        super(gson);
    }
}

经过验证,问题解决

转载请说明出处

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用Elasticsearch的聚合(Aggregation)功能来实现查询某一个字段的出现次数。以下是使用JestClient进行查询的Java代码示例: ```java import io.searchbox.client.JestClient; import io.searchbox.core.Search; import io.searchbox.core.SearchResult; import io.searchbox.params.Parameters; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.metrics.sum.Sum; import java.io.IOException; public class ElasticsearchQuery { public static void main(String[] args) throws IOException { // 创建JestClient实例 JestClient jestClient = JestClientFactoryUtil.getJestClient(); // 构建查询语句 Search search = new Search.Builder("{\n" + " \"query\": {\n" + " \"match_all\": {}\n" + " },\n" + " \"aggs\": {\n" + " \"count_by_field\": {\n" + " \"terms\": {\n" + " \"field\": \"field_name.keyword\"\n" + // 要查询的字段名 " }\n" + " }\n" + " }\n" + "}\n") .addIndex("index_name") .addType("doc_type") .setParameter(Parameters.SIZE, 0) .build(); // 执行查询并获取结果 SearchResult result = jestClient.execute(search); // 处理结果 Terms terms = result.getAggregations().getTermsAggregation("count_by_field"); for (Terms.Bucket bucket : terms.getBuckets()) { String fieldValue = bucket.getKeyAsString(); long count = bucket.getDocCount(); System.out.println(fieldValue + "出现次数:" + count); } // 关闭JestClient实例 jestClient.shutdownClient(); } } ``` 在上面的代码示例中,我们使用了Elasticsearch的聚合功能来统计某一个字段的出现次数。具体来说,我们通过`AggregationBuilders.terms()`创建了一个Terms聚合,指定了要统计的字段名`field_name.keyword`,并将该聚合命名为`count_by_field`。在执行查询后,我们可以使用`result.getAggregations().getTermsAggregation("count_by_field")`方法获取该聚合的结果,然后遍历每一个Bucket,得到字段值和出现次数。 需要注意的是,由于我们只需要获取聚合结果而不需要查询的具体文档,因此可以将查询的`size`设置为0,从而减少查询的开销。另外,我们也可以使用`QueryBuilders`类来构建查询语句,以实现更复杂的查询需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值