有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我想不出一种方法来将嵌套文档添加到现有的嵌套文档数组中(如果为空,则创建该数组)

我的映射看起来像:

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

  1. # 1 楼答案

    如果使用^{},则必须重新索引整个文档以添加另一个嵌套文档。下面是一个例子:

    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进行调查