elasticsearch排序查询数据restful api以及java代码实现
restful api实现如下:
post:http://192.168.1.111:9200/film/dongzuo/_search/
{
"sort":[
{"publishDate":{"order":"desc"}}
]
}
根据发布日期降序排列;
Java代码实现:
/** * 排序查询 * @throws Exception */ @Test public void searchOrderBy()throws Exception{ SearchRequestBuilder srb=client.prepareSearch("film").setTypes("dongzuo"); SearchResponse sr=srb.setQuery(QueryBuilders.matchAllQuery()) .addSort("publishDate", SortOrder.DESC) .execute() .actionGet(); // 分页排序所有 SearchHits hits=sr.getHits(); for(SearchHit hit:hits){ System.out.println(hit.getSourceAsString()); } }
执行输出: