在Elasticsearch中重新索引时如何将对象数组转换为字符串数组?

时间:2018-07-12 13:22:48

标签: elasticsearch

让我们说源索引有一个像这样的文档:

{
   "name":"John Doe",
   "sport":[
       {
          "name":"surf",
          "since":"2 years"
       },
       {
          "name":"mountainbike",
          "since":"4 years"
       },
   ]
}

如何丢弃“自”信息,以便一旦重新索引该对象将仅包含运动名称?像这样:

{
   "name":"John Doe",
   "sport":["surf","mountainbike"]
}

请注意,如果结果字段保留相同的名称,那不是很好。

1 个答案:

答案 0 :(得分:2)

我不知道您使用的是哪个版本的Elasticsearch,但这是基于pipelines的解决方案,它是ES v5.0中引入节点的引入。

  • 1)script处理器用于从每个子对象中提取值并将其设置在另一个字段(此处为sports
  • 2)前一个sport字段已由remove处理器删除

您可以使用Simulate pipeline API对其进行测试:

POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "description": "random description",
    "processors": [
      {
        "script": {
          "lang": "painless",
          "source": "ctx.sports =[]; for (def item : ctx.sport) { ctx.sports.add(item.name)  }"
        }
      },
      {
        "remove": {
          "field": "sport"
        }
      }
    ]
  },
  "docs": [
    {
      "_index": "index",
      "_type": "doc",
      "_id": "id",
      "_source": {
        "name": "John Doe",
        "sport": [
          {
            "name": "surf",
            "since": "2 years"
          },
          {
            "name": "mountainbike",
            "since": "4 years"
          }
        ]
      }
    }
  ]
}

输出以下结果:

{
  "docs": [
    {
      "doc": {
        "_index": "index",
        "_type": "doc",
        "_id": "id",
        "_source": {
          "name": "John Doe",
          "sports": [
            "surf",
            "mountainbike"
          ]
        },
        "_ingest": {
          "timestamp": "2018-07-12T14:07:25.495Z"
        }
      }
    }
  ]
}

可能有更好的解决方案,因为我没有太多使用管道,或者您可以在将文档提交到Elasticsearch集群之前使用Logstash过滤器进行过滤。

有关管道的更多信息,请查看reference documentation of ingest nodes