弹性搜索更新/删除嵌套


Elasticsearch Updating / Deleting Nested

我已经浏览了一些示例和文档,并找到了一个解决方案,更新了此结果集中的嵌套对象。

  1. 我可以添加一个(如果不存在)
  2. 我可以附加到它(如果确实存在)
  3. 无法弄清楚如何删除所选条目。

有没有一种方法我可以使用(使用 php 客户端)添加一个条目(如果它不存在/更新一个条目(如果它确实存在)/删除第二个条目。

我继承了这个问题,并且是 Elastic 搜索的新手。

谢谢。

    {
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 1,
        "hits": [
            {
                "_index": "products",
                "_type": "categories",
                "_id": "AUpRjtKZfXI7LIe9OpNx",
                "_score": 1,
                "_source": {
                    "name": "Primary",
                    "description": "Primary Category",
                    "slug": "Primary",
                    "created": "2014-12-16 00:25:22",
                    "parent": [
                        {
                            "name": "First One",
                            "description": "Test",
                            "id": "ae74ea4e2e865ed3fd60c18a06e69c65",
                            "slug": "first-one"
                        },
                        {
                            "name": "Second One",
                            "description": "Testing Again",
                            "id": "c8dbe5143c8dfd6957fa33e6cea7a0a8",
                            "slug": "second-one"
                        }
                    ]
                }
            }
        ]
    }
}

你想在同一操作中做这三个操作吗?

删除第二个

嵌套对象是通过删除第二个元素的脚本实现的:

PUT /products
{
  "mappings": {
    "categories": {
      "properties": {
        "parent": {
          "type": "nested", 
          "properties": {
            "name":    { "type": "string"  },
            "description": { "type": "string"  },
            "id":     { "type": "string", "index": "not_analyzed"   },
            "slug":   { "type": "string"   }
          }
        }
      }
    }
  }
}
PUT /products/categories/1
{
   "name": "Primary",
   "description": "Primary Category",
   "slug": "Primary",
   "created": "2014-12-16 00:25:22",
   "parent": [
      {
         "name": "First One",
         "description": "Test",
         "id": "ae74ea4e2e865ed3fd60c18a06e69c65",
         "slug": "first-one"
      },
      {
         "name": "Second One",
         "description": "Testing Again",
         "id": "c8dbe5143c8dfd6957fa33e6cea7a0a8",
         "slug": "second-one"
      }
   ]
}
POST /products/categories/1/_update
{
    "script" : "ctx._source.parent.remove(1)",
    "lang": "groovy"
}
GET /products/categories/1
因此,在PHP

代码(使用官方PHP客户端)中,更新如下所示:

$params = [
  'index' => 'products',
  'type' => 'categories',
  'id' => 1,
  'body' => [
    'script' => 'ctx._source.parent.remove(1)',
    'lang' => 'groovy'
  ]
];
$result = $client->update($params);