Elasticsearch更新/删除嵌套

时间:2014-12-16 05:45:04

标签: php elasticsearch

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

  1. 我可以添加一个(如果不存在)
  2. 我可以附加(如果确实存在)
  3. 无法弄清楚如何删除所选条目。
  4. 是否有一种方法可以使用(使用php客户端)添加条目(如果条目不存在)/更新条目(如果条目确实存在/删除第二条目)。

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

    感谢。

        {
        "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"
                            }
                        ]
                    }
                }
            ]
        }
    }
    

1 个答案:

答案 0 :(得分:1)

你想在同一个手术中做三个吗?

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

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);