Cannot search for letter "a" with text indexed search

db.t.insertMany([
{ “_id” : 1, “t” : “a” },
{ “_id” : 2, “t” : “b” }
])
db.t.createIndex({t: “text”})
db.t.find({$text:{$search: “a”}}) => no match
db.t.find({$text:{$search: “b”}}) => match
why ??

Hello @Heliang_Peng, welcome to the MongoDB Community forum!

why ??

This is the reason: Text Indexes - Stop Words, and it says -

text indexes drop language-specific stop words (e.g. in English, the , an , a , and , etc.) and use simple language-specific suffix stemming.

So, if you try to search for a, i, or, etc., the text search will fail. I think the text index is not used for searching stop words.

thanks for your reply
i get it

Hi @Heliang_Peng,

You can index stop words if you specify a language of none when creating the index:

 db.collection.createIndex(
   { t : "text" },
   { default_language: "none" }
)

Thanks
Pavel

1 Like

thanks @Pavel_Duchovny
it works with {default_language: “none”}

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.