Search code examples
mongodbmongodb-querystop-wordstext-search

MongoDB $text $search not found with keywords: ['by','and','me'...]


I had a question about using find to search keyword 'by', 'and'... in my collections, but it were wrong with some situation, does any know how to fix it?

This is my collection & indexes:

db.stores.insert(
   [
     { _id: 1, name: "Java Hut", description: "Coffee and cakes by Me" },
     { _id: 2, name: "Burger Buns", description: "by" },
     { _id: 3, name: "Coffee Shop", description: "Just coffee" },
     { _id: 4, name: "Clothes Clothes Clothes", description: "By Clothes" }
   ]
)

db.stores.createIndex( { name: "text", description: "text" } )

And this scripts will gonna be fine

db.stores.find({description: /by/})
db.stores.find({description: /and/})
db.stores.find( { $text: { $search: "java coffee shop" } } )
db.stores.find( { $text: { $search: "\"coffee shop\"" } } )
db.stores.find( { $text: { $search: "\"and cakes by\"" } } )
db.stores.find( { $text: { $search: "and cakes by" } } )
db.stores.find( { $text: { $search: "cakes" } } )
db.stores.find( { $text: { $search: "coffee" } } )

But something goes wrong here, I do not understanding why keyword 'by', 'and', 'me'... can not use in this query. Why it going wrong when I use it alone in text search?

db.stores.find( { $text: { $search: "by" } } )
db.stores.find( { $text: { $search: "and" } } )

If you have a doc or link about this, give me. Or if this question has existed somewhere else, give me too.

Thanks you!

Edited: Similar question have another Disable stop word filtering in a MongoDB text search


Solution

  • @D.SM's answer is perfect. In addition to that,

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

    db.quotes.createIndex(
       { name : "text" },
       { default_language: "none" } ---> Note here
    )
    

    MongoDB supports text search for various languages. text indexes drop language-specific stop words (e.g. in English, the, an, a, and, etc.) and use simple language-specific suffix stemming. For a list of the supported languages, see Text Search Languages.