how to sort elasticsearch results using jest

雨燕双飞 提交于 2019-12-25 09:01:44

问题


so i'm using jest to practice elasticsearch 5.2 have this:

@Override
public List<Aptitude> findAllAptitudes() {
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.matchAllQuery());

    Search search = new Search.Builder(searchSourceBuilder.toString()).addIndex(APTITUDE_INDEX_NAME).build();

    try {
        SearchResult result = JestClientUtils.getClient().execute(search);
        List<Hit<Aptitude, Void>> aptitudes = result.getHits(Aptitude.class);
        return aptitudes.stream().map(this::getAptitude).sorted(new Comparator<Aptitude>() {

            @Override
            public int compare(Aptitude o1, Aptitude o2) {
                return o1.getId().compareTo(o2.getId());
            }
        }).collect(Collectors.toList());
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}

private Aptitude getAptitude(Hit<Aptitude, Void> hit) {
    return hit.source;
}

but i want to get the results in order by ID (Long atribute from aptitude) but cant quite figure out how to. can somebody point me in the right direction? thanks in advance


回答1:


You don't need to sort your results in Java, you can simply ask ES to do it for you. You can add the third line below and remove all your Java Stream code.

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
searchSourceBuilder.sort("id", SortOrder.ASC);


来源:https://stackoverflow.com/questions/42261521/how-to-sort-elasticsearch-results-using-jest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!