ElasticSearch

ElasticSearch 中boolQueryBuilder的

2021-02-23  本文已影响0人  小张同学99
public void () throws IOException {
    SearchRequest searchRequest = new SearchRequest("item");
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    
    boolQueryBuilder.must(QueryBuilders.matchQuery("scompCode", "G0000001"));
    // 模糊查询
    boolQueryBuilder.filter(QueryBuilders.wildcardQuery("itemDesc", "*手机*"));
    // 范围查询 from:相当于闭区间; gt:相当于开区间(>) gte:相当于闭区间 (>=) lt:开区间(<) lte:闭区间 (<=)
    boolQueryBuilder.filter(QueryBuilders.rangeQuery("itemPrice").from(4500).to(8899));
    sourceBuilder.query(boolQueryBuilder);

    searchRequest.source(sourceBuilder);
    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    logger.info("查询数据:{}", Arrays.toString(searchResponse.getHits().getHits()));
}
        BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        boolBuilder.filter(QueryBuilders.termQuery("mobile", AppConfigConstants.MONTH_REPORT_TOTAL_USER));
        sourceBuilder.query(boolBuilder);

        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices(AppConfigConstants.MONTH_REPORT_ES_INDEX);
        searchRequest.source(sourceBuilder);
        SearchResponse response = null;
        try {
            response = highLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            logger.error("[ElasticSearchService] countTotalUserNum error!", e);
            throw new InsurfaceException(InsurfaceCodeEnum.ES_QUERY_ERROR);
        }
        BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        boolBuilder.filter(QueryBuilders.rangeQuery("num").gte(userPolicyNum));
        sourceBuilder.query(boolBuilder);

        CountRequest countRequest = new CountRequest();
        countRequest.indices(AppConfigConstants.MONTH_REPORT_ES_INDEX);
        countRequest.source(sourceBuilder);
        CountResponse count = null;
        try {
            count = highLevelClient.count(countRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            logger.error("[ElasticSearchService] search error!", e);
            throw new InsurfaceException(InsurfaceCodeEnum.ES_QUERY_ERROR);
        }
上一篇 下一篇

猜你喜欢

热点阅读