我需要通过转换其他索引字段的数据来使用格式化字符串填充一些索引字段。 为此,我定义了一个包含脚本处理器的摄取管道。一切编译;但在建立索引后,目标字段不会填充任何值。
索引:
PUT my_index
{
"mappings": {
"product": {
"properties": {
"product_name": {"type": "text", "index": true},
"formatted_product_name": {"type": "keyword", "index": true},
"production_date": {"type": "keyword", "index": "true"},
"formatted_date": {"type": "keyword", "index": "true"}
}
}
}
}
有了这个示例索引,我想获取由摄取管道逻辑填充的字段 formatted_product_name
和 formatted_date
。
摄取管道(没有任何真正的逻辑):
PUT _ingest/pipeline/product_data_preprocessing
{
"processors" : [
{"script": {
"lang": "painless",
"inline": "def source_fields = [ctx.product_name, ctx.production_date]; def target_fields = [ctx.formatted_product_name, ctx.formatted_date]; for(def i=0; i<source_fields.length; i++) { target_fields[i] = source_fields[i]; }"
}}
]
}
数据:
PUT _bulk?pipeline=product_data_preprocessing
{"index": {"_index": "my_index", "_type": "product", "_id": "1"}}
{"product_name": "ipad", "production_date": "2017-02-17"}
{"index": {"_index": "my_index", "_type": "product", "_id": "2"}}
{"product_name": "tv", "production_date": "2017-10-07"}
查询:
获取我的索引/产品/_搜索
{
"query": {
"match_all": {}
}
}
备注:以下管道有效。但这不会扩展。因此,我正在寻找一种通过以动态方式处理某些源索引字段的值来填充一组目标字段的方法。
PUT _ingest/pipeline/product_data_preprocessing
{
"processors" : [
{"script": {
"lang": "painless",
"inline": "ctx.formatted_date = ctx.production_date"
}}
]
}
那么有没有一种方法可以在摄取管道处理器中定义(无痛)脚本,通过定义一组源字段和一组目标字段以及适当的处理逻辑来动态填充一组索引字段?
最佳答案
我一直在寻找如何使用摄取管道添加 count
字段并遇到了您的问题。经过大量试验和错误后,我设法编写了一个管道,它按换行符拆分字符串,然后为拆分数组中的条目数添加一个字段。不确定它是否有帮助,但无论如何它就在这里
{
"description" : "split content from Tika into rows",
"processors" : [
{
"gsub": {
"field": "content",
"pattern": "\\t+",
"replacement": " "
}
},
{
"split": {
"field": "content",
"separator": "\\n"
}
},
{
"script": {
"inline": "ctx.nrows = ctx.content.size()"
}
}
]
}
请注意,ctx.content
将是前 2 个处理器的结果
关于Elasticsearch:摄取节点 - 使用脚本处理器填充索引字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42374813/