顿搜
飞过闲红千叶,夕岸在哪
类目归类
使用match_all查询文档时,ES不对文档进行打分计算,默认情况下给每个文档赋予1.0的得分。
用户可以通过boost参数设定该分值。以下示例使用match_all查询所有文档,并设定所有文档的分值为2.0
GET /hotel/_search
{
"_source": [ //只返回title和city字段
"title",
"city"
],
"query": {
"match_all": { //查询所有文档
"boost": 2 //设定所有文档的分值为2.0
}
}
}在Java客户端中进行查询时,可以调用QueryBuilders.matchAllQuery()方法新建一个match_all查询,
并且通过boost()方法设置boost值。
构建完term查询后,调用searchSourceBuilder.query()方法设置查询条件。
public void matchAllSearch() {
//新建搜索请求
SearchRequest searchRequest = new SearchRequest("hotel");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery()
.boost(2.0f); //新建match_all查询,并设置boost值为2.0
searchSourceBuilder.query(matchAllQueryBuilder);
searchRequest.source(searchSourceBuilder); //设置查询
printResult(searchRequest); //打印结果
}结果解析
public void printResult(SearchRequest searchRequest) {
try {
SearchResponse searchResponse = client.search(searchRequest,
RequestOptions.DEFAULT); //执行搜索
SearchHits searchHits = searchResponse.getHits(); //获取搜索结果集
for (SearchHit searchHit : searchHits) { //遍历搜索结果集
String index = searchHit.getIndex(); //获取索引名称
String id = searchHit.getId(); //获取文档_id
Float score = searchHit.getScore(); //获取得分
String source = searchHit.getSourceAsString(); //获取文档内容
System.out.println("index=" + index + ",id=" + id + ",score="
+ score + ",source=" + source); //打印数据
}
} catch (Exception e) {
e.printStackTrace();
}
}