【问题标题】:ElasticSearch : reusing nested type mappingElasticSearch:重用嵌套类型映射
【发布时间】:2020-04-17 01:13:06
【问题描述】:

我有一个具有多个相同复杂类型属性的对象,我想在 ElastcSearch 中对其进行索引。

例如:

Root {
  a : Foo
  b : Foo
  c : Foo
}

Foo {
  x : Bar
  y : Bar
  z : Bar
}

Bar {
   ...
}

etc.

其中Root 是根,Foo, Bar 是嵌套对象。

如何避免在 ElasticSearch 映射 JSON 文件中重复类型 FooBar 的嵌套类型定义?

【问题讨论】:

    标签: elasticsearch mapping


    【解决方案1】:

    你可以使用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
          }
        }
      }
    }
    

    然后为您生成以下映射:

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

    【讨论】:

    • 这对@Alex 有帮助吗?如果没有,你是怎么解决的?
    猜你喜欢
    • 1970-01-01
    • 2018-11-17
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 2013-10-19
    • 2021-12-11
    相关资源
    最近更新 更多