Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create ElasticSearch dynamic template to ensure all fields are set to not_analyzed

I have an ElasticSearch type for which I want the mapping to be set dynamically. There are a few select fields on that type that I want to be analyzed, but everything else should be set to "not_analyzed".

I've come up with the following snippet. This sets all string fields to not be analyzed, but doesn't cover all other data types. I tried using the "generic" field shown in the documentation but it didn't help. Can anyone tell me how I can accomplish this?

{
  "TypeName": {
    "dynamic_templates": [
      {
        "template_name": {
          "match": "*",
          "match_mapping_type": "string",
          "mapping": {
            "index": "no",
            "type": "string"
          }
        }
      }
    ],
    "dynamic": true,
    "properties": {
      "url": {
        "index": "analyzed",
        "type": "string"
      },
      "resourceUrl": {
        "index": "analyzed",
        "type": "string"
      }
    }
  }
}
like image 701
Stephen Avatar asked Jul 07 '15 22:07

Stephen


1 Answers

{
  "mappings": {
    "TypeName": {
      "dynamic_templates": [
        {
          "base": {
            "mapping": {
              "index": "not_analyzed"
            },
            "match": "*",
            "match_mapping_type": "*"
          }
        }
      ],
      "dynamic": true,
      "properties": {
        "url": {
          "index": "analyzed",
          "type": "string"
        },
        "resourceUrl": {
          "index": "analyzed",
          "type": "string"
        }
      }
    }
  }
}

Overall, index-level template:

{
  "mappings": {
    "_default_": {
      "dynamic_templates": [
        {
          "base": {
            "mapping": {
              "index": "not_analyzed"
            },
            "match": "*",
            "match_mapping_type": "*"
          }
        }
      ]
    }
  }
}
like image 182
Andrei Stefan Avatar answered Sep 30 '22 13:09

Andrei Stefan