如何使用logstash从弹性中获取符合条件的嵌套对象

时间:2018-09-22 23:23:41

标签: elasticsearch logstash

我正在尝试使用logstash和以下名为“ export-nested.conf”的配置文件从弹性搜索中使用嵌套数据类型来检索嵌套对象

input {
 elasticsearch {
    hosts => "localhost:9200"
    index => "test"
    query => '
  {"query": {
      "nested": {
      "path": "comments",
      "query": {
        "match": {"comments.active": true}
      },
      "inner_hits": {
         "highlight": {
          "fields": {
            "comments.active": {}
          }
        }
      }
    }
}}'
  }
}
output {
   csv {
    fields => ["comments.author","comments.number"]
    path => "output.csv"
  }
}

要重现该问题: 步骤1:- 我使用以下映射创建了以下索引

PUT test
{
  "mappings": {
    "_doc": {
      "properties": {
        "comments": {
          "type": "nested"
        }
      }
    }
  }
}

步骤2:- 在我创建的索引中输入数据:

PUT test/_doc/1?refresh
{
  "title": "Test1",
  "comments": [
    {
      "author": "elis",
      "number": 1,
      "active": true
    },
    {
      "author": "zara",
      "number": 2,
      "active": false
    }
  ]
}

PUT test/_doc/2?refresh
{
  "title": "Test2",
  "comments": [
    {
      "author": "john",
      "number": 3,
      "active": false
    },
    {
      "author": "rob",
      "number": 4,
      "active": true
    }
  ]
}

步骤3:- 使用以下命令运行logstash

bin/logstash -f export-nested.conf

输出: 我在输出文件中得到空白数据。

,
,

预期输出:

elis,1
rob,4

1 个答案:

答案 0 :(得分:2)

在阅读了教程并花费了大量时间之后,我终于得到了上述查询的解决方案。我更改了logstash配置文件以解决此问题。我已经测试过了,它给了我想要的输出。

input {
 elasticsearch {
    hosts => "localhost:9200"
    index => "objectindex"
    query => '
      {"query": {
        "match": {"comments.active": true}
      }}'
  }
}
filter {
   split {
     field => "comments"
   }
}
output {
  if [comments][active] {  
    stdout { codec => rubydebug }
    csv {
      fields => ["[comments][author]","[comments][number]"]
      path => "output.csv"
    }
  }
}

输出:-

elis,1
rob,4

在这里,我已使用过滤器拆分了注释数组,然后仅导出那些其comment.active为true的对象的数据。

此配置可以与默认数据类型“对象”的嵌套对象“注释”一起使用,并且在输出插件中,我将其以及csv文件打印到控制台。因此,您可以选择两者,也可以根据需要对其进行修改。

-谢谢

相关问题