# Atlas Search Multi-Language Data Modeling

We live in an increasingly globalized economy. By extension, users have expectations that our applications will understand the context of their culture and by extension: language.

Luckily, most search engines—including, Atlas Search—support multiple languages. This article will walk through three options of query patterns, data models, and index definitions to support your various multilingual application needs.

To illustrate the options, we will create a fictitious scenario. We manage a recipe search application that supports three cultures, and by extension, languages: English, Japanese (Kuromoji), and German. Our users are located around the globe and need to search for recipes in their native language.

## 1. Single field

We have one document for each language in the same collection, and thus each field is indexed separately as its own language. This simplifies the query patterns and UX at the expense of bloated index storage.

**Document:**

```
[
  {"name":"すし"},
  {"name":"Fish and Chips"},
  {"name":"Käsespätzle"}
]
```

**Index:**

```
{
  "name":"recipes",
  "mappings": {
    "dynamic": false,
    "fields": {
      "name": {
        "type": "string",
        "analyzer": "lucene.kuromoji"
      },
      "name": {
        "type": "string",
        "analyzer": "lucene.english"
      },
      "name": {
        "type": "string",
        "analyzer": "lucene.german"
      }
    }
  }
}
```

**Query:**

```
{
  "$search": {
    "index": "recipes",
    "text": {
      "query": "Fish and Chips",
      "path": "name"
    }
  }
}
```

**Pros:**

* One single index definition.
* Don’t need to specify index name or path based on user’s language.
* Can support multiple languages in a single query.

**Cons:**

* As more fields get added, the index definition needs to change.
* Index definition payload is potentially quite large (static field mapping per language).
* Indexing fields as irrelevant languages causing larger index size than necessary.

## 2. Multiple collections

We have one collection and index per language, which allows us to isolate the different recipe languages. This could be useful if we have more recipes in some languages than others at the expense of lots of collections and indexes.

**Documents:**

```
recipes_jp:
[{"name":"すし"}]

recipes_en:
[{"name":"Fish and Chips"}]

recipes_de:
[{"name":"Käsespätzle"}]
```

**Index:**

```
{
  "name":"recipes_jp",
  "mappings": {
    "dynamic": false,
    "fields": {
      "name": {
        "type": "string",
        "analyzer": "lucene.kuromoji"
      }
    }
  }
}

{
  "name":"recipes_en",
  "mappings": {
    "dynamic": false,
    "fields": {
      "name": {
        "type": "string",
        "analyzer": "lucene.english"
      }
    }
  }
}

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

**Query:**

```
{
  "$search": {
    "index": "recipes_jp"
    "text": {
      "query": "すし",
      "path": "name"
    }
  }
}
```

**Pros:**

* Can copy the same index definition for each collection (replacing the language).
* Isolate different language documents.

**Cons:**

* Developers have to provide the language name in the index path in advance.
* Need to potentially copy documents between collections on update.
* Each index is a change stream cursor, so could be expensive to maintain.

## 3. Multiple fields

By embedding each language in a parent field, we can co-locate the translations of each recipe in each document.

**Document:**

```
{
  "name": {
    "en":"Fish and Chips",
    "jp":"すし",
    "de":"Käsespätzle"
  }
}
```

**Index:**

```
{
  "name":"multi_language_names",
  "mappings": {
    "dynamic": false,
    "fields": {
      "name": {
        "fields": {
          "de": {
            "analyzer": "lucene.german",
            "type": "string"
          },
          "en": {
            "analyzer": "lucene.english",
            "type": "string"
          },
          "jp": {
            "analyzer": "lucene.kuromoji",
            "type": "string"
          }
        },
        "type": "document"
      }
    }
  }
}
```

**Query:**

```
{
  "$search": {
    "index": "multi_language_names"
    "text": {
      "query": "Fish and Chips",
      "path": "name.en"
    }
  }
}
```

**Pros:**

* Easier to manage documents.
* Index definition is sparse.

**Cons:**

* Index definition payload is potentially quite large (static field mapping per language).
* More complex query and UX.