1
votes

I would like to create an index in which only some fields are indexed. I created a template with the enabled property set to false. So no field is indexed by default. https://www.elastic.co/guide/en/elasticsearch/reference/6.4/enabled.html

Then I defined the fields I want to index with dynamic templates. After I inserted a document, no field is indexed. I guess it's because enabled:false is applied on the children of the root element, and since none should be indexed, the dynamic templates are not applied.

Is there a way to set the enabled to false for all fields not covered by the dynamic templates?

DELETE so

DELETE _template/test

PUT _template/test
{
  "index_patterns": [
    "*so*"
  ],
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "_doc": {
      "dynamic": true,
      "enabled": false,
      "dynamic_templates": [
        {
          "typeOfMaterial": {
            "path_match": "*.material.typeOfMaterial",
            "mapping": {
              "enabled": true,
              "type": "nested"
            }
          }
        },
        {
          "typeOfMaterialCode": {
            "path_match": "*.material.typeOfMaterial.code",
            "mapping": {
              "enabled": true,
              "type": "keyword"
            }
          }
        }
      ]
    }
  }
}

PUT so/_doc/1
{
  "count": 5,
  "AAA": {
    "material": {
      "typeOfMaterial": [
        {
          "code": "MAT1"
        }
      ]
    }
  }
}
1

1 Answers

0
votes

According to the documentation:

Templates are processed in order — the first matching template wins.

Based on this assumption, I would try to modify the template like this:

PUT _template/test
{
  "index_patterns": [
    "*so*"
  ],
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "_doc": {
      "dynamic": true,
      "dynamic_templates": [
        {
          "typeOfMaterial": {
            "path_match": "*.material.typeOfMaterial",
            "mapping": {
              "enabled": true,
              "type": "nested"
            }
          }
        },
        {
          "typeOfMaterialCode": {
            "path_match": "*.material.typeOfMaterial.code",
            "mapping": {
              "enabled": true,
              "type": "keyword"
            }
          }
        },
        {
          "allOtherFields": {
            "match": "*",
            "mapping": {
              "enabled": false
            }
          }
        }
      ]
    }
  }
}