es查询用抽象工具类-JAVA

为优化ES查询代码,减少冗余,本文介绍了一种抽象出的Java工具类,能够简洁高效地处理搜索和推荐业务的查询操作。通过使用这个工具类,可以将原先复杂的多行代码简化为一行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

为精简目前查询es用到的方法,以及方便收归管理和提升代码规范,现抽象出一个工具类,用于搜索和推荐业务的查询。

老代码:

List<BiggieCatBean> biggieCatList = new ArrayList<>();
// try {
//     SearchResult searchResult = jestClient.execute(searchBiggie);
//     if (searchResult.isSucceeded()) {
//         List<SearchResult.Hit<BiggieCatBean, Void>> hits = searchResult.getHits(BiggieCatBean.class);
//         hits.forEach(h -> biggieCatList.add(h.source));
//     } else {
//         log.error("failed to get result from es:{},request:{}", searchResult.getErrorMessage(), JSONObject.toJSONString(searchBiggie));
//     }
// } catch (Exception e) {
//     log.error("[zjy_119]jest execute error:", e);
// }

可以看到注释中的代码比较多,而且在多个业务场景下使用的话,会非常冗余

新代码:

List<BiggieCatBean> biggieCatList = JestSearchUtil.getSearch(jestClient, searchBiggie, BiggieCatBean.class);

使用本工具类之后,只要一行代码就可以完成相应业务

工具类具体实现

/**
 * @author zoujunyuan
 */
@Slf4j
public class JestSearchUtil {
    /**
     * 常规的将查询内容存入集合中
     * @param jestClient
     * @param search 查询语句
     * @param sourceType source实体类型
     */
    public static <T> List<T> getSearch(JestClient jestClient, Search search, Class<T> sourceType) {
        try {
            SearchResult searchResult = jestClient.execute(search);
            if (searchResult.isSucceeded()) {
                return searchResult.getSourceAsObjectList(sourceType, true);
            } else {
                log.error("failed to get result from es:{},request:{}", searchResult.getErrorMessage(), JSONObject.toJSONString(search));
                return Lists.newArrayList();
            }
        } catch (Exception e) {
            log.error("jest execute failed :", e);
            return Lists.newArrayList();
        }
    }
 
    /**
     * 将查询内容进行处理得到所需字段的集合
     * @param jestClient
     * @param search 查询语句
     * @param sourceType source实体类型
     * @param identifier 处理的方法
     * @param <T>
     * @param <K> 所需集合的类型
     * @return
     */
    public static <T, K> List<K> getSearchWithIdentify(JestClient jestClient, Search search, Class<T> sourceType, Function<T, K> identifier) {
        try {
            SearchResult searchResult = jestClient.execute(search);
            if (searchResult.isSucceeded()) {
                List<T> sourceList = searchResult.getSourceAsObjectList(sourceType, true);
                return sourceList.stream().filter(Objects::nonNull).map(identifier).collect(Collectors.toList());
 
            } else {
                log.error("failed to get result from es:{},request:{}", searchResult.getErrorMessage(), JSONObject.toJSONString(search));
                return Lists.newArrayList();
            }
        } catch (Exception e) {
            log.error("jest execute failed or identify execute failed :", e);
            return Lists.newArrayList();
        }
    }
 
 
}

Q:如何使用第二种需要处理字段的方法?

A:

// 获取查询得到的toId并转成Long类型
JestSearchUtil.getSearchWithIdentify(jestClient, search, UserRelationBean.class, (k) -> Long.valueOf(k.getToId()));
// 获取查询得到的toId
JestSearchUtil.getSearchWithIdentify(jestClient, search, UserRelationBean.class, UserRelationBean::getToId);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值