2015-03-02 113 views
-2

我不能想出一个办法,以嵌套的文档添加到嵌套文档的现有阵列(或创建数组,如果它为空)添加一个嵌套的文件嵌套文档的数组

我的映射是像:

{ 
    "example" : { 
    "properties" : { 
     "name" : { "type" : "string", "index" : "not_analyzed" }, 
     "pets" : { 
      "type" : "nested", 
      "properties": { 
       "name" : {"type": "string", "index": "not_analyzed" }, 
       "type" : {"type": "string", "index": "not_analyzed" } 
      } 
     } 
    } 
    } 
} 

回答

1

如果您使用"nested"你必须重新索引整个文档添加另一个嵌套的文档。这里有一个例子:

DELETE /test_index 

PUT /test_index 
{ 
    "settings": { 
     "number_of_shards": 1 
    }, 
    "mappings": { 
     "doc": { 
     "properties": { 
      "name": { 
       "type": "string", 
       "index": "not_analyzed" 
      }, 
      "pets": { 
       "type": "nested", 
       "properties": { 
        "name": { 
        "type": "string", 
        "index": "not_analyzed" 
        }, 
        "type": { 
        "type": "string", 
        "index": "not_analyzed" 
        } 
       } 
      } 
     } 
     } 
    } 
} 

PUT /test_index/doc/1 
{ 
    "name": "Sloan", 
    "pets": [ 
     { "name": "Pearl", "type": "cat" }, 
     { "name": "Dexter", "type": "cat" } 
    ] 
} 

POST /test_index/_search 
... 
{ 
    "took": 2, 
    "timed_out": false, 
    "_shards": { 
     "total": 1, 
     "successful": 1, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 1, 
     "max_score": 1, 
     "hits": [ 
     { 
      "_index": "test_index", 
      "_type": "doc", 
      "_id": "1", 
      "_score": 1, 
      "_source": { 
       "name": "Sloan", 
       "pets": [ 
        { 
        "name": "Pearl", 
        "type": "cat" 
        }, 
        { 
        "name": "Dexter", 
        "type": "cat" 
        } 
       ] 
      } 
     } 
     ] 
    } 
} 

PUT /test_index/doc/1 
{ 
    "name": "Sloan", 
    "pets": [ 
     { "name": "Pearl", "type": "cat" }, 
     { "name": "Dexter", "type": "cat" }, 
     { "name": "Momo", "type": "cat" } 
    ] 
} 

POST /test_index/_search 
... 
{ 
    "took": 8, 
    "timed_out": false, 
    "_shards": { 
     "total": 1, 
     "successful": 1, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 1, 
     "max_score": 1, 
     "hits": [ 
     { 
      "_index": "test_index", 
      "_type": "doc", 
      "_id": "1", 
      "_score": 1, 
      "_source": { 
       "name": "Sloan", 
       "pets": [ 
        { 
        "name": "Pearl", 
        "type": "cat" 
        }, 
        { 
        "name": "Dexter", 
        "type": "cat" 
        }, 
        { 
        "name": "Momo", 
        "type": "cat" 
        } 
       ] 
      } 
     } 
     ] 
    } 
} 

根据您的使用情况,您可能需要使用parent/child relationship,而不是进行调查。