0

所以我用jest来练习elasticsearch 5.2 有这个:

@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;
}

但我想按 ID(来自 aptitude 的长属性)按顺序获得结果,但不知道如何去做。有人可以指出我正确的方向吗?提前致谢

4

1 回答 1

3

你不需要在 Java 中对结果进行排序,你可以简单地让 ES 为你做。您可以添加下面的第三行并删除所有 Java Stream 代码。

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
searchSourceBuilder.sort("id", SortOrder.ASC);
于 2017-02-16T04:33:18.177 回答