如何使用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