展开全部
我刚刚特意写的,用2个Map作为传参,一62616964757a686964616fe4b893e5b19e31333363383363个是must,一个是should。代码如下:
//创建查询
SearchRequestBuilder srb = client.prepareSearch(INDEX);
srb.setTypes(ASK_TYPE);
srb.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
//分页
srb.setFrom((pageNo - 1) * pageSize).setSize(pageSize);
//按时间倒序
SortBuilder sortBuilder = SortBuilders.fieldSort("date").order(SortOrder.DESC);
srb.addAggregation(aggregation);//聚合
srb.addSort(sortBuilder);//排序
/**********************主要看这里 start*********************************/
if(null != mustMap && mustMap.size() > 0){
//创建一个查询
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
//这里查询的条件用map传递
for(String key : mustMap.keySet()){
queryBuilder.must(QueryBuilders.termQuery(key,mustMap.get(key)));
}
//这里查询的条件用map传递
for(String key : shouldMap.keySet()){
queryBuilder.should(QueryBuilders.termQuery(key,mustMap.get(key)));
}
//查询
srb.setQuery(queryBuilder);
}
/**********************主要看这里 end*********************************/
//请求
SearchResponse response = srb.get();