我正在尝试编写一个查询elasticsearch数据库并在其上构建报告的e ruby脚本。我正在使用jbuilder来构建这样的查询字符串:
require 'elasticsearch'
require 'date'
require 'jbuilder'
client = Elasticsearch::Client.new log: true, host: 'x.x.x.x', request_timeout: 10
filter_conditions = {}
filter_conditions['must'] = []
filter_conditions['should'] = []
filter_conditions['must'] << Jbuilder.encode do |json|
json.term do
json._type 'httpry-log'
end
end
filter_conditions['must'] << Jbuilder.encode do |json|
json.range do
json.set! '@timestamp' do
_now = DateTime.now
json.gte (_now - 1.00/24).strftime('%Q').to_i
json.lte _now.strftime('%Q').to_i
json.format 'epoch_millis'
end
end
end
query = Jbuilder.encode do |json|
json.size 10
json.query do
json.bool do
json.must do
json.array!(filter_conditions['must'])
end
end
end
end
puts query
但这是我得到的查询结果: 的 {&#34;大小&#34;:10,&#34;查询&#34; {&#34;布尔&#34; {&#34;必须&#34;:[&#34; { \&#34;术语\&#34;:{\&#34; _type \&#34;:\&#34; httpry日志\&#34;}}&#34;&#34; { \&#34;范围\&#34;:{\&#34; @timestamp \&#34;:{\&#34; GTE \&#34;:1477919154057,\&#34; LTE \&# 34;:1477922754057,\&#34;格式\&#34;:\&#34; epoch_millis \&#34;}}}&#34;]}}}
如何在主json输出中获取内部数组的未扩展版本?
提前致谢,
答案 0 :(得分:0)
假设查询每次都返回相同的对象和键:
a = your_query_hash
# The following map! will parse your string array into an array in ruby
a[:query][:bool][:must].map! { |arr| JSON.parse(arr) }
如果查询每次都没有返回相同的对象和密钥,我建议编写一个解析每个值的递归方法。