8

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?

1 Answer 1

1

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"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Sign up to request clarification or add additional context in comments.

1 Comment

Did this help @Alex? If not, how did you solve it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.