如何将字符串类型的Elasticsearch索引字段转换为json?

nbewdwxp  于 2022-11-22  发布在  ElasticSearch
关注(0)|答案(1)|浏览(401)

我有一个Opensearch索引,其中字符串字段message定义如下:

{"name":"message.message","type":"string","esTypes":["text"],"count":0,"scripted":false,"searchable":true,"aggregatable":false,"readFromDocValues":false}

示例数据:

"_source" : {
          "message" : {
            "message" : "user: AB, from: home, to: /app1"
          }
        }

我想把message列转换成json,这样我就可以分别访问message.usermessage.frommessage.to
我该怎么做?

czq61nw1

czq61nw11#

您可以使用Json Processor

POST /_ingest/pipeline/_simulate
{
  "pipeline": {
    "description": "convert json to object",
    "processors": [
      {
        "json": {
          "field": "foo",
          "target_field": "json_target"
        }
      }
    ]
  },
  "docs": [
    {
      "_index": "index",
      "_id": "id",
      "_source": {
        "foo": "{\"name\":\"message.message\",\"type\":\"string\",\"esTypes\":[\"text\"],\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false}\r\n"
      }
    }
  ]
}

回应:

{
  "docs": [
    {
      "doc": {
        "_index": "index",
        "_id": "id",
        "_version": "-3",
        "_source": {
          "foo": """{"name":"message.message","type":"string","esTypes":["text"],"count":0,"scripted":false,"searchable":true,"aggregatable":false,"readFromDocValues":false}
""",
          "json_target": {
            "esTypes": [
              "text"
            ],
            "readFromDocValues": false,
            "name": "message.message",
            "count": 0,
            "aggregatable": false,
            "type": "string",
            "scripted": false,
            "searchable": true
          }
        },
        "_ingest": {
          "timestamp": "2022-11-09T19:38:01.16232Z"
        }
      }
    }
  ]
}

相关问题