Search for documents in the collection containing multiple languages

Hi everyone!

I’m trying to reproduce the example from the documentation.

But, if I search for “réalité”, I can’t find anything.

default_language = english

To work correctly I need to explicitly pass the language parameter to the search query – { “$language”: “fr” }

But the documentation doesn’t say anything about it. I was under the impression that explicit language transfer is optional.

Tell me, please, is there there error in the documentation? Are there ways to search for documents without explicitly passing the language parameter?

Thank you for your attention,
Best regards!

Hi @cosmonaut13 and welcome to MongoDB community forums!!

Thank you for reporting the issue and the concern has been reported internally.

By default, which creating the text index, the default_language is set is nothing is specified while creating the index.
If you wish to use the same query in the example attached in the documentation, you can create index with the default_lauguage specified.
You can refer to the example below:

Atlas atlas-xp4gev-shard-0 [primary] test> db.quotes.createIndex( { original: "text", "translation.quote": "text"} ,{ "default_language": "fr"} )
original_text_translation.quote_text
Atlas atlas-xp4gev-shard-0 [primary] test> db.quotes.find( { $text: { $search: "réalité" } } )
[
  {
    _id: 2,
    language: 'spanish',
    original: 'Nada hay más surrealista que la realidad.',
    translation: [
      {
        language: 'english',
        quote: 'There is nothing more surreal than reality.'
      },
      {
        language: 'french',
        quote: "Il n'y a rien de plus surréaliste que la réalité."
      }
    ]
  }
]

However, if you do not wish to create index with language specified, you would have to use the search query as

Atlas atlas-xp4gev-shard-0 [primary] test> db.quotes.find( { $text: { $search: "réalité" , $language: "fr" }} )
[
  {
    _id: 2,
    language: 'spanish',
    original: 'Nada hay más surrealista que la realidad.',
    translation: [
      {
        language: 'english',
        quote: 'There is nothing more surreal than reality.'
      },
      {
        language: 'french',
        quote: "Il n'y a rien de plus surréaliste que la réalité."
      }
    ]
  }
]

to get the expected response.

Please reach out in case of further queries.

Regards
Aasawari

1 Like

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