I’m not entirely sure the error indicates that the tags field needs to be included in the text
index (unless you are advising that it just needs to be indexed which I believe is the case). However, see my testing below.
I reproduced the error below by creating a text index only on the 2 fields and then trying to perform the $or
operation:
DB> db.collection.createIndex({title:"text",description:"text"})
title_text_description_text
DB> db.collection.find({$text:{$search:'hurricanes'}})
[
{
title: 'Sandy knocked them down. Nothing will make them leave.',
description: 'The risk of hurricanes hitting New York and southern New England is definitely going up.',
tags: [ 'hurricanes', 'hurricane sandy', 'New York', 'New England' ]
}
]
/// Performing a query with `$or` now with a text search:
DB> db.collection.find({$or:[{$text:{$search:'hurricanes'}},{tags:{$in:['hurricanes']}}]})
Uncaught:
MongoServerError: error processing query: ns=textdb.collectionTree: $or
tags $eq "hurricanes"
TEXT : query=hurricanes, language=english, caseSensitive=0, diacriticSensitive=0, tag=NULL
After creating an index on tags
and executing the same $or
query above:
DB> db.collection.createIndex({tags:1})
tags_1
DB> db.collection.find({$or:[{$text:{$search:'hurricanes'}},{tags:{$in:['hurricanes']}}]})
[
{
title: 'Sandy knocked them down. Nothing will make them leave.',
description: 'The risk of hurricanes hitting New York and southern New England is definitely going up.',
tags: [ 'hurricanes', 'hurricane sandy', 'New York', 'New England' ]
}
]
As you have advised before, you could create the text index so that it also includes the tags
field (I added the array element "Dragon"
only to the tags
field for demonstration):
/// Note : I dropped all the previous indexes mentioned above
DB> db.collection.createIndex({title:"text",description:"text",tags:"text"})
title_text_description_text_tags_text
db.collection.find({$text:{$search:'Dragon'}})
[
{
title: 'Sandy knocked them down. Nothing will make them leave.',
description: 'The risk of hurricanes hitting New York and southern New England is definitely going up.',
tags: [
'hurricanes',
'hurricane sandy',
'New York',
'New England',
'Dragon'
]
}
]
Thanks for the clarification Jet 