gpt4 book ai didi

elasticsearch - 如何使用logstash从弹性中获取匹配条件的嵌套对象

转载 作者:行者123 更新时间:2023-12-03 00:51:42 26 4
gpt4 key购买 nike

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

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

最佳答案

阅读教程并花费大量时间后,我终于得到了上述查询的解决方案。我更改了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文件打印到控制台。因此,您可以选择两者,也可以根据需要对其进行修改。

-谢谢

关于elasticsearch - 如何使用logstash从弹性中获取匹配条件的嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52461704/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com