【问题标题】:Logstash : Is there a way to change some of the properties in document while migratingLogstash:有没有办法在迁移时更改文档中的某些属性
【发布时间】:2020-02-17 14:26:37
【问题描述】:

我一直在使用 Logstash 将一些索引从自托管 Elasticsearch 迁移到 AmazonElasticSearch。在迁移文档时,我们需要根据一些逻辑更改索引中的字段名称。

我们的 Logstash 配置文件

input {
 elasticsearch {
 hosts => ["https://staing-example.com:443"]
 user => "userName"
 password => "password"
 index => "testingindex"
 size => 100
 scroll => "1m"
 }
}

filter {

}

output {
 amazon_es {
 hosts => ["https://example.us-east-1.es.amazonaws.com:443"]
 region => "us-east-1"
 aws_access_key_id => "access_key_id"
 aws_secret_access_key => "access_key_id"
 index => "testingindex"
}
stdout{
  codec => rubydebug
  }
}

这是我们自托管弹性搜索中 testingIndex 的文档之一

{
    "uniqueIdentifier" => "e32d331b-ce5f-45c8-beca-b729707fca48",
         "createdDate" => 1527592562743,
     "interactionInfo" => [
         {
                        "value" => "Hello this is testing",
                        "title" => "msg",
            "interactionInfoId" => "8c091cb9-e51b-42f2-acad-79ad1fe685d8"
        },
         {
                        **"value"** => """"{"edited":false,"imgSrc":"asdfadf/soruce","cont":"Collaborated in  <b class=\"mention\" gid=\"4UIZjuFzMXiu2Ege6cF3R4q8dwaKb9pE\">@2222222</b>  ","chatMessageObjStr":"Btester has quoted your feed","userLogin":"test.comal@google.co","userId":"tester123"}"""",
                        "title" => "msgMeta",
            "interactionInfoId" => "f6c7203b-2bde-4cc9-a85e-08567f082af3"
        }
    ],
         "componentId" => "compId",

               "status" => [
                "delivered"
        ]
    },
           "accountId" => "test123",
       "applicationId" => "appId"
}

当文档迁移到我们的 AmazonElasticSearch 时,这是我们所期望的

{
    "uniqueIdentifier" => "e32d331b-ce5f-45c8-beca-b729707fca48",
         "createdDate" => 1527592562743,
     "interactionInfo" => [
         {
                        "value" => "Hello this is testing",
                        "title" => "msg",
            "interactionInfoId" => "8c091cb9-e51b-42f2-acad-79ad1fe685d8"
        },
         {
                        **"value-keyword"** => """"{"edited":false,"imgSrc":"asdfadf/soruce","cont":"Collaborated in  <b class=\"mention\" gid=\"4UIZjuFzMXiu2Ege6cF3R4q8dwaKb9pE\">@2222222</b>  ","chatMessageObjStr":"Btester has quoted your feed","userLogin":"test.comal@google.co","userId":"tester123"}"""",
                        "title" => "msgMeta",
            "interactionInfoId" => "f6c7203b-2bde-4cc9-a85e-08567f082af3"
        }
    ],
         "componentId" => "compId",

               "status" => [
                "delivered"
        ]
    },
           "accountId" => "test123",
       "applicationId" => "appId"
}

我们需要将 "value" 字段更改为 "value-keyword",只要我们找到一些 JSON 格式。 Logstash 中是否还有其他过滤器可以实现此目的

【问题讨论】:

标签: elasticsearch logstash amazon-elasticsearch


【解决方案1】:

尝试将此添加到您的过滤器中:

filter {
  ruby {
    code => "event.get('interactionInfo').each { |item| if item['value'].match(/{.+}/) then item['value-keyword'] = item.delete('value') end }"
  }
}

【讨论】:

  • 。提交的值是嵌套文档。我们不确定每份文件中提交的价值有多少。
  • @Thilak,对不起,我没有注意到 interactionInfo 是一个数组。请尝试我的更新答案。
  • @Thilak 您能否在您的解决方案中发布答案,以便每个人都可以从中学习?
  • @Thilak 没关系,我找到了:stackoverflow.com/q/58538903/65060
【解决方案2】:

如 Logstash 网站中所述:

https://www.elastic.co/guide/en/logstash/current/plugins-filters-mutate.html#plugins-filters-mutate-rename

您可以使用 mutate 过滤器,应用重命名功能。

例如:

filter {
  mutate {
    replace => { "old-field" => "new-field" }
  }
}

对于嵌套字段,您可以只传递字段的路径:

filter {
  mutate {
    replace => { "[interactionInfo][value]" => "[interactionInfo][value-keyword]" }
  }
}

【讨论】:

  • 您好,我知道 mutate 过滤器。我在这里需要的是仅在满足某些条件时更改字段名称。这是可能的
  • 这取决于您需要应用条件运算符的数据在哪里。例如,如果您的事件中有一个字段document_type,您可以使用if [document_type] == "process" mutate { ... }。那是在mutate 过滤器之外,但在filter 块内。你觉得怎么样?
猜你喜欢
  • 2021-03-12
  • 1970-01-01
  • 2013-07-22
  • 1970-01-01
  • 2020-12-02
  • 1970-01-01
  • 2015-06-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多