美文网首页
Elasticsearch Java REST高级客户端Sear

Elasticsearch Java REST高级客户端Sear

作者: 觉释 | 来源:发表于2020-10-29 15:58 被阅读0次

    1.matchAllQuery查询

    /**
         * matchAll查询
         */
        public static void matchAllQuery() {
            RestHighLevelClient client = getClient();
     
            // 这里可以不指定索引,也可以指定多个
            SearchRequest searchRequest = new SearchRequest("movies");
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchRequest.source(searchSourceBuilder);
            searchSourceBuilder.query(QueryBuilders.matchAllQuery());
            // 从第一条开始,包括第一条
            searchSourceBuilder.from(0);
            // 查询30条
            searchSourceBuilder.size(3);
            // 先按year倒排
            searchSourceBuilder.sort(SortBuilders.fieldSort("year").order(SortOrder.DESC));
            // 再按id正排
            searchSourceBuilder.sort("id");
            SearchResponse search = null;
            try {
                search = client.search(searchRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
            search.getHits().forEach(System.out::println);
            close(client);
        }
    

    2、matchQuery查询

    /**
         * match查询
         */
        public static void matchQuery() {
            RestHighLevelClient client = getClient();
     
            // 这里可以不指定索引,也可以指定多个
            SearchRequest searchRequest = new SearchRequest("movies");
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchRequest.source(searchSourceBuilder);
            MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("title", "you life");
            searchSourceBuilder.query(matchQueryBuilder);
    //      // 模糊查询
    //      matchQueryBuilder.fuzziness(Fuzziness.AUTO);
    //      // 前缀查询的长度
    //      matchQueryBuilder.prefixLength(3);
    //      // max expansion 选项,用来控制模糊查询
    //      matchQueryBuilder.maxExpansions(10);
            searchSourceBuilder.from(0);
            searchSourceBuilder.size(32);
            // 按评分排序
            searchSourceBuilder.sort("_score");
            SearchResponse search = null;
            try {
                search = client.search(searchRequest, RequestOptions.DEFAULT);
                search.getHits().forEach(System.out::println);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                close(client);
            }
        }
    

    3.termQuery查询

    public static void termQuery() {
            RestHighLevelClient client = getClient();
            SearchRequest request = new SearchRequest("movies");
            SearchSourceBuilder builder = new SearchSourceBuilder();
            request.source(builder);
            builder.from(0);
            builder.size(5);
            builder.query(QueryBuilders.termQuery("title", "you"));
            SearchResponse search = null;
            try {
                search = client.search(request, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
            search.getHits().forEach(System.out::println);
     
            close(client);
        }
    

    4、创建和关闭客户端

    /**
         * 关闭客户端
         * 
         * @param client
         */
        private static void close(RestHighLevelClient client) {
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 获取客户端
         * 
         * @return
         */
        private static RestHighLevelClient getClient() {
            RestHighLevelClient client = new RestHighLevelClient(
                    RestClient.builder(new HttpHost("elk-node01", 9200, "http"), new HttpHost("elk-node02", 9200, "http"),
                            new HttpHost("elk-node03", 9200, "http")));
            return client;
        }
    

    4、 maven

           <dependency>
                <groupId>org.elasticsearch.plugin</groupId>
                <artifactId>x-pack-sql-jdbc</artifactId>
                <version>7.2.0</version>
    <!--            <version>6.7.1</version> -->
            </dependency>
     
            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-high-level-client</artifactId>
                <version>7.2.0</version>
    <!--            <version>6.7.1</version> -->
            </dependency>
    
    

    相关文章

      网友评论

          本文标题:Elasticsearch Java REST高级客户端Sear

          本文链接:https://www.haomeiwen.com/subject/bfrdpktx.html