26-RestHighLevelClient----高亮&分页&排序&指定返回字段

@SpringBootTest
public class RestHighLevelClientOpsTest {

    private RestHighLevelClient restHighLevelClient;

    @Autowired
    public RestHighLevelClientOpsTest(RestHighLevelClient restHighLevelClient) {
        this.restHighLevelClient = restHighLevelClient;
    }

    /**
     * 查询所有 分页查询 form 起始位置  size 每页展示记录的条数
     */
    @Test
    public void testSearch() throws IOException {
        SearchRequest searchRequest = new SearchRequest("products");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.matchAllQuery())
                .from(0)    //起始位置:start=(page-1)*size
                .size(1);   //每页显示条数

        searchRequest.source(sourceBuilder);

        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

        System.out.println("总条数:" + search.getHits().getTotalHits().value);
        System.out.println("获取文档最大得分:" + search.getHits().getMaxScore());

        SearchHit[] hits = search.getHits().getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
    }

    /**
     * 查询结果默认是按分数排序,我们可以手动指定排序规则
     * 排序:按价格排序
     */
    @Test
    public void testSort() throws IOException {
        SearchRequest searchRequest = new SearchRequest("products");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.matchAllQuery()).sort("price", SortOrder.ASC);
        searchRequest.source(sourceBuilder);

        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

        System.out.println("总条数:" + searchResponse.getHits().getTotalHits().value);
        System.out.println("最大得分:" + searchResponse.getHits().getMaxScore());

        SearchHit[] hits = searchResponse.getHits().getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
    }

    /**
     * 指定返回的字段
     */
    @Test
    public void testReturn() throws IOException {
        SearchRequest searchRequest = new SearchRequest("products");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

        //之所以设置两个数组,是因为如果我们字段很多的时候,我们业务需求只希望查询/排除很少一部分的字段,则只需要在查询/排除数组里面写那仅有的几个字段即可,省时省力
        sourceBuilder.query(QueryBuilders.matchAllQuery()).fetchSource(new String[]{"title"}, new String[]{});//参数1:包含的字段数组   参数2:排除的字段数组

        searchRequest.source(sourceBuilder);

        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

        System.out.println("总条数:" + searchResponse.getHits().getTotalHits().value);
        System.out.println("最大得分:" + searchResponse.getHits().getMaxScore());

        SearchHit[] hits = searchResponse.getHits().getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
    }

    /**
     * 高亮查询
     */
    @Test
    public void testHighlight() throws IOException {
        SearchRequest searchRequest = new SearchRequest("products");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

        //高亮器
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.requireFieldMatch(false)//因为高亮查询默认是对查询字段即description就行高亮,可以关闭字段匹配,这样就可以对查询到的多个字段(前提是有关键词并且改字段可以分词)进行高亮显示
                        .field("description")//若有关键字切可以分词,则可以高亮,写*可以匹配所有字段
                        .field("title")//若有关键字切可以分词,则可以高亮,写*可以匹配所有字段
                        .preTags("<span style='color:red;'>")//手动前缀标签
                        .postTags("</span>");

        sourceBuilder.query(QueryBuilders.termQuery("description", "好吃")).highlighter(highlightBuilder);///高亮搜索
        searchRequest.source(sourceBuilder);

        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        SearchHit[] hits = searchResponse.getHits().getHits();
        for (SearchHit hit : hits) {
            //获取高亮字段
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();

            //如果高亮字段包含在description中
            if (highlightFields.containsKey("description")){
                System.out.println("description中有这个字段");
                Text[] fragments = highlightFields.get("description").getFragments();
                for (Text fragment : fragments) {
                    System.out.println(fragment);
                }
            }

            //如果高亮字段包含在title中
            if (highlightFields.containsKey("title")){
                System.out.println("title中有这个字段");
                Text[] fragments = highlightFields.get("title").getFragments();
                for (Text fragment : fragments) {
                    System.out.println(fragment);
                }
            }
        }
    }
}
上一篇:python我的学习笔记(0)


下一篇:浅析 Netty 实现心跳机制与断线重连