elasticsearch high level rest api分页查询数据

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

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


		
		    org.elasticsearch
		    elasticsearch
		    7.3.0
		
		
		
		
		    org.elasticsearch.client
		    elasticsearch-rest-high-level-client
		    7.3.0
		
		
		
		
		    org.elasticsearch.client
		    elasticsearch-rest-client
		    7.3.0
		


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查询.

上一篇: Centos7 下测试mosquitto性能
下一篇: Linux下top+pstack+gdb的组合拳定位程序进程线程问题并调试
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章来源于互联网, 若有侵权, 联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布文章.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号