Search code examples
elasticsearchterraformchalice

Can you set a field to not_analyzed in an auto created index in Elasticsearch?


As part of our AWS infrastructure, I am using an Elasticsearch (7.4) index. We use Terraform to create the domain in AWS Elasticsearch but we don't create the index explicitly. Instead when the first document is posted, the index is auto-created. This worked well, but now I have been given the requirement to have a non analyzed field (user id).

Is there a way to make a field not_analyzed when putting the first document?

If there is not, what are my options to set the field to not_analyzed? Should I do some sort of init/bootstrapping? Maybe there is a way to do it from Terraform. The application is build using Chalice and runs in Lambda. Not sure how to do initialization in Lambda in that case. Ideally I would fire this call a single time:

PUT /my_index
{
    "mappings" : {
          "properties" : {
              "user_id" : {
                  "type" : "string",
                  "index" : "not_analyzed" 
              }
          }
    }

}

When restarting the application, this call would be send again but I guess it's immutable (PUT).


Solution

  • This might be an overkill but I would consider using index template feature

    This may look like

    PUT _index_template/template_1
    {
      "index_patterns": [
        "my_template*"
      ],
      "template": {
        "mappings": {
          "properties": {
            "user_id" : {
              "type" : "keyword"
            }
          }
        }
      },
      "priority": 1
    }
    

    It can be terraformed using dedicated provider - it also integrates directly with AWS Elasticsearch using IAM keys.

    Then first document created in that way will also build an index using given template (of course if name will match the pattern)