Logstash导入嵌套类型至Elasticsearch:两项技术问题求助
解决方案
1. 为posts属性设置nested类型
Elasticsearch不会自动识别nested类型,必须手动预创建索引映射:
- 通过ES的REST API创建目标索引并指定nested映射:
PUT /your_target_index { "mappings": { "properties": { "source": { "properties": { "posts": { "type": "nested", "properties": { "title": {"type": "text"}, "content": {"type": "text"}, "publish_date": {"type": "date"} // 根据你的实际业务字段补充其他子属性 } }, // 补充source下的其他字段映射 "user_id": {"type": "keyword"}, "username": {"type": "text"} } } } } }
- 配置Logstash输出时,指定该已创建的索引,同时关闭自动模板管理避免覆盖自定义映射:
output { elasticsearch { hosts => ["http://localhost:9200"] index => "your_target_index" manage_template => false # 关键:禁止Logstash自动生成默认模板 } }
2. 将posts设为source的子属性
在Logstash的filter阶段使用mutate插件调整字段层级:
- 如果原始数据中posts是顶级字段,用
rename将其移动到source节点下:
filter { mutate { rename => { "posts" => "[source][posts]" } } }
- 确保posts字段为数组格式(nested类型要求子元素是数组内的独立对象),若原始数据不是数组,可通过
split转换(示例以分号为分隔符,根据实际情况调整):
filter { mutate { split => { "[source][posts]" => ";" } } }
内容的提问来源于stack exchange,提问作者user2545517
相关产品推荐
相关产品推荐

