I have an object with several properties of the same complex type which I want to index in ElastcSearch.
For example:
Root {
a : Foo
b : Foo
c : Foo
}
Foo {
x : Bar
y : Bar
z : Bar
}
Bar {
...
}
etc.
where Root
is root and Foo, Bar
are nested objects.
How do I avoid duplicating nested type definitions for types Foo
and Bar
in the ElasticSearch mapping JSON file?
You can use dynamic_templates:
PUT roots
{
"mappings": {
"dynamic_templates": [
{
"bar_template": {
"path_match": "*.*.*",
"match_mapping_type": "object",
"mapping": {
"type": "nested",
"properties": {
"bar1": {
"type": "integer"
}
}
}
}
},
{
"foo_template": {
"path_match": "*.*",
"match_mapping_type": "object",
"mapping": {
"type": "nested"
}
}
}
],
"properties": {
"root": {
"type": "nested"
}
}
}
}
POST roots/_doc
{
"root": {
"a": {
"x": {
"bar1": 1
},
"y": {
"bar1": 1
}
},
"b": {
"x": {
"bar1": 1
},
"y": {
"bar1": 1
}
},
"c": {
"x": {
"bar1": 1
},
"y": {
"bar1": 1
}
}
}
}
which then generates the following mapping for you:
GET roots/_mapping
{
"roots" : {
"mappings" : {
"dynamic_templates" : [
{
"bar_template" : {
"path_match" : "*.*.*",
"match_mapping_type" : "object",
"mapping" : {
"properties" : {
"bar1" : {
"type" : "integer"
}
},
"type" : "nested"
}
}
},
{
"foo_template" : {
"path_match" : "*.*",
"match_mapping_type" : "object",
"mapping" : {
"type" : "nested"
}
}
}
],
"properties" : {
"root" : {
"type" : "nested",
"properties" : {
"a" : {
"type" : "nested",
"properties" : {
"x" : {
"type" : "nested",
"properties" : {
"bar1" : {
"type" : "integer"
}
}
},
"y" : {
"type" : "nested",
"properties" : {
"bar1" : {
"type" : "integer"
}
}
}
}
},
"b" : {
"type" : "nested",
"properties" : {
"x" : {
"type" : "nested",
"properties" : {
"bar1" : {
"type" : "integer"
}
}
},
"y" : {
"type" : "nested",
"properties" : {
"bar1" : {
"type" : "integer"
}
}
}
}
},
"c" : {
"type" : "nested",
"properties" : {
"x" : {
"type" : "nested",
"properties" : {
"bar1" : {
"type" : "integer"
}
}
},
"y" : {
"type" : "nested",
"properties" : {
"bar1" : {
"type" : "integer"
}
}
}
}
}
}
}
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With