【ElasticSearch(八)进阶】filter过滤
-
布尔查询中的每个must、should和must not元素都称为查询子句。
文档满足 must 或 should 子句中的标准的程度有助于文档的相关性得分。分数越高,文档就越符合我们的搜索条件。默认情况下,ElasticSearch返回会按照相关性得分对文档排序。
-
must_not 子句中的条件,影响文档是否包含在结果中。
filter、must_not 都不影响文档的得分。
-
还可以显式指定任意过滤器filter,以包含或排除基于结构化数据的文档。
【例子1】
查找年龄范围在 18 - 30 ,并且地址中包含 mill 的数据
range
:限制条件的范围
filter
:过滤掉不符合条件的数据,且filter不会影响相关性得分。
GET /bank/_search
{
"query":{
"bool":{
"filter":{
"range":{
"age":{
"gte":18,
"lte": 30
}
}
},
"must":{
"match":{
"address": "mill"
}}
}
}
}
返回结果:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 5.4032025,
"hits" : [
{
"_index" : "bank",
"_type" : "account",
"_id" : "970",
"_score" : 5.4032025,
"_source" : {
"account_number" : 970,
"balance" : 19648,
"firstname" : "Forbes",
"lastname" : "Wallace",
"age" : 28,
"gender" : "M",
"address" : "990 Mill Road",
"employer" : "Pheast",
"email" : "forbeswallace@pheast.com",
"city" : "Lopezo",
"state" : "AK"
}
}
]
}
}
【例子2】
我们可以看到想关心得分是0,filter
并没有影响得分,只是筛选出了数据。
GET /bank/_search
{
"query":{
"bool":{
"filter":{
"range":{
"age":{
"gte":18,
"lte": 30
}
}
}
}
}
}
返回的结果是:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 498,
"relation" : "eq"
},
"max_score" : 0.0,
"hits" : [
{
"_index" : "bank",
"_type" : "account",
"_id" : "13",
"_score" : 0.0,
"_source" : {
"account_number" : 13,
"balance" : 32838,
"firstname" : "Nanette",
"lastname" : "Bates",
"age" : 28,
"gender" : "F",
"address" : "789 Madison Street",
"employer" : "Quility",
"email" : "nanettebates@quility.com",
"city" : "Nogal",
"state" : "VA"
}
},
{
"_index" : "bank",
"_type" : "account",
"_id" : "49",
"_score" : 0.0,
"_source" : {
"account_number" : 49,
"balance" : 29104,
"firstname" : "Fulton",
"lastname" : "Holt",
"age" : 23,
"gender" : "F",
"address" : "451 Humboldt Street",
"employer" : "Anocha",
"email" : "fultonholt@anocha.com",
"city" : "Sunriver",
"state" : "RI"
}
},
。。。
]
}
}