full-text search on inherited schemas
See original GitHub issueHi, I would bring the attention on this stackoverflow question: http://stackoverflow.com/questions/36056594/mongodb-error-failed-to-use-text-index-to-satisfy-text-query
I know it’s a huge post, basically I’m trying to make a full-text search on 4 subschemas together.
The compound text index is defined myRootSchema.index({ "_type": 1, "$**": "text" })
_type is the discriminatorKey.
This query should work since _type is indeed a discriminatorkey, so mongoose should ‘autofill’ that, but it doesn’t.
Model
.find(
{ $text : { $search :req.query.q } }
)
.exec(function(err, data) {
if(err)res.json(err)
res.json(data)
});
trying this query I get the error failed to use text index to satisfy $text query
.
A temporary solution could be explicitly fill _type
and use parallel from async module and query each subschema like this
async.parallel([
function(callback){
caseNote
.find({_type:'FIRST_TYPE',$text : { $search :req.query.q } })
.exec(function(err, data) {
if(err) {
callback(err);
}
else {
callback(null, data);
}
});
},
.....#query on each other type..#
]);
but I don’t think there is a proper way to paginate those results…so I’m stuck here.
Issue Analytics
- State:
- Created 7 years ago
- Comments:16
In your example, is
Model
the base model? If so, the discriminator won’t get ‘autofilled’ like you said, because when you query by the base model the_type
field won’t get set.Try doing:
It’ll definitely index all the inherited fields,
$**
has nothing to do with mongoose, it’s all at the mongodb level, so it doesn’t even know about discriminators.Yeah, building a compound index and querying by the
_type
field would be all about performance - if you don’t need that extra performance, I wouldn’t recommend bothering to figure it out.