我認爲使用Apache Solr或ElasticSearch可以獲得更多的靈活性和性能,但這是使用Aggregation Framework支持的。
使用MongoDB的主要問題是你必須查詢N次:第一次獲得匹配結果,然後每組一次;在使用全文搜索引擎的同時,您可以在一個查詢中獲得全部內容。
例
//'tags' filter simulates the search
//this query gets the products
db.products.find({tags: {$all: ["tag1", "tag2"]}})
//this query gets the size facet
db.products.aggregate(
{$match: {tags: {$all: ["tag1", "tag2"]}}},
{$group: {_id: "$size"}, count: {$sum:1}},
{$sort: {count:-1}}
)
//this query gets the color facet
db.products.aggregate(
{$match: {tags: {$all: ["tag1", "tag2"]}}},
{$group: {_id: "$color"}, count: {$sum:1}},
{$sort: {count:-1}}
)
//this query gets the brand facet
db.products.aggregate(
{$match: {tags: {$all: ["tag1", "tag2"]}}},
{$group: {_id: "$brand"}, count: {$sum:1}},
{$sort: {count:-1}}
)
一旦用戶使用方面過濾搜索,你必須添加此過濾器來查詢謂詞和匹配謂語如下。
//user clicks on "Brand 1" facet
db.products.find({tags: {$all: ["tag1", "tag2"]}, brand: "Brand 1"})
db.products.aggregate(
{$match: {tags: {$all: ["tag1", "tag2"]}}, brand: "Brand 1"},
{$group: {_id: "$size"}, count: {$sum:1}},
{$sort: {count:-1}}
)
db.products.aggregate(
{$match: {tags: {$all: ["tag1", "tag2"]}}, brand: "Brand 1"},
{$group: {_id: "$color"}, count: {$sum:1}},
{$sort: {count:-1}}
)
db.products.aggregate(
{$match: {tags: {$all: ["tag1", "tag2"]}}, brand: "Brand 1"},
{$group: {_id: "$brand"}, count: {$sum:1}},
{$sort: {count:-1}}
)
聚合框架很有前途。我沒有看到每個facet組執行額外查詢的問題。讓我創建一個POC應用程序來驗證此實現。 –
是的,它非常強大,它給了我們很多可能性。這個框架的主要問題是查詢優化。使用分片時,缺少查詢優化。我正在修補這個問題並將它在github中引入。 –