I have fallen in a case where I need to do a $text $search in MongoDB by matching exact tokens within a string. I thought I can resolve this by creating a text index without a default language and perform the query by wrapping each token with \"token\"
, as written in the documentation. So I created my index in this way:
db.collection.createIndex({"denom": "text"}, {"default_language": "none"})
And the query I have to perform is
db.collection.find( {"$text": {"$search": "\"consorzio\" \"la\""}}, {"denom": 1} )
The result I was expecting are all documents that contains exactly the tokens "consorzio"
and "la"
, but instead this query match documents whose tokens contain the string "la" and "consorzio" inside each token
For example, the query above returns the following denom's values in which I expect:
Can someone address me in this problem please? I hope the problem is clear.
Thank you very much in advance.
Mongodb has a reported bug for this issue. Exact maching is not working.
You can take a look at maching score:
db.docs.find({$text: {$search: "\"consorzio\" \"la\""}},
{score: { $meta: "textScore" }, "_id": 0})
{ "t" : "CONSORZIO LA* CASCINA OK", "score" : 1.25 }
{ "t" : "LA RADA CONSORZIO OK", "score" : 1.25 }
{ "t" : "GESCO CONSORZIO AGRICOLA WRONG", "score" : 0.625 }
A solution should be to take into consideration the highest scores ...