在Mongo文本搜索之前缩小集合范围

时间:2015-12-28 20:27:47

标签: mongodb meteor full-text-search simple-schema

我有一个简单的集合,看起来有点像这个

VendorCategories = new SimpleSchema({
    name: {
        type: String,
        label: "Category"
    },
    slug: {
        type: String,
        label: "Slug"
    }
});

VendorsSchema = new SimpleSchema({
    vendorName: {
        type: String,
        label: "Vendor Name",
        min: 3,
    },
    vendorCategory: {
        type: VendorCategories,
    },
    vendorDescription: {
        type: String,
        label: "Vendor Description",
        min: 45,
    },
});

我有这样的集合的mongo索引设置。

Vendors._ensureIndex({
  'vendorName': 'text',
  'vendorDescription': 'text',
  'VendorCategories.name': 'text',
});

目前,这是我使用Mongo Text Search https://docs.mongodb.org/v3.0/reference/operator/query/text/通过此索引搜索的出版物

Meteor.publish('searchVendors', function(query) {
    if (query) {
        return Vendors.find({
            $text: {
                $search: query
            }
        }, {
            fields: {
                score: {
                    $meta: 'textScore'
                }
            },
            sort: {
                score: {
                    $meta: 'textScore'
                }
            }
        });
    } else {
        return Vendors.find();
    }
});

我的助手方法看起来像这样。

vendorSearchResults: function() {
    var category = FlowRouter.getQueryParam("category");//narrow by this first
    var terms = FlowRouter.getQueryParam("terms");

    Meteor.subscribe("searchVendors", terms);
    if (terms) {
        return Vendors.find({}, { sort: [["score", "desc"]] });
    } else {
        return Vendors.find({});
    }
}

目前,此操作会返回与供应商名称和说明匹配的所有 var term 的搜索。但我想要实现的是首先将收集结果缩小到 vendorCategories.name ,然后再搜索 vendorName vendorDescription

对此的任何帮助都非常感谢,谢谢您的阅读。

1 个答案:

答案 0 :(得分:1)

$text运算符可以与其他匹配条件结合使用:

    return Vendors.find({
        "VendorCategories.name": "Acme Corporation",
        $text: {
            $search: query
        }
    },