Elasticsearch query DSL to SQL, clause by clause
Paste your Elasticsearch DSL query above and read it back as SQL. Converts bool queries to WHERE, aggregations to GROUP BY, range queries to comparison operators. Whatever has no SQL counterpart is listed in a note instead of quietly disappearing, and everything runs in your browser.
How to Convert Elasticsearch to SQL — Step-by-Step
Input Your Elasticsearch Query
Got an Elasticsearch query you need to understand as SQL? Paste the JSON body from your Kibana Dev Tools or your application code (the request you send, not the response you get back). You can include the GET /index/_search line — the converter extracts the index name automatically.
.json or .txt fileExample Elasticsearch Input
A typical bool query combining term, range, and terms filters:
GET /products/_search { "query": { "bool": { "must": [ { "term": { "status": "active" } }, { "range": { "price": { "gte": 50 } } }, { "terms": { "category": ["electronics", "clothing"] } } ] } } }
Automatic Conversion to SQL
The converter maps Elasticsearch query types to standard SQL:
WHERE ... AND ...(... OR ...) when there is no must or filter, or when minimum_should_match is set(field IS NULL OR NOT (...)), so rows without the field are kept, as in ElasticsearchIN ('a', 'b')>= / <= comparisonsLIKE, with any literal % or _ escapedIS NOT NULLORDER BY, LIMIT and OFFSET (LIMIT 10 when size is missing, like Elasticsearch)SQL Output
The bool query from Step 1 produces this SQL statement:
-- Note: No "size" was given, and Elasticsearch returns 10 hits by default, so the SQL has LIMIT 10. SELECT * FROM products WHERE status = 'active' AND price >= 50 AND category IN ('electronics', 'clothing') LIMIT 10;
Aggregation to GROUP BY
Elasticsearch aggregations are converted into SQL GROUP BY clauses with the appropriate aggregate functions. Each terms aggregation gets its own statement with the field as the grouping column and a doc_count, while sub-aggregations like sum, avg, min, and max map to their SQL equivalents.
Elasticsearch Aggregation
A terms aggregation grouped by category with sum and avg sub-aggregations:
{ "size": 0, "aggs": { "by_category": { "terms": { "field": "category" }, "aggs": { "total_revenue": { "sum": { "field": "revenue" } }, "avg_price": { "avg": { "field": "price" } } } } } }
SQL Output
What the converter writes for it (size 0, so there is no hits query):
-- aggregation "by_category" SELECT category, COUNT(*) AS doc_count, SUM(revenue) AS total_revenue, AVG(price) AS avg_price FROM table_name WHERE category IS NOT NULL GROUP BY category ORDER BY doc_count DESC, category ASC LIMIT 10;
Copy or Download
.sql fileFrequently Asked Questions — Elasticsearch to SQL
Which Elasticsearch queries can it convert?
bool (must, filter, should and must_not, including minimum_should_match), term, terms, range, exists, ids, prefix, wildcard, match, match_phrase, multi_match, constant_score, dis_max, match_all and match_none. sort becomes ORDER BY, _source becomes the column list, and size and from become LIMIT and OFFSET. Anything else, such as query_string, nested, geo or script queries, is left out and named in a "-- Note:" line above the SQL, so you can see what still needs doing by hand.
Can I paste a request straight from Kibana Dev Tools?
Yes. Keep the GET /index/_search line, with or without the leading slash, and the index becomes the table name. Comment lines starting with # or // are skipped, several requests in one paste each get their own statement, and a _count request becomes SELECT COUNT(*). If you paste only the JSON body, the table is called table_name.
How do should and must_not end up in SQL?
should on its own means at least one clause has to match, so it becomes an OR group. Next to must or filter, should only changes the score in Elasticsearch, so it stays out of the WHERE clause unless you set minimum_should_match; a value like 2 or 75% is written as a count of CASE WHEN checks. must_not keeps rows where the field is empty, the same way Elasticsearch keeps documents that lack the field, so it is written as (status IS NULL OR NOT (status = 'inactive')).
Why does match become LIKE instead of =?
match is a full-text search on analysed text, so an equals sign would almost never find anything. Each word becomes LOWER(column) LIKE '%word%', joined with OR (or AND when the query sets operator to and), and match_phrase looks for the whole phrase. Any % or _ in your search text is escaped with ESCAPE '!' so it matches literally. It is close, not identical: there is no stemming or relevance scoring.
How are aggregations turned into SQL?
Each terms aggregation gets its own statement with GROUP BY on the field, COUNT(*) AS doc_count, ORDER BY doc_count DESC and a LIMIT equal to the aggregation size (10 by default). avg, sum, min, max, value_count, cardinality and stats become AVG, SUM, MIN, MAX, COUNT, COUNT(DISTINCT) and the five stats columns. Metric aggregations at the top level share one SELECT over the filtered rows. date_histogram, range and deeper bucket aggregations are listed in a note instead.
Do long IDs and quoted values come through unchanged?
Numbers are written exactly as they appear in your query, so 8901240544102066246 keeps every digit and 14.50 keeps its trailing zero, while a quoted "00501" stays the text '00501'. Quotes inside values are doubled (O'Brien becomes 'O''Brien'), unusual column and table names go in double quotes, and size and from must be whole numbers, so nothing in the query can sneak a second statement into the SQL. The output uses standard string quoting; if a value holds a backslash, the note explains what to switch on in MySQL.
Does my query leave the browser?
No. The conversion runs in this page's JavaScript. Nothing you paste or upload is sent anywhere or kept, and a reload starts with an empty editor.
Going the other way? Try SQL to Elasticsearch, or compare with MongoDB to SQL and DynamoDB to SQL.
Related Tools
SQL to MongoDB
Convert SQL queries to MongoDB query syntax with aggregation pipeline support
MongoDB to SQL
Convert MongoDB queries and aggregation pipelines to equivalent SQL statements
SQL to DynamoDB
Convert SQL queries to AWS DynamoDB query and scan operations with PartiQL support
DynamoDB to SQL
Convert AWS DynamoDB query and scan operations to equivalent SQL statements
MongoDB Query Formatter
Format, beautify, and validate MongoDB queries with proper indentation and syntax highlighting
MongoDB to DynamoDB
Convert MongoDB queries to AWS DynamoDB query operations and vice versa