Elasticsearch: how to add language analyser on a field?

Viewed 907

I have an index with french words. I want to apply an analyser on index properties. Let's say I have a title property and I want to consider it as a "french property". I tried this (in kibana):

PUT thing/_mappings/thing
{
  "properties": {
    "title": {
      "type": "text",
      "analyzer": "french",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    }
  }
}

But it results in:

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "Mapper for [title] conflicts with existing mapping in other types:\n[mapper [title] has different [analyzer]]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "Mapper for [title] conflicts with existing mapping in other types:\n[mapper [title] has different [analyzer]]"
  },
  "status": 400
}

I don't understand why I have this error. If I display mapping (GET thing/_mappings), it contains no existing analyzer (unless I misunderstood something):

 // ...
    "title": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    }
  }

How can I consider my title property as a french property? (source: https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-lang-analyzer.html)

1 Answers

You're not allowed to change the analyzer of the title field, which is standard by default if not specified when creating the field.

You need to delete your index, change your mapping to fit your needs and then reindex your data.

Another solution would be to add another sub-field to the title field with the proper analyzer:

PUT thing/_mappings/thing
{
  "properties": {
    "title": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        },
        "french": {                 <--- add this
          "type": "text",
          "analyzer": "french"
        }
      }
    }
  }
}

After running this, you don't need to reupload all your 1GB data, but simply call

POST thing/_update_by_query

in order to pick up the new sub-field.

The only drawback of this second approach is that you end up with more analyzed data than needed if you don't need the title field with the standard analyzer. Up to you.

Related