elasticsearch怎么使用?
用2個Map作為傳參,一個是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();
//更多看這里:http://www.sojson.com/tag_elasticsearch.html