eswrapsearch
A simple Ruby wrapper for Elasticsearch, mostly for aggregational purposes
Building a query
Consider an index called "accounting" that has Invoice data(broken down into line items), Creditnotes(broken down into line items) with the following fields : DocumentType, Datestamp, DocumentId, DocumentLineId, ProductId, Description, Amount
Scenario : We want to get the total amount for all invoices generated in a certain time period
esquery = EsWrapSearch.new('127.0.0.1',9200,false)
startdate = Time.parse("2015-04-01 00:00:00").to_i*1000
enddate = Time.parse("2015-04-15 23:59:59").to_i*1000
esquery.must_range("Datestamp",startdate,enddate)
esquery.must_match("DocumentType","Invoice")
esquery.set_aggs([{"field" => "Amount", "type" => "sum"}])
esquery.search("accounting")
puts esquery.get_aggs
But it would be better if we can break down it down and get totals per invoice, then we rather sub-aggregate with set_aggs by adding another item in the array
esquery.set_aggs([{"field" => "DocumentId", "type" => "terms"},{"field" => "Amount", "type" => "sum"}])
esquery.search("accounting")
puts esquery.get_aggs
Getting the source documents ( you need to specify size otherwise it defaults to 0 and will return no source documents)
esquery.size(10)
esquery.search("accounting")
puts esquery.get_source