在spring-data-elasticsearch中使用ElasticsearchTemplate获取计数和ID列表 [英] Getting count and list of ids using ElasticsearchTemplate in spring-data-elasticsearch

查看:93
本文介绍了在spring-data-elasticsearch中使用ElasticsearchTemplate获取计数和ID列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为项目使用spring-data-elasticsearch,以为其提供全文搜索功能.我们将真实数据保存在关系数据库中,并将相关元数据以及各自的ID保留在elasticsearch中.因此对于搜索结果,只需要id字段,因为将从关系数据库中检索实际数据.

I am using spring-data-elasticsearch for a project to provide it with full text search functionality. We keep the real data in a relational database and relevant metadata along with respective id in elasticsearch. So for search results, only id field is required as the actual data will be retrieved from the relational database.

我正在根据搜索条件构建搜索查询,然后执行queryForIds():

I am building the search query based on search criteria and then performing a queryForIds():

    SearchQuery searchQuery = new NativeSearchQueryBuilder()
            .withIndices(indexName)
            .withTypes(typeName)
            .withQuery(getQueryBuilder(searchParams))
            .withPageable(pageable)
            .build();

    return elasticsearchTemplate.queryForIds(searchQuery);

如果我还需要该特定searchQuery的总数,则可以执行另一个 elasticsearchTemplate.count(searchQuery)调用,但是据我所知这将是多余的.我认为有一种方法可以通过一次调用使用 elasticsearchTemplate.queryForPage()之类的方法来获取ID列表和总数.

If I also need the total count for that specific searchQuery, I can do another elasticsearchTemplate.count(searchQuery) call, but that will be redundant as I understand. I think there is a way to get both the list of id and total count by using something like elasticsearchTemplate.queryForPage() in a single call.

还可以在 queryForPage(SearchQuery query,Class< T> clazz,SearchResultMapper映射器)中使用未用 @Document 注释的自定义类吗?实际的文档类确实很大,并且如果我不确定传递大型类是否会给引擎带来任何额外负担,因为有100多个字段需要json映射,但是我所需要的只是id字段.无论如何,我将在查询生成器中有一个 .withFields("id").

Also, can I use a custom class in queryForPage(SearchQuery query, Class<T> clazz, SearchResultMapper mapper) which is not annotated with @Document? The actual document class is really big, and if I am not sure if passing large classes will cause any extra load on the engine since there are over 100 fields to be json mapped, but all I need is the id field. I will have a .withFields("id") in the query builder anyway.

推荐答案

如果要防止两次调用elasticsearch,我建议编写一个自定义的ResultsExtractor:

If you want to prevent two calls to elasticsearch, i would suggest to write an custom ResultsExtractor:

SearchQuery searchQuery = new NativeSearchQueryBuilder().withIndices(indexName)
        .withTypes(typeName)
        .withQuery(queryBuilder)
        .withPageable(pageable)
        .build();
SearchResult result = template.query(searchQuery, new ResultsExtractor<SearchResult>() {

    @Override
    public SearchResult extract(SearchResponse response) {
        long totalHits = response.getHits()
                .totalHits();
        List<String> ids = new ArrayList<String>();
        for (SearchHit hit : response.getHits()) {
            if (hit != null) {
                ids.add(hit.getId());
            }
        }
        return new SearchResult(ids, totalHits);
    }
});
System.out.println(result.getIds());
System.out.println(result.getCount());

其中SearchResult是自定义类:

where SearchResult is a custom class:

public class SearchResult {

    List<String> ids;
    long count;

    //getter and setter
}

通过这种方式,您可以从elasticsearch SearchResponse 中获取所需的信息.

This way you can get the information you need from the elasticsearch SearchResponse.

关于第二个问题:据我所知,当调用 queryForPage(SearchQuery query,Class< T> clazz,SearchResultMapper映射器)时不会检查传递的类的 @Document 批注.试试吧!

Regarding your second question: As far as I can see, when calling queryForPage(SearchQuery query, Class<T> clazz, SearchResultMapper mapper) the passed class is not checked for the @Document annotation. Just try it out!

这篇关于在spring-data-elasticsearch中使用ElasticsearchTemplate获取计数和ID列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆