自解:过滤不影响评分(指的是使用滤波器的匹配过程不会改变评分),评分占用大量的cpu资源,应当尽量使用filter过滤结果,在剩下的文档中进行评分,可以缩小需要评分的规模,所有应当尽可能使用过滤器。 不使用滤波器的查询:
curl -XGET 'localhost:9200/library/book/_search?pretty' -d '{
"query" : {
"term" : {
"available" : true
}
}
}'
使用滤波器的查询:
curl -XGET 'localhost:9200/library/book/_search?pretty' -d '{
"query" : {
"bool" : {
"must" : {
"match_all" : { }
},
"filter" : {
"term" : {
"available" : true
}
}
}
}
}'
不使用滤波器查询的返回结果:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : { "total" : 2,
"max_score" : 1.0,
"hits" : [ {
"_index" : "library",
"_type" : "book",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"title" : "Crime and Punishment",
"otitle" : "Преступлéние и наказáние",
"author" : "Fyodor Dostoevsky",
"year" : 1886,
"characters" : [ "Raskolnikov", "Sofia Semyonovna Marmeladova" ],
"tags" : [ ],
"copies" : 0,
"available" : true
}
}, {
"_index" : "library",
"_type" : "book",
"_id" : "1",
"_score" : 0.30685282,
"_source" : {
"title" : "All Quiet on the Western Front",
"otitle" : "Im Westen nichts Neues",
"author" : "Erich Maria Remarque",
"year" : 1929,
"characters" : [ "Paul Bäumer", "Albert Kropp", "Haie Westhus",
"Fredrich Müller", "Stanislaus Katczinsky", "Tjaden" ],
"tags" : [ "novel" ],
"copies" : 1,
"available" : true,
"section" : 3
}
} ]
}
}
使用滤波器查询的结果:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [ {
"_index" : "library",
"_type" : "book",
"_id" : "4",
"_score" : 1.0, "_source" : {
"title" : "Crime and Punishment",
"otitle" : "Преступлéние и наказáние",
"author" : "Fyodor Dostoevsky",
"year" : 1886,
"characters" : [ "Raskolnikov", "Sofia Semyonovna Marmeladova" ],
"tags" : [ ],
"copies" : 0,
"available" : true
}
}, {
"_index" : "library",
"_type" : "book",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"title" : "All Quiet on the Western Front",
"otitle" : "Im Westen nichts Neues",
"author" : "Erich Maria Remarque",
"year" : 1929,
"characters" : [ "Paul Bäumer", "Albert Kropp", "Haie Westhus",
"Fredrich Müller", "Stanislaus Katczinsky", "Tjaden" ],
"tags" : [ "novel" ],
"copies" : 1,
"available" : true,
"section" : 3
}
} ]
}
}
两个结果不同的是第一个查询返回的两个文档的评分是1.0和0.30685282,文档评分受到查询影响。 使用滤波器的查询结果分数都是1.0,不受查询影响。滤波器不影响评分。