java对es的操作主要是查询和聚合,想做上面两个操作先拼装DSL,然后执行DSL
封装查询DSL的两种方式
org.elasticsearch.search.builder SearchSourceBuilder(org.elasticsearch.index.query)
com.google.gson.JsonObject
今天讲第一种方式拼接DSL:(第二种主要拼接jsonarray,和方式一大致相同。)
常见的方式有
SearchSourceBuilder oSearchSrcBuilder=new SearchSourceBuilder();
BoolQueryBuilder oQuery=new BoolQueryBuilder();
中间用oQuery.must() 或者mustnot()或者shuould()一系列的api
封装oSearchSrcBuilder.query(oQuery);
聚合的api
AggregationBuilders .terms(sAggName.trim()) .field(sAggField).size(nSize);
//要加执行的索引
Search oSearch = new Search.Builder(oSearchSrcBuilder.toString()).addIndices(xxxx).build();
获取客户端的连接方式
if (client == null) {
JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(new HttpClientConfig
.Builder(getESHost())
.multiThreaded(true)
.defaultMaxTotalConnectionPerRoute(2)//Per default this implementation will create no more than 2 concurrent connections per given route
.maxTotalConnection(20) // and no more 20 connections in total
.readTimeout(180000)//设置读取超时时间
.build());
client = factory.getObject();
JestClient client = JestHelper.getClient();
执行DLS SearchResult searchResult = client.execute(oSearch);
好了 这就获得了你想要的结果集