将嵌套文档添加到嵌套文档数组中

时间:2015-03-02 19:28:13

标签: java elasticsearch

我无法找到一种方法将嵌套文档添加到现有的嵌套文档数组中(如果数组为null,则创建数组)

我的映射看起来像:

{
  "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 个答案:

答案 0 :(得分: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进行调查。