Reputation: 709
I have pipeline:
[ { '$match': { '$text': [Object] } },
{ '$addFields': { sortFieldTextRelevance: [Object] } },
{ '$match': { '$and': [Array] } } ]
And I see that indexes for second match are not being used, but if I remove
{ $match: { $text: { $search: search } }
everything is fine. What may be wrong?
Upvotes: 0
Views: 82
Reputation: 37048
The $match and $sort pipeline operators can take advantage of an index when they occur at the beginning of the pipeline.
So indexes are intended to work only on the first stage match.
If you can create a compound text index you can use it by combining both matches into first stage:
[ { '$match': { '$and': [...Array, { '$text': [Object] }] } },
{ '$addFields': { sortFieldTextRelevance: [Object] } }]
Upvotes: 1