其他分享
首页 > 其他分享> > ES代码

ES代码

作者:互联网

搜索

查询全部match_all:

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

    /**
     * 查询所有match all
     * @throws IOException
     */
    @Test
    public  void testMatchAll() throws IOException {
        //创建查询构造器queryBuilder来指定查询matchAllQuery
        QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();

        //创建搜索构造器searchSourceBuilder
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //指定搜索构造器的查询方式为查询构造器queryBuilder指定查询matchAllQuery
        searchSourceBuilder.query(queryBuilder);
        //指定搜索构造器的分页信息
        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);
        }

        /*//看看命中了多少
        long value = searchHits.getTotalHits().value;
        System.out.println("总记录数:"+value);*/
    }

词条查询/精确查询term:

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

/**
     * 词条查询term
     * @throws IOException
     */
    @Test
    public  void testTerm() throws IOException {
        //创建查询构造器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);
        //指定搜索构造器的分页信息
        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:

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

    /**
     * 词条分词查询match
     * @throws IOException
     */
    @Test
    public  void testMatch() throws IOException {
        //创建查询构造器其父类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);
        //指定搜索构造器的分页信息
        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);
        }
    }

标签:goods,searchSourceBuilder,代码,指定,查询,搜索,searchRequest,ES
来源: https://www.cnblogs.com/maomaodesu/p/12631808.html