Elasticsearch将值附加到Array字段

时间:2016-04-28 17:32:29

标签: elasticsearch

我正在使用Elasticsearch v2.1.1。我已经用一些字段索引了一个数据集,如关键字,Collection等。

我的样本索引数据集如下:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 103,
    "max_score": 1,
    "hits": [
      {
        "_index": "pibtest1",
        "_type": "SearchTech",
        "_id": "http://www.searchtechnologies.com/images/solutions/candidate-search-match-dashboard.PNG",
        "_score": 1,
        "_source": {
          "Collection": "default_collection",
          "keywords": "keywords-NOT-PROVIDED"
          }
      }
    }
}

现在我想将逗号分隔值附加到Collection字段。

例如:“Collection”:[“default_collection”,“wiki_collection”]

现在,Collection字段的类型为“string”。我相信Collection字段需要是类型数组。那么在索引ES中的数据之前,我应该创建一个数组类型映射吗?如果是的话,我该怎么做? 我试图创建映射(在索引数据之前),如下所示,但它不起作用,并给了我一个错误。

PUT pibtest1
{
  "mappings":{
    "SearchTech": {
      "properties": {
        "Collection" :{
          "type": "array",
          "index": "analyzed"
        }
      }
    }
  }
}

错误:

{
   "error": {
      "root_cause": [
         {
            "type": "mapper_parsing_exception",
            "reason": "No handler for type [array] declared on field [Collection]"
         }
      ],
      "type": "mapper_parsing_exception",
      "reason": "Failed to parse mapping [SearchTech]: No handler for type [array] declared on field [Collection]",
      "caused_by": {
         "type": "mapper_parsing_exception",
         "reason": "No handler for type [array] declared on field [Collection]"
      }
   },
   "status": 400
}

如何使用_update api?我读到更新api将使用新值替换现有值,但我需要将值附加到数组。 我想搜索查询并更新结果的Collection字段。谢谢。

我发现了类似的帖子:Append to an existing elasticsearch array field using python

1 个答案:

答案 0 :(得分:3)

您无需更改任何内容。在Elasticsearch中,每个字段都可以处理一个值或值数组:

-z

      POST index/type/id
      {
      "Collection": "default_collection",
      "keywords": "keywords-NOT-PROVIDED"
      }

都使用相同的映射