ElasticSearch 中boolQueryBuilder的使用
List<EsActivity> list(ActivityQueryRequest request);
public <T> List<T> documentList(BoolQueryBuilder boolQuery,String index,Integer pageSize,Integer pageNum, Class<T> tClass){
SearchRequest searchRequest = new SearchRequest(index);
searchRequest.types(type);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(boolQuery);
searchSourceBuilder.sort("updateTime", SortOrder.DESC);
if(pageSize!=null && pageNum!=null){
searchSourceBuilder.from((pageNum-1)*pageSize);
searchSourceBuilder.size(pageSize);
}else{
searchSourceBuilder.from(0);
searchSourceBuilder.size(10000);
}
// String[] includeFields = new String[] {"activityId", "eCommerceId","platformCode"};
// String[] excludeFields = new String[] {};
// searchSourceBuilder.fetchSource(includeFields, excludeFields);
searchRequest.source(searchSourceBuilder);
try {
//查询结果
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchResponse.getHits();
SearchHit[] searchHits = hits.getHits();
List<T> result = Arrays.stream(searchHits).map(item -> {
JSONObject jsonObject = JSONObject.parseObject(item.getSourceAsString());
jsonObject.put("documentId",item.getId());
return JSONObject.toJavaObject(jsonObject, tClass);
}).collect(Collectors.toList());
return result;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return new ArrayList<>();
}
}
-----------------------------------
@RequestMapping("/searchMatch")
public List<NBAPlayer> searchMatch(@RequestParam(value = "displayNameEn", required = false) String displayNameEn) throws IOException {
return nbaPlayerService.searchMatch("displayNameEn", displayNameEn);
}
List<NBAPlayer> searchMatch(String key, String value) throws IOException;
@Override
public List<NBAPlayer> searchMatch(String key, String value) throws IOException {
SearchRequest request = new SearchRequest(NBA_INDEX);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolQuery = new BoolQueryBuilder();
boolQuery.must(QueryBuilders.termQuery(key, value));
searchSourceBuilder.query(QueryBuilders.matchQuery(key, value));
// searchSourceBuilder.query(QueryBuilders.);
searchSourceBuilder.from(0);
searchSourceBuilder.size(1000);
request.source(searchSourceBuilder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
System.out.println(JSONObject.toJSON(response));
SearchHit[] hits = response.getHits().getHits();
List<NBAPlayer> playerList = new ArrayList<>();
for (SearchHit hit : hits) {
NBAPlayer player = JSONObject.parseObject(hit.getSourceAsString(), NBAPlayer.class);
playerList.add(player);
}
return playerList;
}
-----------------------------------------------------------------------------
1. 为什么filter会快?
看上面的流程图就能很明显的看到,filter
与query
还是有很大的区别的。
比如,query
的时候,会先比较查询条件,然后计算分值,最后返回文档结果;
而filter
则是先判断是否满足查询条件,如果不满足,会缓存查询过程(记录该文档不满足结果);满足的话,就直接缓存结果。
综上所述,filter
快在两个方面:
- 1 对结果进行缓存
- 2 避免计算分值
2. bool查询的使用
Bool
查询对应Lucene
中的BooleanQuery
,它由一个或者多个子句组成,每个子句都有特定的类型
must
返回的文档必须满足must
子句的条件,并且参与计算分值
filter
返回的文档必须满足filter
子句的条件,但是不会像must
一样,参与计算分值
should
返回的文档可能满足should
子句的条件.在一个bool
查询中,如果没有must
或者filter
,有一个或者多个should
子句,那么只要满足一个就可以返回.minimum_should_match
参数定义了至少满足几个子句.
must_not
返回的文档必须不满足定义的条件
如果一个查询既有
filter
又有should
,那么至少包含一个should
子句.
bool
查询也支持禁用协同计分选项disable_coord
.一般计算分值的因素取决于所有的查询条件.
bool
查询也是采用more_matches_is_better
的机制,因此满足must和should
子句的文档将会合并起来计算分值.
3.JavaAPI使用
1
|
|