【问题标题】:Access array element after split processor in ingest node在摄取节点中拆分处理器后访问数组元素
【发布时间】:2017-06-23 12:49:05
【问题描述】:

我在使用摄取节点管道中的“拆分”处理器将字符串拆分为数组后尝试访问数组元素?

我有一个用斜杠 ('/') 分隔的长字符串。我只想将一个子字符串传递给索引,然后转储其余的。

例如,我有一个字符串“/aaa/bbb/ccc”。我只想索引“ccc”。

我目前的想法是使用split + set + remove,但是不知道如何访问分割后的数组元素。

PS:如果我使用 Logstash 有解决方案吗?

【问题讨论】:

    标签: elasticsearch logstash data-ingestion


    【解决方案1】:

    您可以使用script 处理器

    POST _ingest/pipeline/_simulate
    {
      "pipeline" :
      {
        "description": "_description",
        "processors": [
          {
            "script" : {
              "inline" : "ctx.foo = ctx.foo.substring(ctx.foo.lastIndexOf('/') + 1)"
            }
          }
        ]
      },
      "docs": [
        {
          "_index": "index",
          "_type": "type",
          "_id": "id",
          "_source": {
            "foo": "/divided/by/slashes"
          }
        }
      ]
    }
    

    返回

    {
        "docs": [
          {
              "doc": {
                  "_id": "id",
                  "_type": "type",
                  "_index": "index",
                  "_source": {
                      "foo": "slashes"
                  },
                  "_ingest": {
                     "timestamp": "2017-06-23T14:53:46.871Z"
                  }
               } 
           }
       ]
    

    }

    请注意,这不是完全的故障保护,但应该让您有一个初步的想法。

    【讨论】:

    • 非常感谢,alr!我还有一个问题:如果字符串是“/aaa/bbb/ccc/ddd/eee”而我想要“ccc”怎么办?如何检查“ccc”是否为整数?
    • 您可以先提取该字段,然后尝试使用convert 处理器将其转换为数字
    • "ccc" 可以是一串字母,也可以是一个整数。我想先检查它是否是整数。你在哪里找到像 lastIndexOf() 这样的函数?谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多