简体   繁体   English

如何以编程方式创建Kibana(Elasticsearch)脚本字段?

[英]How to create a Kibana (Elasticsearch) Scripted Field programatically?

Kibana's UI allows the user to create a scripted field which is stored as part of the index (screenshot below). Kibana的UI允许用户创建一个脚本化字段,该字段存储为索引的一部分(下面的屏幕截图)。 How can that be done programatically? 怎么能以编程方式完成? In particular, using either the NEST client or the Elasticsearch low level client. 特别是,使用NEST客户端或Elasticsearch低级客户端。

Kibana UI for the Indice with the Scripted Fields tab highlighted 突出显示了带有脚本字段选项卡的Indice的Kibana UI

Note that I am not asking how to create add an expression/script field as part of a query, I'm specifically looking for how to add it as part of the Index when the mapping is created so that queries can reference it without having to explicitly include it. 请注意,我不是要求如何创建添加表达式/脚本字段作为查询的一部分,我特别在寻找如何在创建映射时将其添加为索引的一部分,以便查询可以引用它而不必明确地包括它。

Kibana dashboards are stored in the .kibana index. Kibana仪表板存储在.kibana索引中。 To export dashboards, you can query the Kibana index as you would any other index. 要导出仪表板,您可以像查询任何其他索引一样查询Kibana索引。 For example, curl -XGET http://localhost:9200/.kibana/_search?type=dashboard&pretty would show the JSON for your dashboards. 例如, curl -XGET http://localhost:9200/.kibana/_search?type=dashboard&pretty将显示仪表板的JSON。 You could export the template, add the scripted field to the JSON, and then POST it again. 您可以导出模板,将脚本字段添加到JSON,然后再次POST。 Since Kibana uses a standard Elasticsearch index, the normal Elasticsearch API would apply to modifying Kibana dashboards. 由于Kibana使用标准的Elasticsearch索引,因此普通的Elasticsearch API将适用于修改Kibana仪表板。 This may provide a little more clarification. 可以提供更多的说明。

At the time of writing, current version 5.2 does not have an official way to do this. 在撰写本文时,当前版本5.2没有正式的方法来执行此操作。

This is how I do it: 我是这样做的:

  1. Get index fields: GET /.kibana/index-pattern/YOUR_INDEX 获取索引字段: GET /.kibana/index-pattern/YOUR_INDEX
  2. Add your scripted field to _source.fields (as string, notice scaped quotation marks) 将脚本字段添加到_source.fields (作为字符串,注意跳过引号)

     "fields":"[...,{\\"name\\":\\"test\\",\\"type\\":\\"number\\",\\"count\\":0,\\"scripted\\":true,\\"script\\":\\"doc['area_id'].value\\",\\"lang\\":\\"painless\\",\\"indexed\\":false,\\"analyzed\\":false,\\"doc_values\\":false,\\"searchable\\":true,\\"aggregatable\\":true}]" 
  3. Post back _source json to /.kibana/index-pattern/YOUR_INDEX 将_source json发回到/.kibana/index-pattern/YOUR_INDEX

     { "title":"YOUR_INDEX", "timeFieldName":"time", "fields":"[...,{\\"name\\":\\"test\\",...}]" } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM