ethanxyz_0
ethanxyz_0

Reputation: 743

Elasticsearch - dynamic mapping with multi-field support

Is it possible to add new fields with multi-field support dynamically?

My index has properties that will only be known at indexing time. So these fields will be included with dynamic mapping.

But, when a new field is added dynamically, I need it to be mapped as text and with three sub-fields: keyword, date (if it fits with dynamic_date_formats) and long.

With these three sub-fields I will be able to search and aggregate many queries with maximum performance.

I know I can do a "pre" mapping my index with these "dynamic fields" using nested field with key and value properties so I can create the value property with these three sub-fields. But I don't want to create a nested key/value field because it's not very fast when performing aggregations with a lot of documents.

Upvotes: 0

Views: 2163

Answers (1)

ethanxyz_0
ethanxyz_0

Reputation: 743

I found it.

Dynamic templates is the answer.

Very simple :)

{
  "mappings": {
    "doc": {
      "dynamic_templates": [
        {
         "objs": {
            "match_mapping_type": "object",
            "mapping": {
                "type": "{dynamic_type}"
            }
         }  
        },
        {
          "attrs": {
            "match_mapping_type": "*",
            "mapping": {
              "type": "text",
              "fields": {
                "raw": {
                    "type": "keyword"
                },
                "long": {
                    "type": "long",
                    "ignore_malformed": true
                },
                "double": {
                    "type": "double",
                    "ignore_malformed": true
                },
                "date": {
                    "type": "date",
                    "format": "dd/MM/yyyy||dd/MM/yyyy HH:mm:ss||dd/MM/yyyy HH:mm",
                    "ignore_malformed": true
                }
              }
            }
          }
        }
      ],
      "dynamic": "strict",
      "properties": {
        "fixed": {
            "properties": {
                "aaa": {
                    "type": "text"
                },
                "bbb": {
                    "type": "long"
                },
                "ccc": {
                    "type": "date",
                    "format": "dd/MM/yyyy"
                }
            }
        },
        "dyn": {
            "dynamic": true,
            "properties": {
            }
        }
      }
    }
  }
}

Upvotes: 3

Related Questions