阅读文本大概需要3分钟。

Elasticsearch查到多种多样

1、matchAllQuery查询所有索引

public static void matchAllQueryTest(RestHighLevelClient client) throws Exception{

    // 1、创建search请求

    SearchRequest searchRequest = new SearchRequest();

    searchRequest.indices("it"); //索引名称

    searchRequest.types("_doc");//类型名称

    // 2、用SearchSourceBuilder来构造查询请求体 ,请仔细查看它的方法,构造各种查询的方法都在这。

    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

    //搜索全部文档

    sourceBuilder.query(QueryBuilders.matchAllQuery());



    //将请求体加入到请求中

    searchRequest.source(sourceBuilder);

    //3、发送请求

    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

    SearchHits hits = searchResponse.getHits();

    SearchHit[] searchHits = hits.getHits();

    for (SearchHit searchHit : searchHits) {

        String source = searchHit.getSourceAsString();

        System.out.println(source);

    }

}

 

测试结果:

{"bookName":"C++","publishDate":"2019-06-30T11:45:00.985Z","sales":"100.00"}
{"bookName":"java","publishDate":"2013-01-30","sales":"100.00"}
{"bookName":"python","publishDate":"2019-06-30T11:45:00.735Z","sales":"100.00","reason":"dailyupdate","updated":"2019-07-13T02:51:27.915Z"}

 

2、matchQuery(filedName,value)匹配单个字段,匹配字段名为filedName,值为value的文档

public static void matchQueryTest(RestHighLevelClient client) throws Exception{

    // 1、创建search请求

    SearchRequest searchRequest = new SearchRequest();

    searchRequest.indices("it"); //索引名称

    searchRequest.types("_doc");//类型名称

    // 2、用SearchSourceBuilder来构造查询请求体 ,请仔细查看它的方法,构造各种查询的方法都在这。

    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

    //搜索全部文档

    sourceBuilder.query(QueryBuilders.matchQuery("bookName", "java"));



    //将请求体加入到请求中

    searchRequest.source(sourceBuilder);

    //3、发送请求

    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

    SearchHits hits = searchResponse.getHits();

    SearchHit[] searchHits = hits.getHits();

    for (SearchHit searchHit : searchHits) {

        String source = searchHit.getSourceAsString();

        System.out.println(source);

    }

}

 

测试结果:

{"bookName":"java","publishDate":"2013-01-30","sales":"100.00"}

 

3multiMatchQuery(Object text, String... fieldNames)多个字段匹配某一个值

public static void multiMatchQueryTest(RestHighLevelClient client) throws Exception{

    // 1、创建search请求

    SearchRequest searchRequest = new SearchRequest();

    searchRequest.indices("it"); //索引名称

    searchRequest.types("_doc");//类型名称

    // 2、用SearchSourceBuilder来构造查询请求体 ,请仔细查看它的方法,构造各种查询的方法都在这。

    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

    //搜索文档

    QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery("java",

            "bookName", "sales");

    sourceBuilder.query(queryBuilder);



    //将请求体加入到请求中

    searchRequest.source(sourceBuilder);

    //3、发送请求

    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

    SearchHits hits = searchResponse.getHits();

    SearchHit[] searchHits = hits.getHits();

    for (SearchHit searchHit : searchHits) {

        String source = searchHit.getSourceAsString();

        System.out.println(source);

    }

}

 

测试结果

{"bookName":"java","publishDate":"2013-01-30","sales":"100.00"}

 

4、        wildcardQuery()模糊查询;模糊查询,?匹配单个字符,*匹配多个字符

public static void wildcardQueryTest(RestHighLevelClient client) throws Exception{

        // 1、创建search请求

        SearchRequest searchRequest = new SearchRequest();

        searchRequest.indices("it"); //索引名称

        searchRequest.types("_doc");//类型名称

        // 2、用SearchSourceBuilder来构造查询请求体 ,请仔细查看它的方法,构造各种查询的方法都在这。

        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

        //搜索文档

        //搜索名字中含有python文档(bookName中只要包含pyton即可)

//        WildcardQueryBuilder queryBuilder = QueryBuilders.wildcardQuery("bookName",

//                "*python*");



        WildcardQueryBuilder queryBuilder = QueryBuilders.wildcardQuery("reason",

                "*update*");



        sourceBuilder.query(queryBuilder);



        //将请求体加入到请求中

        searchRequest.source(sourceBuilder);

        //3、发送请求

        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        SearchHits hits = searchResponse.getHits();

        SearchHit[] searchHits = hits.getHits();

        for (SearchHit searchHit : searchHits) {

            String source = searchHit.getSourceAsString();

            System.out.println(source);

        }

    }

测试结果:

{"bookName":"python","publishDate":"2019-06-30T11:45:00.735Z","sales":"100.00","reason":"dailyupdate","updated":"2019-07-13T02:51:27.915Z"}

 

5、        BoolQueryBuilder复合查询

public static void boolQueryBuilderTest(RestHighLevelClient client) throws Exception{

    // 1、创建search请求

    SearchRequest searchRequest = new SearchRequest();

    searchRequest.indices("it"); //索引名称

    searchRequest.types("_doc");//类型名称

    // 2、用SearchSourceBuilder来构造查询请求体 ,请仔细查看它的方法,构造各种查询的方法都在这。

    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

    //搜索文档

    //模糊查询  搜索名字中含有pytond的文档

    WildcardQueryBuilder queryBuilder1 = QueryBuilders.wildcardQuery(

            "bookName", "*python*");

    WildcardQueryBuilder queryBuilder2 = QueryBuilders.wildcardQuery(

            "reason", "*update*");//搜索interest中含有read的文档



    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

    //bookName中必须含有python,reason中必须含有update,相当于and

    boolQueryBuilder.must(queryBuilder1);

    boolQueryBuilder.must(queryBuilder2);



    sourceBuilder.query(boolQueryBuilder);



    //将请求体加入到请求中

    searchRequest.source(sourceBuilder);

    //3、发送请求

    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

    SearchHits hits = searchResponse.getHits();

    SearchHit[] searchHits = hits.getHits();

    for (SearchHit searchHit : searchHits) {

        String source = searchHit.getSourceAsString();

        System.out.println(source);

    }

}

测试结果:

{"bookName":"python","publishDate":"2019-06-30T11:45:00.735Z","sales":"100.00","reason":"dailyupdate","updated":"2019-07-13T02:51:27.915Z"}

 

 

public static void boolQueryBuilderTest(RestHighLevelClient client) throws Exception{

        // 1、创建search请求

        SearchRequest searchRequest = new SearchRequest();

        searchRequest.indices("it"); //索引名称

        searchRequest.types("_doc");//类型名称

        // 2、用SearchSourceBuilder来构造查询请求体 ,请仔细查看它的方法,构造各种查询的方法都在这。

        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

        //搜索文档

        //搜索bookName中含有pytond的文档

         WildcardQueryBuilder queryBuilder1 = QueryBuilders.wildcardQuery(

                "bookName", "*python*");

        //搜索reason中含有update的文档

        WildcardQueryBuilder queryBuilder2 = QueryBuilders.wildcardQuery(

                "bookName", "*java*");



        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

        //bookName中含有python或者bookName含有java,相当于or

        boolQueryBuilder.should(queryBuilder1);

        boolQueryBuilder.should(queryBuilder2);



        sourceBuilder.query(boolQueryBuilder);



        //将请求体加入到请求中

        searchRequest.source(sourceBuilder);

        //3、发送请求

        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        SearchHits hits = searchResponse.getHits();

        SearchHit[] searchHits = hits.getHits();

        for (SearchHit searchHit : searchHits) {

            String source = searchHit.getSourceAsString();

            System.out.println(source);

        }

    }

测试结果:

{"bookName":"java","publishDate":"2013-01-30","sales":"100.00"}
{"bookName":"python","publishDate":"2019-06-30T11:45:00.735Z","sales":"100.00","reason":"dailyupdate","updated":"2019-07-13T02:51:27.915Z"}

11、使用Java High Level REST Client操作elasticsearch_搜索