2018-06-22 18:40:25 -04:00
|
|
|
[role="xpack"]
|
|
|
|
[testenv="basic"]
|
2017-12-13 10:19:31 -05:00
|
|
|
[[sql-translate]]
|
|
|
|
== SQL Translate API
|
|
|
|
|
|
|
|
The SQL Translate API accepts SQL in a JSON document and translates it
|
|
|
|
into native Elasticsearch queries. For example:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2018-11-27 22:16:21 -05:00
|
|
|
POST /_sql/translate
|
2017-12-13 10:19:31 -05:00
|
|
|
{
|
|
|
|
"query": "SELECT * FROM library ORDER BY page_count DESC",
|
|
|
|
"fetch_size": 10
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
// CONSOLE
|
|
|
|
// TEST[setup:library]
|
|
|
|
|
|
|
|
Which returns:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"size" : 10,
|
|
|
|
"docvalue_fields" : [
|
2018-05-23 08:39:04 -04:00
|
|
|
{
|
2019-01-30 04:31:51 -05:00
|
|
|
"field": "page_count"
|
2018-05-23 08:39:04 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"field": "release_date",
|
|
|
|
"format": "epoch_millis"
|
|
|
|
}
|
2017-12-13 10:19:31 -05:00
|
|
|
],
|
2017-12-18 20:57:50 -05:00
|
|
|
"_source": {
|
|
|
|
"includes": [
|
|
|
|
"author",
|
|
|
|
"name"
|
|
|
|
],
|
|
|
|
"excludes": []
|
|
|
|
},
|
2017-12-13 10:19:31 -05:00
|
|
|
"sort" : [
|
|
|
|
{
|
|
|
|
"page_count" : {
|
2018-10-19 09:44:33 -04:00
|
|
|
"order" : "desc",
|
|
|
|
"missing" : "_first",
|
|
|
|
"unmapped_type" : "short"
|
2017-12-13 10:19:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
// TESTRESPONSE
|
|
|
|
|
|
|
|
Which is the request that SQL will run to provide the results.
|
|
|
|
In this case, SQL will use the <<search-request-scroll,scroll>>
|
|
|
|
API. If the result contained an aggregation then SQL would use
|
|
|
|
the normal <<search-request-body,search>> API.
|
|
|
|
|
|
|
|
The request body accepts all of the <<sql-rest-fields,fields>> that
|
|
|
|
the <<sql-rest,SQL REST API>> accepts except `cursor`.
|