I have a collection that is a simpleschema that looks somewhat like this
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,
},
});
I have a mongo index setup for this collection like so.
Vendors._ensureIndex({
'vendorName': 'text',
'vendorDescription': 'text',
'VendorCategories.name': 'text',
});
Currently this is what my publication to search thru this index using Mongo Text Search https://docs.mongodb.org/v3.0/reference/operator/query/text/ looks like
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();
}
});
And my helper method looks something like this.
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({});
}
}
Currently this returns the search for all var terms that match the vendor name and description. But what I want to achieve is first have the collection results be narrowed down to the vendorCategories.name before the vendorName and vendorDescription gets searched.
Any help with this would much appreciated, Thank you for reading.
The $text
operator can be combined with other match criteria:
return Vendors.find({
"VendorCategories.name": "Acme Corporation",
$text: {
$search: query
}
},