简体   繁体   中英

Use custom normalizer as default for all text/keywords fields in the elasticsearch

I want to use case insensitive sorting in the elasticsearch. But ES uses ASCII sorting default. So to make case-insensitive searching. I have to use normalizer.

{
  "analysis": {
    "normalizer": {
      "custom_sort_normalizer": {
        "type": "custom",
        "char_filter": [],
        "filter": [
          "lowercase",
          "asciifolding"
        ]
      }
    }
  }
}

I want to apply this custom_sort_normalizer to all text or keyword based fields. As I am using AWS elasticsearch, I can not simply close the index and apply this normalizer. Either I have to reindex and create index from start, I want to avoid both this option for future.

Right now, I am adding manually in the mapping to tell elasticsearch use the normalizer. Is there anyway I can make it as default?

Update: I am maintaining mapping using yaml file. Below is a sample mapping.

properties:
  studetName:
    type: text
    fields:
      keyword:
        type: keyword
        ignore_above: 256
  studentGender:
    type: text
    fields:
      keyword:
        type: keyword
        ignore_above: 256
  studentGroup:
    type: text
    fields:
      keyword:
        type: keyword
        normalizer: custom_sort_normalizer
        ignore_above: 256

My main objective is that every new text fields we add in this mapping.yaml (every new text/keyword file in that index) should by default use normalizer.

If I understand correctly, you want to have your custom custom_sort_normalizer applied to all the fields in your future elasticsearch indices. if yes, then you can simply use the index template , which will allow you to define this setting in the template, and later on this template, you can apply to your future indices.

From the docs

Index templates allow you to define templates that will automatically be applied when new indices are created. The templates include both settings and mappings and a simple pattern template that controls whether the template should be applied to the new index.

you can utilize dynamic templates to achieve what you want. create your index template with mapping as follows:

 PUT my-index
{
  "settings": {
    "index": {
      "analysis": {
        "normalizer": {
          "my_normalizer": {
            "type": "custom",
            "char_filter": [],
            "filter": [
              "lowercase"
            ]
          }
        }
      }
    }
  },
  "mappings": {
    "dynamic_templates": [
      {
        "strings": {
          "match_mapping_type": "string",
          "mapping": {
            "type": "keyword",
            "ignore_above": 256,
            "normalizer": "my_normalizer"
          }
        }
      }
    ]
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM