Home > Blockchain >  Setting custom analyzer as default for index in Elasticsearch
Setting custom analyzer as default for index in Elasticsearch

Time:09-08

I have created custom analyzer for Elasticsearch and it's working as expected, however I'm not able to set it as default one for index. I'm only able to set it for each field separately, but it's important for me to have it as default, since index mapping will probably change overtime and I'd like to avoid having to remember to set analyzer for each new field.

Json contents I'm using for index creation:

{
    "settings": {
        "analysis": {
            "char_filter": {
                "my_mappings_char_filter": {
                    "type": "mapping",
                    "mappings": [
                        "- => ¡h¡",
                    ],
                }
            },
            "analyzer": {
                "test_analyzer": {
                    "tokenizer": "whitespace",
                    "char_filter": ["my_mappings_char_filter"],
                    "filter": ["lowercase"]
                },
                "default": {
                    "type": "test_analyzer"
                }
            },
        }
    },
}

Response is below (status 400)

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "Unknown analyzer type [test_analyzer] for [default]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "Unknown analyzer type [test_analyzer] for [default]"
  },
  "status": 400
}

I'm following those instructions from official elastic docs, but for some reason it throws error in my case? Or maybe it's not possible to have custom analyzer as default?

CodePudding user response:

The default analyzer setting is not a reference to an existing analyzer (except built-in ones) but a full-blown definition of its own. This will work:

{
  "settings": {
    "analysis": {
      "char_filter": {
        "my_mappings_char_filter": {
          "type": "mapping",
          "mappings": [
            "- => ¡h¡"
          ]
        }
      },
      "analyzer": {
        "default": {
          "type": "custom",
          "tokenizer": "whitespace",
          "char_filter": [
            "my_mappings_char_filter"
          ],
          "filter": [
            "lowercase"
          ]
        }
      }
    }
  }
}
  • Related