Text Search query error

Hey guys, I am currently blocked in a weird situation, I am using full text search for a backend module I am currently building where I faced this issue. Whenever I am trying to query via country naming code like US for United States, IN for India, etc. The search query for {$text: {$search: 'IN'}} was not returning any data but there was data stored inside the database, even if I change the input of the query to US from IN {$text: {$search: 'US'}} the query works. And the problem is whenever I am trying to query 'country':'IN', I am receiving output. These are the 3 types of output for anyone who might be interested in helping me out here.

Hi @Arnab_Ray - Welcome to the community :wave:

I would just like to clarify the issue you’re experiencing - My interpretation is that when you query for {$text: {$search: 'IN'}}, no documents are returned. However, when the query is {$text: {$search: 'US'}}, the correct documents are returned? If so, this is possibly due to "IN" being a stop word and perhaps the below default_language specification on the text index may help your use case.

Please also note that as per the Text Search Languages documentation:

If you specify a language value of “none”, then the text search uses simple tokenization with no list of stop words and no stemming.

Test documents, text index and initial $text query yielding no results:

myFirstDatabase> db.text.find()
[
  { _id: ObjectId("638d229c86e26bbc448fd096"), country: 'IN' },
  { _id: ObjectId("638d22a286e26bbc448fd097"), country: 'US' },
  { _id: ObjectId("638d245886e26bbc448fd099"), country: 'india' },
  { _id: ObjectId("638d245c86e26bbc448fd09a"), country: 'and' },
  { _id: ObjectId("638d249686e26bbc448fd09b"), country: 'the' }
]

myFirstDatabase> db.text.createIndex({country:"text"})
country_text

myFirstDatabase> db.text.find({$text:{$search:"IN"}})
/// No documents returned

Dropping all indexes on the test collection, re-creating the "text" index with {default_language: "none"} and performing the same $text query above:

myFirstDatabase> db.text.dropIndexes()
{
  nIndexesWas: 2,
  msg: 'non-_id indexes dropped for collection',
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1670194676, i: 1 }),
    signature: {
      hash: Binary(Buffer.from("eae9fced3efdda5d4c1a91b9fb13bd39c49ffd4f", "hex"), 0),
      keyId: Long("7115111615044780034")
    }
  },
  operationTime: Timestamp({ t: 1670194676, i: 1 })
}

myFirstDatabase> db.text.createIndex({country:"text"},{default_language:"none"})
country_text

myFirstDatabase> db.text.find({$text:{$search:"IN"}})
[ { _id: ObjectId("638d229c86e26bbc448fd096"), country: 'IN' } ]

Hope this helps.

On a side note, if you’re running on Atlas then perhaps it may be worth taking a look into Atlas Search.

Regards,
Jason