Java操作ElasticSearch(四、排序、高亮、分页、Filter过滤、source筛选)
排序
通过 SearchSourceBuilder 的 sort(String, SortOrder) 方法用来实现排序条件的封装
@Test public void test18() throws IOException { SearchRequest request = new SearchRequest("user"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.matchQuery("note", "Java")); // 对搜索到的结果按年龄升序排序 searchSourceBuilder.sort(new FieldSortBuilder("age").order(SortOrder.ASC)); request.source(searchSourceBuilder); SearchResponse response = client.search(request, RequestOptions.DEFAULT); SearchHits searchHits = response.getHits(); System.out.println("命中数:" + searchHits.getTotalHits().value); SearchHit[] hits = searchHits.getHits(); for (SearchHit hit : hits) { String sourceAsString = hit.getSourceAsString(); User user = JSON.parseObject(sourceAsString, User.class); System.out.println(user); } }
高亮
@Test public void test20() throws IOException { SearchRequest request = new SearchRequest("user"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.matchQuery("note", "唱歌表演")); // 高亮,指定高亮字段 searchSourceBuilder.highlighter(new HighlightBuilder().field("note")); request.source(searchSourceBuilder); SearchResponse response = client.search(request, RequestOptions.DEFAULT); SearchHits searchHits = response.getHits(); System.out.println("命中数:" + searchHits.getTotalHits().value); SearchHit[] hits = searchHits.getHits(); for (SearchHit hit : hits) { String sourceAsString = hit.getSourceAsString(); User user = JSON.parseObject(sourceAsString, User.class); HighlightField note = hit.getHighlightFields().get("note"); user.setNote(note.getFragments()[0].toString()); System.out.println(user); } }
分页
在原来查询的基础上,给 SearchSourceBuilder 中添加 from 和 size 即可。
page = 1、size = 5,代表查询第一页,每页5条,可以计算出:from = (page - 1) * size = 0
@Test public void test19() throws IOException { SearchRequest request = new SearchRequest("user"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.matchAllQuery()); // from 开始索引 // size 每页显示几条 searchSourceBuilder.from(0); searchSourceBuilder.size(5); // 对搜索到的结果按年龄升序排序 searchSourceBuilder.sort(new FieldSortBuilder("age").order(SortOrder.ASC)); request.source(searchSourceBuilder); SearchResponse response = client.search(request, RequestOptions.DEFAULT); SearchHits searchHits = response.getHits(); System.out.println("命中数:" + searchHits.getTotalHits().value); SearchHit[] hits = searchHits.getHits(); for (SearchHit hit : hits) { String sourceAsString = hit.getSourceAsString(); User user = JSON.parseObject(sourceAsString, User.class); System.out.println(user); } }
filter过滤
添加 filter 条件,不参与打分
source筛选
在原来搜索的基础上,通过 SearchSourceBuilder 的 fetchSource(String[] includes, String[] excludes)方法实现
- includes 包含的字段
- excludes 要排除的字段
@Test public void test17() throws IOException { SearchRequest request = new SearchRequest("user"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.matchQuery("note", "Java")); // 不要id、age searchSourceBuilder.fetchSource(new String[0], new String[]{"id", "age"}); request.source(searchSourceBuilder); SearchResponse response = client.search(request, RequestOptions.DEFAULT); SearchHits searchHits = response.getHits(); System.out.println("命中数:" + searchHits.getTotalHits()); SearchHit[] hits = searchHits.getHits(); for (SearchHit hit : hits) { String sourceAsString = hit.getSourceAsString(); User user = JSON.parseObject(sourceAsString, User.class); System.out.println(user); } }