我用这两个查询进行测试。
必须查询
{
"size": 200,
"from": 0,
"query": {
"bool": {
"must": [ {
"match": {
"_all": "science"
}
},
{
"match": {
"category": "fiction"
}
},
{
"match": {
"country": "us"
}
}
]
}
}
}
使用W应当+ minimum_should_match查询
{
"size": 200,
"from": 0,
"query": {
"bool": {
"should": [ {
"match": {
"_all": "science"
}
},
{
"match": {
"category": "fiction"
}
},
{
"match": {
"country": "us"
}
}
],
minimum_should_match: 3
}
}
}
这两个查询都给出了相同的结果,我不知道这2之间的区别,什么时候应该使用minimum_should_match?
发布于 2017-03-16 10:21:49
我想你是说minimum_number_should_match
吧?
在这两种情况下,都是一样的,因为在should
中有相同数量的子句。minimum_number_should_match
通常用于当您指定的子句比指定的数字更多时使用。
例如,如果您有5个but子句,但出于某种原因,您只需要满足其中的三个,您就可以这样做:
{
"query": {
"bool": {
"should": [
{
"term": {
"tag": "wow"
}
},
{
"term": {
"tag": "elasticsearch"
}
},
{
"term": {
"tag": "tech"
}
},
{
"term": {
"user": "plchia"
}
},
{
"range": {
"age": {
"gte": 10,
"lte": 20
}
}
}
],
"minimum_should_match": 3
}
}
}
发布于 2017-03-16 10:14:48
这是正确的,也是理想的行为。让我们破译一下:
must
子句的布尔查询意味着must
节下的所有子句都必须匹配。就像在英语里一样--它意味着强烈的义务。should
子句的布尔查询意味着需要匹配一些子句,而其他子句则不匹配(即软义务)。这里必须匹配的默认子句数仅为1。要覆盖此行为,minimum_should_match
参数将起作用。如果指定minimum_should_match=3
,则意味着should
下的3个子句必须匹配。从实际角度来看,这与用must
指定这些子句完全一样。希望它能详细解释。
https://stackoverflow.com/questions/42830882
复制相似问题