Search code examples
elasticsearchelasticsearch-7

How to create an index in Elasticsearch with an analyzer and shard defined?


I am trying to create an index with the mapping of text and keyword with the analyzer defined, here what i have tried till now:


{
    "settings" : {
        "number_of_shards" : 2,
        "number_of_replicas" : 1
    },

    "analysis": {
      "normalizer": {
        "my_normalizer": {
          "type": "custom",
          "char_filter": [],
          "filter": ["lowercase", "asciifolding"]
        }
      }
    }
  ,
    "mappings": {
    "properties": {
  "question": {
    "type":"text",
    "fields": {
      "keyword": {
        "type": "keyword"
      },
     "normalize": {
      "type": "keyword",
      "normalizer": "my_normalizer"
    }

}
}
}
}
}

I have tried this but getting error :

"error": {
    "root_cause": [
        {
            "type": "parse_exception",
            "reason": "unknown key [analysis] for create index"
        }
    ],
    "type": "parse_exception",
    "reason": "unknown key [analysis] for create index"
},
"status": 400

}

Question is the field where I need to add this mapping. I am trying this in AWS ES service.


Solution

  • Great start, you're almost there!

    The analysis section needs to be located inside the top-level settings section, like this:

    {
      "settings": {
        "index": {
          "number_of_shards": 2,
          "number_of_replicas": 1
        },
        "analysis": {
          "normalizer": {
            "my_normalizer": {
              "type": "custom",
              "char_filter": [],
              "filter": [
                "lowercase",
                "asciifolding"
              ]
            }
          }
        }
      },
      "mappings": {
        "properties": {
          "question": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword"
              },
              "normalize": {
                "type": "keyword",
                "normalizer": "my_normalizer"
              }
            }
          },
          "answer": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword"
              },
              "normalize": {
                "type": "keyword",
                "normalizer": "my_normalizer"
              }
            }
          }
        }
      }
    }