Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch : reusing nested type mapping

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?

like image 220
Alex Vayda Avatar asked Jul 09 '14 12:07

Alex Vayda


1 Answers

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

like image 86
Joe - Elasticsearch Handbook Avatar answered Oct 14 '22 05:10

Joe - Elasticsearch Handbook