lib/ej/core.rb in ej-0.1.2 vs lib/ej/core.rb in ej-0.1.3

- old
+ new

@@ -114,20 +114,40 @@ "filter"=>{"bool"=>{"must"=>[{"match_all"=>{}}]}}}}}}}}, "size"=>0} @client.search index: @index, body: body end - def aggs(term, size, query) - body = {"size"=>0, - "query"=> - {"filtered"=> - {"query"=>{"query_string"=>{"query"=> query}}, - "filter"=>{"bool"=>{"must"=>[], "must_not"=>[]}}}}, - "aggs"=> - {"agg_" + term => - {"terms"=>{"field"=>term, "size"=>size, "order"=>{"_count"=>"desc"}}}}} - results = @client.search index: @index, body: body - results['aggregations']["agg_" + term]['buckets'] + def aggs(terms, size, query) + body = { + "size"=>0, + "query"=>{ + "query_string"=>{ + "query"=>query + } + } + } + + agg_terms = [] + code = %Q{['aggs']} + terms.each_with_index do |term, i| + term_name = "agg_#{term}" + aggs_body = { + term_name=>{ + "terms"=>{ + "field"=>term, + "size"=>size, + "order"=>{ + "_count"=>"desc" + } + } + } + } + + eval(%Q{body#{code} = aggs_body}) + code += %Q{['#{term_name}']['aggs']} + end + + @client.search index: @index, body: body end def min(term) body = { aggs: {