BoolQueryBuilder 和 wildcardQuery withFilter 查询

一. BoolQueryBuilder查询说明

BoolQueryBuilder qb = QueryBuilders.boolQuery();

1. 返回的文档必须满足must子句的条件,并且参与计算分值

qb.must(QueryBuilder queryBuilder); 

2. 返回的文档必须不满足定义的条件

qb.mustNot(QueryBuilder queryBuilder) 

3. 返回的文档可能满足should子句的条件.在一个bool查询中,如果没有must或者filter,有一个或者多个should子句,那么只要满足一个就可以返回.minimum_should_match参数定义了至少满足几个子句.

qb.should(QueryBuilder queryBuilder)) 

4. 返回的文档必须满足filter子句的条件,但是不会像must一样,参与计算分值

qb.filter(QueryBuilder queryBuilder)) 

二. 实例

1. 利用BoolQueryBuilder的must,should实现精确查找

    /**
     * must,should查找
     * @param keyword
     * @return
     */
    @GetMapping("/blog/search/title")
    public JsonResult> searchTitle(String keyword) {
//        if (StringUtils.isEmpty(keyword))
//            throw new JsonResult("参数不能为空");

        // 精确查找
        // ElasticSearchTemplate加中文筛选后无结果的解决办法:title.keyword
        String field = "title.keyword";
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.should(QueryBuilders.termsQuery(field, keyword));
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(boolQueryBuilder)
                .withPageable(PageRequest.of(0, 100))
                .build();

        // 默认查找
//        SearchQuery searchQuery = new NativeSearchQueryBuilder()
//                .withQuery(QueryBuilders.queryStringQuery(keyword).defaultField(field))
//                .withPageable(PageRequest.of(0, 100))
//                .build();

        List list = elasticsearchTemplate.queryForList(searchQuery, BlogModel.class);
        return new JsonResult<>(list);
    }

2. 利用 wildcardQuery,实现模糊查询,?匹配单个字符,*匹配多个字符

QueryBuilders.wildcardQuery(field, "*"+keyword+"*")

/**
     * 模糊查找,wildcardQuery
     * @param keyword
     * @return
     */
    @GetMapping("/blog/search/wildCardTitle")
    public JsonResult> searchWildcardQueryTitle(String keyword) {
//        if (StringUtils.isEmpty(keyword))
//            throw new JsonResult("参数不能为空");

        // 模糊查找
        // ElasticSearchTemplate加中文筛选后无结果的解决办法:title.keyword
        String field = "title.keyword";
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.should(QueryBuilders.wildcardQuery(field, "*"+keyword+"*"));
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(boolQueryBuilder)
                .withPageable(PageRequest.of(0, 100))
                .build();

        List list = elasticsearchTemplate.queryForList(searchQuery, BlogModel.class);
        return new JsonResult<>(list);
    }

3.使用withFilter,对结果过滤:

不加过滤条件以前,返回结果:"中国","中国人"
加了过滤条件"中国人",返回结果:"中国人"

  /**
     * 模糊查找,wildcardQuery
     * @param keyword
     * @return
     */
    @GetMapping("/blog/search/wildCardTitle")
    public JsonResult> searchWildcardQueryTitle(String keyword) {
        // 模糊查找
        // ElasticSearchTemplate加中文筛选后无结果的解决办法:title.keyword
        String field = "title.keyword";
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.should(QueryBuilders.wildcardQuery(field, "*"+keyword+"*"));

        // 不加过滤条件以前,返回结果:"中国","中国人"
        // 加了过滤条件"中国人",返回结果:"中国人"
        BoolQueryBuilder filter =  QueryBuilders.boolQuery();
        filter.should(QueryBuilders.termsQuery(field, "中国人"));
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(boolQueryBuilder)
                .withPageable(PageRequest.of(0, 100))
                .withFilter(filter)
                .build();

        List list = elasticsearchTemplate.queryForList(searchQuery, BlogModel.class);
        return new JsonResult<>(list);
    }

BoolQueryBuilder 和 wildcardQuery withFilter 查询_第1张图片

BoolQueryBuilder 和 wildcardQuery withFilter 查询_第2张图片

 

 

你可能感兴趣的:(Java,java)