elasticsearch high level rest api分页查询数据

Transport方式查询数据,在今后的elasticsearch中将不在维护,官方推荐用用 high level rest api或者lower level api去操作elasticsearch中的数据。在elasticsearch的增删改查操作中,其实最复杂的也就是分页查询了,根据elasticsearch官方资料,做个简单的笔记.

1. 准备jar包, 我用的elasticsearch 7.3版本, 各版本需要匹配.
程序代码 程序代码

<!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.3.0</version>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-high-level-client -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.3.0</version>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-client -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>7.3.0</version>
        </dependency>


2. 编写测试代码:
程序代码 程序代码

import java.io.IOException;

import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;

public class ElasticSearchTest {
    
    public final String ES_URL = "127.0.0.1";
    
    public final int ES_PORT = 9200;
    
    
    public static RestHighLevelClient getClientConnection() {
        
        RestHighLevelClient client = new RestHighLevelClient(
            RestClient.builder(
                    new HttpHost("localhost", 9200, "http")
                    )
            );
        
        return client;
    }
    
    public static void searchById() throws IOException {
        RestHighLevelClient client = getClientConnection();
        GetRequest getRequest = new GetRequest("gateway_log", "DceJqGwBqlIig5BB05Z-");
        GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
        System.out.println(getResponse.getSourceAsString());
        client.close();
    }
    
    /**
     * https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-search.html
     *
     * @throws IOException
     */
    public static void paginationSearch() throws IOException {
        
        SearchRequest searchRequest = new SearchRequest();
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.matchPhraseQuery("eventType", "WAN_ONOFF"));
        sourceBuilder.from(0);
        sourceBuilder.size(1);
        sourceBuilder.timeout(new TimeValue(1000));
        sourceBuilder.trackTotalHits(true);
        searchRequest.source(sourceBuilder);
        
        
        RestHighLevelClient client = getClientConnection();
        SearchResponse response = client.search(new SearchRequest("gateway_log")
                .source(sourceBuilder), RequestOptions.DEFAULT);
      
        System.out.println(response.toString());
        client.close();
    }
    
    public static void paginationSearch2() throws IOException {
        RestHighLevelClient client = getClientConnection();
        
        
        BoolQueryBuilder boolQuery = new BoolQueryBuilder();        
        
        RangeQueryBuilder rangeQuery= QueryBuilders.rangeQuery("count").gte(8);
        boolQuery.filter(rangeQuery);        
        
        MatchQueryBuilder matchQuery = new MatchQueryBuilder("eventType", "WAN_ONOFF");
        boolQuery.must(matchQuery);    
        
        SearchResponse response = client.search(new SearchRequest("gateway_log")
                .source(new SearchSourceBuilder()
                        .query(boolQuery)
                        .from(0)
                        .size(2)
                        .trackTotalHits(true)
                ), RequestOptions.DEFAULT);

        System.out.println(response.getHits().getTotalHits());
        System.out.println(response.toString());
        client.close();
    }
    
    
    public static void main(String[] args) throws IOException {
        //searchById();
        //paginationSearch();
        paginationSearch2();
    }
    
}


其中 paginationSearch2 其实就相当于如下这个DSL查询.



除非申明,文章均为一号门原创,转载请注明本文地址,谢谢!
[本日志由 yihaomen 于 2019-08-20 03:44 PM 编辑]
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: elasticsearch
相关日志:
评论: 0 | 引用: 0 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.