Motivation: Elasticsearch commands are confusing to write at a certain point. See the Elasticsearch reference for more help.
npm install sqltoes
var sqltoes = require('sqltoes')
var query = {select: ['sum(value)'], where: ['commodity = rice'], groupBy: ['description','year']}
sqltoes(query)
// { aggs: { where_commodity_rice: { filter: [Object], aggs: [Object] } } }
JSON.stringify(sqltoes(query))
// '{"aggs":{"where_commodity_rice":{"filter":{"term":{"commodity":"rice"}},"aggs":{"group_by_description":{"terms":{"field":"description","size":1000},"aggs":{"group_by_year":{"terms":{"field":"year","size":1000},"aggs":{"sum_value":{"sum":{"field":"value"}}}}}}}}}}'
Looks for three properties: select
, where
, and groupBy
(currently requires them all)
- Anything in the
select
clause is ignored unless it is wrapped insum, min, max, or avg
all of which become a final aggregation - The
where
clause supports two types of queries:[field] = [value]
will be treated as a term filter.[field] in ([value1],value[2])
will be treated as a terms filter.
- Anything in the
groupBy
clause is treated like the second example shown here.