Mickael Magniez
Mickael Magniez

Reputation: 111

ElasticSearch Dynamic Template for nested

I try to create dynamic template for a nested object.

Here is a document to index :

{
    "title": "My title",
    "attributes": {
        "color": {
            "id": 42,
            "label": "red"
        },
        "material": {
            "id": 43,
            "label": "textile"
        }
    }
}

This is the template i tried, without success

{
  "dynamic": "false",
  "dynamic_templates": [
    {
      "attributes": {
        "path_match": "attributes",
        "mapping": {
          "type": "nested"
        }
      }
    },
    {
      "attributes_nested": {
        "path_match": "attributes.*",
        "mapping": {
          "properties": {
            "id": {
              "type": "integer"
            },
            "value": {
              "type": "string"
            }
          }
        }
      }
    }
  ],
  "properties": {
    "title": {
      "type": "string"
    }
  }
}

I'd like to be able to make aggregations on attributes.color.id, and attributes.material.id

Upvotes: 1

Views: 3829

Answers (1)

Mickael Magniez
Mickael Magniez

Reputation: 111

Nevermind, the problem was that i had

{ "dynamic": false}

The correct mapping is

{
  "dynamic": "false",
  "dynamic_templates": [
    {
      "attributes_nested": {
        "path_match": "attributes.*",
        "mapping": {
          "properties": {
            "id": {
              "type": "integer"
            },
            "value": {
              "type": "string"
            }
          }
        }
      }
    }
  ],
  "properties": {
    "title": {
      "type": "string"
    },
    "attributes": {
      "type": "nested",
      "dynamic": true
    }
  }
}  

Upvotes: 6

Related Questions