I wanted to do some tests using SQL queries to elasticsearch from commandline. I ended up with a really messy looking curl command:
curl -XPOST http://ACCOUNT:APIKEY@elasticsearch:9200/_sql?format=tsv -H 'Content-Type: application/json' -d '{"query":"select customer_number, complaint_address, tenant_name from "ip-owner" where dumptime > CURDATE() - INTERVAL 1 DAY and complaint_address is not null and complaint_address != '"''"' and customer_number = '"'0004'"' group by customer_number , complaint_address , tenant_name"}'
So I fiddled around a bit to make the real query a bit more readable. I thought the tip might be useful for others too, so here it is:
curl -XPOST http://ACCOUNT:APIKEY@elasticsearch:9200/_sql?format=tsv \
-H 'Content-Type: application/json' \
-d "{\"query\":\"$( while read line ; do echo -n $line' ' ; done <<'QUERY'
select customer_number
, complaint_address
, tenant_name
from \\"ip-owner\\"
where dumptime > CURDATE() - INTERVAL 1 DAY
and complaint_address is not null
and complaint_address != ''
and customer_number = '0004'
group by customer_number
, complaint_address
, tenant_name
QUERY
)\"}"
The first 3 lines have to be adjusted to one's own requirements. The last 2 lines have to be used as they are. In between one can simply put the query as one wants except for double quotes which have to be escaped by 2 backslashes.
I hope this tip is useful.