在弹性搜索中添加新的派生字段

时间:2017-08-23 08:38:43

标签: elasticsearch elasticsearch-2.0

我想在现有索引loctype中添加新字段ymetrics,以便新字段具有现有字段location的值,如下所示。

ctx._source.location.substring(0, ctx._source.location.indexOf(1,\"/\"))"

我在下面尝试过,但似乎完全不同的解释并添加了一个id为#34; _update"

的新记录
POST /ymetrics/request/_update
{
  "script": "ctx._source.loctype = ctx._source.location.substring(0, ctx._source.location.indexOf(1,\"/\"))"
}

如何添加新的field,以使用新字段值更新所有现有documents。而且每当添加新文档时,这个新字段将具有来自上面expression派生的其他字段的值?

这背后的原因是我无法更改创建document的第三方库,也不能在从源插入时更改现有field的值。所以我需要这个新的field根据我的需要放置一些图表。

我使用的是elasticsearch 2.x版本,因此script field不会采用除number之外的任何其他类型。

使用_update_by_query后,抛出解析异常。

查询:

POST /ymetrics/_update_by_query
{
  "script": "ctx._source.loctype = doc['resource'].substring(0, doc['location'].indexOf(1,'/'))"
  "query": {
        "match_all": {}
    }
}

错误:

{
   "error": {
      "root_cause": [
         {
            "type": "parse_exception",
            "reason": "Failed to parse content to map"
         }
      ],
      "type": "parse_exception",
      "reason": "Failed to parse content to map",
      "caused_by": {
         "type": "json_parse_exception",
         "reason": "Unexpected character ('\"' (code 34)): was expecting comma to separate Object entries\n at [Source: org.elasticsearch.transport.netty.ChannelBufferStreamInput@34ceed6e; line: 3, column: 4]"
      }
   },
   "status": 400
}

0 个答案:

没有答案
相关问题