我正在熟悉 Lucene 和 MongoDB Atlas 搜索,并且我对查询效率有疑问。
这些查询中的哪一个使用较少的资源?
如果对执行以下任务有更好的查询,请告诉我。
我想返回与标题值匹配的所有电影(sample_mflix)。电影必须是特定年份的(不应返回任何不属于该年份的电影),我想返回movies.awards.nominations & movies.awards.wins 的“$gte”值的电影。
第一个查询似乎更复杂(这似乎增加了资源利用率 - 查询复杂性?)。此查询也不只返回该年份的值。这让我认为 Atlas 搜索可能有更好的方法来做到这一点。
第二个查询在不同阶段使用 $search 和 $match。它有一个简单的 Lucene 搜索(可能会返回比第一个查询更多的电影?),并且匹配运算符将过滤结果。第二个查询更精确 - 从我的测试来看,它尊重年份约束。如果我应用限制阶段,这会是更好的解决方案吗?
如果这些查询是在相同的场景中执行的,哪一个会更有效,为什么(抱歉,第二个查询是为 .net 驱动程序格式化的)?
new BsonArray
{
new BsonDocument("$search",
new BsonDocument
{
{ "index", "nostoreindex" },
{ "compound",
new BsonDocument
{
{ "must",
new BsonDocument("near",
new BsonDocument
{
{ "path", "year" },
{ "origin", 2000 },
{ "pivot", 1 }
}) },
{ "must",
new BsonDocument("text",
new BsonDocument
{
{ "query", "poor" },
{ "path", "title" }
}) },
{ "should",
new BsonDocument("range",
new BsonDocument
{
{ "path", "awards.nominations" },
{ "gte", 1 }
}) },
{ "should",
new BsonDocument("range",
new BsonDocument
{
{ "path", "awards.wins" },
{ "gte", 1 }
}) }
} }
})
}
VS
var searchStage =
new BsonDocument("$search",
new BsonDocument
{
{ "index", "nostoreindex" },
{ "text",
new BsonDocument
{
{ "query", title },
{ "path", "title" }
} }
});
var matchStage = new BsonDocument("$match",
new BsonDocument("$and",
new BsonArray
{
new BsonDocument("year",
new BsonDocument("$eq", year)),
new BsonDocument("awards.nominations",
new BsonDocument("$gte", nominations)),
new BsonDocument("awards.wins",
new BsonDocument("$gte", awards))
})
);