我有一个具有相同复杂类型属性的对象,我想在ElastcSearch中编制索引。
例如:
Root {
a : Foo
b : Foo
c : Foo
}
Foo {
x : Bar
y : Bar
z : Bar
}
Bar {
...
}
etc.
其中Root
是root,Foo, Bar
是嵌套对象。
如何避免在ElasticSearch映射JSON文件中复制类型Foo
和Bar
的嵌套类型定义?
答案 0 :(得分:0)
您可以使用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"
}
}
}
}
}
}
}
}
}
}
}