Multilingual search and retrieval of a translated field

Hello,

I have the following mapping:

{
  "settings": {
    "analysis": {
      "analyzer": {
        "en_stopwords": {
          "type": "standard",
          "stopwords": "_english_"
        },
        "de_stopwords": {
          "type": "standard",
          "stopwords": "_german_"
        },
        "fr_stopwords": {
          "type": "standard",
          "stopwords": "_french_"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "title": {
        "type": "text"
      },
      "_translations": {
        "properties": {
          "de": {
            "properties": {
              "locale": {
                "type": "text",
                "index": false
              },
              "title": {
                "type": "text"
              }
            }
          },
          "fr": {
            "properties": {
              "locale": {
                "type": "text",
                "index": false
              },
              "title": {
                "type": "text"
              }
            }
          }
        }
      }
    }
  }
}

I can't do a global search for titles in the _translations property like:

GET _search
{
  "query": {
    "multi_match": {
      "query": "My Search",
      "fields": ["title", "_translations.*.title"]
    }
  }
}

We have a dozen translations, do we have to list each field one by one like this:

GET _search
{
  "query": {
    "multi_match": {
      "query": "My Search",
      "fields": [
        "title",
        "_translations.fr.title",
        "_translations.de.title"
      ]
    }
  }
}

We also have another problem, when the language is English, we display the title directly but when it is for example French, is it possible to replace the value of the title field by that of _translations.fr.title at the time of data retrieval.

Thanks for your help.

Hi.

Maybe Query string helps you:

{
  "query": {
    "query_string": {
      "fields": ["title", "_translations.*.title"],
      "query": "aaa"
    }
  }
}
1 Like

Hi @RabBit_BR,

Thank you for your answer.

Finally for the search, the initial code worked, I had a problem with the registered mapping.

GET _search
{
  "query": {
    "multi_match": {
      "query": "My Search",
      "fields": ["title", "_translations.*.title"]
    }
  }
}

I also found the solution to update the title field with the translated version.
I pass the locale of the translated field that I want to retrieve.

GET videos/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "My Search",
            "fields": ["title"]
          }
        }
      ]
    }
  },
  "script_fields": {
    "title": {
      "script": {
        "source": "field('_translations.'+ params.get('locale') +'.title').get('')", 
        "params": {
          "locale": "de"
        }
      }
    }
  }
}
1 Like