Elasticsearch Query DSL 之 Compound queries(复合查询)
SearchResponse result = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(result);
} catch (Throwable e) {
e.printStackTrace();
} finally {
EsClient.close(client);
}
}
返回的结果为:为了对比,左边的结果是 QueryBuilders.wildcardQuery(“user”, “ding*”)查询,而右边的是 constant_score (复合查询)。
{ {
"took":4, "took":2,
"timed_out":false, "timed_out":false,
"_shards":{ "_shards":{
"total":5, "total":5,
"successful":5, "successful":5,
"skipped":0, "skipped":0,
"failed":0 "failed":0
}, },
"hits":{ "hits":{
"total":1, "total":1,
"max_score":0.9808292, "max 《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 _score":1.5,
"hits":[ "hits":[
{ {
"_index":"twitter", "_index":"twitter",
"_type":"_doc", "_type":"_doc",
"_id":"12", "_id":"12",
"_score":0.9808292, "_score":1.5,
"_source":{ "_source":{
"post_date":"2009-11-18T14:12:12", "post_date":"2009-11-18T14:12:12",
"message":"test bulk", "message":"test bulk",
"user":"dingw" "user":"dingw"
} }
} }
] ]
} }
} }
[](()2、bool query
布尔查询。bool query 里能包含的主要字句类型如下:
| 字句类型 | 描述 |
| --- | --- |
| must | 该字句类型的查询语句,文档必须满足,并对评分产生影响(相关度) |
| filter | 子句(查询)必须出现在匹配的文档中。然而,与 must 不同的是,查询的分数将被忽略。过滤器子句在过滤器上下文中执行,子句被考虑用于缓存。 |
| should | 应该匹配;如果没有 must 和 filter,多个 should 只需要至少一个匹配即可,该数据可以通过参数 minimum_should_match 控制,如果包含了 must 或 filter,则 should 不参与实际过滤,但会参与评分。 |
| must_not | 查询条件取反,及匹配到的文档必须不符合 must_not 的条件。 |
[](()2.1 过滤上下文(filter context)中查询对相关性(打分)的影响
在过滤上下文环境的查询字句并不会对相关性产生影响,也就是说过滤上下文中的查询字句,返回的 score 为 0。
例如如下查询示例(使用 Java 编写):
public static void testBoolQuery_filterContext_score() {
RestHighLevelClient client = EsClient.getClient();
try {
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("twitter");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(
QueryBuilders.boolQuery()
.filter(QueryBuilders.termQuery("user", "dingw"))
);
searchRequest.source(sourceBuilder);
SearchResponse result = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(result);
} catch (Throwable e) {
e.printStackTrace();
} finally {
EsClient.close(client);
}
}
其返回的结果,其 score 都为 0,结果如下:
{
"took":4,
"timed_out":false,
"_shards":{
"total":5,
"successful":5,
"skipped":0,
"failed":0
},
"hits":{
"total":3,
"max_score":0,
"hits":[
{
"_index":"twitter",
"_type":"_doc",
"_id":"22",
"_score":0,
"_source":{
"post_date":"2018-10-31T14:12:10",
"message":"ab and hell",
"user":"dingw"
}
},
{
"_index":"twitter",
"_type":"_doc",
"_id":"12",
"_score":0,
"_source":{
"post_date":"2009-11-18T14:12:12",
"message":"test bulk",
"user":"dingw"
}
},
{
"_index":"twitter",
"_type":"_doc",
"_id":"11",
"_score":0,
"_source":{
"post_date":"2009-11-19T14:12:12",
"message":"test bulk update",
"user":"dingw"
}
}
]
}
}
[](()3、dis max query
该查询方式将所有查询字句进行联合查询(union),只需要其中一个条件匹配匹配文档,但在计算相关性时不是将所有条件的匹配度(score)相加,而是使使用评分最高的查询条件的 score,如果有指定 tie_breaker 的话,则为最大 score 加上 其他 score * tie_breaker。dis max query 是实现(match query multi fields best_fields)的核心。每个查询,可以指定其评分因子(权重、boost),dis max query 使用示例:
/**
dis max query
*/
public static void testDisMaxQuery() {
RestHighLevelClient client = EsClient.getClient();
try {
评论