ES代码

范围查询


 /**
     * 范围查询range
     * @throws IOException
     */
    @Test
    public  void testRange() throws IOException {
        ///创建构造器
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("price");
        //指定下限2000,上限3000
        rangeQueryBuilder.gte(2000);
        rangeQueryBuilder.lte(3000);
        ///创建搜索构造器searchSourceBuilder
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //指定搜索构造器的查询方式
        searchSourceBuilder.query(rangeQueryBuilder);
        //指定搜索构造器的排序方式
        searchSourceBuilder.sort("price", SortOrder.ASC);
        //指定搜索构造器的分页信息
        searchSourceBuilder.from(0);
        searchSourceBuilder.size(100);
        ///创建搜索请求对象searchRequest指定搜索的索引名称goods
        SearchRequest searchRequest = new SearchRequest("goods");
        //搜索请求对象指定搜索构造器searchSourceBuilder为我们要使用的搜索工具
        searchRequest.source(searchSourceBuilder);
        ///创建搜索返回对象拿到指定搜索请求的搜索结果
        SearchResponse searchResponse = client.search(searchRequest,RequestOptions.DEFAULT);
        ///创建搜索命中数对象从搜索返回对象中拿到命中数
        SearchHits searchHits = searchResponse.getHits();
        ///创建一个集合
        List<Goods> goodsList = new ArrayList<>();
        //以集合方式获取命中的数据
        for (SearchHit searchHit : searchHits) {
            //获取json字符串格式的数据
            String sourceAsString = searchHit.getSourceAsString();
            //字符串转为java对象
            Goods goods  = JSON.parseObject(sourceAsString,Goods.class);
            goodsList.add(goods);
        }
        ///打印一下
        for (Goods goods : goodsList) { System.out.println(goods); }
    }

查询全部match_all:

查询全部数据,一般不会用这个。

        //创建查询构造器queryBuilder来指定查询matchAllQuery
        QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();

        //创建搜索构造器searchSourceBuilder
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //指定搜索构造器的查询方式为查询构造器queryBuilder指定查询matchAllQuery
        searchSourceBuilder.query(queryBuilder);

词条查询/精确查询term:

完全匹配,搜索“小米手机”那么结果必须是“小米手机”这四个字连着,如果搜索“小米iphoneXR”则什么结果都不会返回(除非数据库里有数据真的写的是“小米iphoeneXR”),一般用于选择品牌,高级选项等位置。

        //创建查询构造器queryBuilder来指定查询termQuery
        //QueryBuilder queryBuilder = QueryBuilders.termQuery("title","小米");
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.filter(QueryBuilders.termQuery("title","华为"));
        //创建搜索构造器searchSourceBuilder
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //指定搜索构造器的查询方式为查询构造器queryBuilder指定查询termQuery
        //searchSourceBuilder.query(queryBuilder);
        searchSourceBuilder.query(boolQueryBuilder);

分词查询match:

先分词,比如搜索“小米手机”,通过ik分词器分为“小米”和“手机”,“小米”和“手机”有两种逻辑,一种是and,一种是or
如果选择and逻辑:
搜索结果为“xxxx小米xxxxxxxx手机xxxx”,结果较少。
如果选择or逻辑:
搜索结果为“xxxx小米xxxxxxxx手机xxxx”,“xxxxxx小米xxxx”,“xxxxx手机xxxx”,结果较多。

        //创建查询构造器其父类MatchQueryBuilder来指定查询*Query
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("title","小米手机");
        //and逻辑,or逻辑在后面改为OR即可
        matchQueryBuilder.operator(Operator.AND);

        //创建搜索构造器searchSourceBuilder
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //指定搜索构造器的查询方式为查询构造器queryBuilder指定查询*Query
        //searchSourceBuilder.query(queryBuilder);
        searchSourceBuilder.query(matchQueryBuilder);

模糊查询wildcard:

        ///创建模糊查询构造器wildcardQueryBuilder来指定查询*Query
        WildcardQueryBuilder wildcardQueryBuilder = QueryBuilders.wildcardQuery("title","小*");
        ///创建搜索构造器searchSourceBuilder
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //指定搜索构造器的查询方式为模糊查询构造器wildcardQueryBuilder
        searchSourceBuilder.query(wildcardQueryBuilder);

正则查询Regexp:

        ///创建构造器
        RegexpQueryBuilder regexpQueryBuilder = QueryBuilders.regexpQuery("title", "\\w+(.)*");
        ///创建搜索构造器searchSourceBuilder
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //指定搜索构造器的查询方式
        searchSourceBuilder.query(regexpQueryBuilder);

前缀查询:

        ///创建构造器
        PrefixQueryBuilder prefixQueryBuilder = QueryBuilders.prefixQuery("brandName","小");
        ///创建搜索构造器searchSourceBuilder
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //指定搜索构造器的查询方式
        searchSourceBuilder.query(prefixQueryBuilder);

多字段查询queryString:

///创建构造器
        QueryStringQueryBuilder queryStringQueryBuilder = QueryBuilders.queryStringQuery("小米手机").field("title").field("categoryName").field("brandName").defaultOperator(AND);
        ///创建搜索构造器searchSourceBuilder
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //指定搜索构造器的查询方式
        searchSourceBuilder.query(queryStringQueryBuilder);
        //指定搜索构造器的排序方式
        searchSourceBuilder.sort("price", SortOrder.ASC);

多条件拼接boolQuery:

must筛选并计算分支:
should:
must_not:
filter筛选并不计算分值,效率高,推荐使用:

        ///创建各个查询条件:品牌brandName
        QueryBuilder queryTermBuilder = QueryBuilders.termQuery("brandName","小米");
        ///创建各个查询条件:标题title
        QueryBuilder queryMatchQuery = QueryBuilders.matchQuery("title","手机");
        ///创建各个查询条件:价格范围priceRange
        RangeQueryBuilder priceRangeQueryBuilder = QueryBuilders.rangeQuery("price");
        ///创建布尔查询构造器
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(queryTermBuilder);
        boolQueryBuilder.filter(queryMatchQuery);
        boolQueryBuilder.filter(priceRangeQueryBuilder);
        ///创建搜索构造器searchSourceBuilder
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //指定搜索构造器的查询方式
        searchSourceBuilder.query(boolQueryBuilder);

聚合查询——桶聚合Agg:

    public  void testAgg() throws IOException {
        ///创建构造器
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("title", "手机");
        ///创建聚合构造器
        AggregationBuilder aggregationBuilder = AggregationBuilders.terms("goods_brands").field("brandName").size(100);
        ///创建搜索构造器searchSourceBuilder
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //指定搜索构造器的查询方式
        searchSourceBuilder.query(matchQueryBuilder);
        //指定搜索构造器查询聚合
        searchSourceBuilder.aggregation(aggregationBuilder);
        //指定搜索构造器的分页信息
        searchSourceBuilder.from(0);
        searchSourceBuilder.size(100);
        ///创建搜索请求对象searchRequest指定搜索的索引名称goods
        SearchRequest searchRequest = new SearchRequest("goods");
        //搜索请求对象指定搜索构造器searchSourceBuilder为我们要使用的搜索工具
        searchRequest.source(searchSourceBuilder);
        ///创建搜索返回对象拿到指定搜索请求的搜索结果
        SearchResponse searchResponse = client.search(searchRequest,RequestOptions.DEFAULT);
        ///创建搜索命中数对象从搜索返回对象中拿到命中数
        SearchHits searchHits = searchResponse.getHits();
        ///创建一个集合
        List<Goods> goodsList = new ArrayList<>();
        //以集合方式获取命中的数据
        for (SearchHit searchHit : searchHits) {
            //获取json字符串格式的数据
            String sourceAsString = searchHit.getSourceAsString();
            //字符串转为java对象
            Goods goods  = JSON.parseObject(sourceAsString,Goods.class);
            goodsList.add(goods);
        }
        ///打印一下
        for (Goods goods : goodsList) { System.out.println(goods); }

        //获取聚合结果
        Aggregations aggregations = searchResponse.getAggregations();
        Map<String, Aggregation> aggregationMap = aggregations.asMap();
        Terms goods_brands = (Terms)aggregationMap.get("goods_brands");
        List<? extends Term.Bucket>  buckets = goods_brands.getBuckets();
        List brands = new ArrayList();
        for (Term.Bucket bucket : buckets) {
            Object key = bucket.getKey();
            brands.add(key);
        }
        for (Term.Bucket bucket : buckets) {
            System.out.println(brands);
        }
    }

高亮聚合highlight:

    public  void testHighlight() throws IOException {
        ///创建构造器
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("title","手机");
        ///创建高亮构造器和设置三要素
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field("title");
        highlightBuilder.preTags("<font color='red'>");
        highlightBuilder.postTags("</font>");
        ///创建搜索构造器searchSourceBuilder
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //指定搜索构造器的查询方式
        searchSourceBuilder.query(matchQueryBuilder);
        //指定搜索构造器的高亮模式
        searchSourceBuilder.highlighter(highlightBuilder);
        //指定搜索构造器的分页信息
        searchSourceBuilder.from(0);
        searchSourceBuilder.size(100);
        ///创建搜索请求对象searchRequest指定搜索的索引名称goods
        SearchRequest searchRequest = new SearchRequest("goods");
        //搜索请求对象指定搜索构造器searchSourceBuilder为我们要使用的搜索工具
        searchRequest.source(searchSourceBuilder);
        ///创建搜索返回对象拿到指定搜索请求的搜索结果
        SearchResponse searchResponse = client.search(searchRequest,RequestOptions.DEFAULT);
        ///创建搜索命中数对象从搜索返回对象中拿到命中数
        SearchHits searchHits = searchResponse.getHits();
        ///创建一个集合
        List<Goods> goodsList = new ArrayList<>();
        //以集合方式获取命中的数据
        for (SearchHit searchHit : searchHits) {
            //获取json字符串格式的数据
            String sourceAsString = searchHit.getSourceAsString();
            //字符串转为java对象
            Goods goods  = JSON.parseObject(sourceAsString,Goods.class);
            //获取高亮结果,替换goods中的title
            Map<String, HighlightField> highlightFieldMap = searchHit.getHighlightFields();
            HighlightField highlightField = highlightFieldMap.get("title");
            Text[] fragments = highlightField.fragments();
            goods.setTitle(fragments[0].toString());
            goodsList.add(goods);
        }
        ///打印一下
        for (Goods goods : goodsList) { System.out.println(goods); }
    }
posted @ 2020-04-04 14:45  卯毛  阅读(471)  评论(0编辑  收藏  举报