Search code examples
mongodbmongodb-atlasmongodb-atlas-search

Mongodb atlas search: apply multiple language analyzers


There is a collection namely - categories, which has following schema

{
    name: String,
    language: { $type: String, default: "de"}
    translation:[
      {
          language: { $type: String, enum: ["en","fr"]}, 
          name:String
      }
    ]
}

It has language specific data, other than name too like - description and more. I want to create atlas search index on name field for all three languages. I have tried creating atlas search index with 'name' and 'translation.name', but it did not work for translation.name. Here is the atlas search index:

{
  "mappings": {
    "dynamic": false,
    "fields": {
      "name": {
        "analyzer": "lucene.german",
        "type": "string"
      },
      "translation.name": {
        "analyzer": "lucene.french",
        "type": "string"
      }
    }
}

Problem here is, if I specify language analyzer for translation.name as german, I can not apply the same for english. How multiple language analyzers can be used for a single field?


Solution

  • In the index definition documentation for atlas search, I found the answer to my query i.e. applying multiple language anlayzers for a single field. Here is the link to the documentation -

    https://docs.atlas.mongodb.com/reference/atlas-search/index-definitions/

    And this is what I have modified my mappings to:

    {
      "mappings": {
        "dynamic": false,
        "fields": {
          "name": {
            "analyzer": "lucene.german",
            "type": "string"
          },
          "translation": {
            "type": "document",
            "fields": {
              "name": {
                "multi": {
                  "english": {                //english is the name that I have given to this analyzer
                    "analyzer": "lucene.english",
                    "type": "string"
                  },
                  "french": {                //french is the name that I have given to this analyzer
                    "analyzer": "lucene.french",
                    "type": "string"
                  }
                }
              }
            }
          }
        }
      }
    }