Search code examples
elasticsearchmultilingualfull-text-indexing

In Elasticsearch, can I set language-specific multi-fields?


Is it possible to use multi-fields to set and query multilingual fields?

Consider this mapping:

PUT multi_test
{
  "mappings": {
    "data": {
      "_field_names": {
        "enabled": false
      },
      "properties": {
        "book_title": {
          "type": "text",
          "fields": {
            "english": {
              "type": "text",
              "analyzer": "english"
            },
            "german": {
              "type": "text",
              "analyzer": "german"
            },
            "italian": {
              "type": "text",
              "analyzer": "italian"
            }
          }
        }
      }
    }
  }
}

I tried the following, but it doesn't work:

PUT multi_test/data/1
{
  "book_title.english": "It's good",
  "book_title.german": "Das gut"
}

The error seems to indicate I'm trying to add new fields:

{ "error": { "root_cause": [ { "type": "mapper_parsing_exception", "reason": "Could not dynamically add mapping for field [book_title.english]. Existing mapping for [book_title] must be of type object but found [text]." } ], "type": "mapper_parsing_exception", "reason": "Could not dynamically add mapping for field [book_title.english]. Existing mapping for [book_title] must be of type object but found [text]." }, "status": 400 }

What am I doing wrong here?

If my approach is unworkable, what is a better way to do this?


Solution

  • The problem is that you are using using fields for the field book_title.

    In you use case the mapping should be like below

    PUT multi_test

    {
      "mappings": {
        "data": {
          "_field_names": {
            "enabled": false
          },
          "properties": {
            "book_title": {
              "properties": {
                "english": {
                  "type": "text",
                  "analyzer": "english"
                },
                "german": {
                  "type": "text",
                  "analyzer": "german"
                },
                "italian": {
                  "type": "text",
                  "analyzer": "italian"
                }
              }
            }
          }
        }
      }
    }
    

    This will define book_title as object type and you can add multiple fields with different data under book_title