Text search and the score

Hello all,

I want to search documents by the text, and I may search with one or more words. For example, I may search by “coffee”, or I may search by “coffee tea”. In my database some documents contains “coffee” only, some documents contains “tea” only, and there are some documents contains both “coffee” and “tea”. I want to make sure for the documents have both “coffee” and “tea”, the text score is higher than the documents have “coffee” only or have “tea” only. I read the guideline at https://docs.mongodb.com/manual/reference/operator/query/text/#search-for-a-phrase, but here I don’t want to search the “coffee tea” as a whole phrase (in my document the coffee and tea may not connected in sequence, i.e. it may be “coffee and tea”). How can I implement this scenario by the text search statement?

Thanks a lot for the help,

James

Hi @Zhihong_GUO,

You should consider using Atlas search which has all those built in operator and compound expressions.

However, for regular search with $text if you fo not specify a phrase the query auto presume an OR between words and thus scores it.

So just searching “tea coffee” should work.

Thanks
Pavel

Hello Pavel,

Very clear. Thanks for the information.

Regards,

James

1 Like