rest.asciidoc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[sql-rest]]
  4. == SQL REST API
  5. The SQL REST API accepts SQL in a JSON document, executes it,
  6. and returns the results. For example:
  7. [source,js]
  8. --------------------------------------------------
  9. POST /_xpack/sql?format=txt
  10. {
  11. "query": "SELECT * FROM library ORDER BY page_count DESC LIMIT 5"
  12. }
  13. --------------------------------------------------
  14. // CONSOLE
  15. // TEST[setup:library]
  16. Which returns:
  17. [source,text]
  18. --------------------------------------------------
  19. author | name | page_count | release_date
  20. -----------------+--------------------+---------------+------------------------
  21. Peter F. Hamilton|Pandora's Star |768 |2004-03-02T00:00:00.000Z
  22. Vernor Vinge |A Fire Upon the Deep|613 |1992-06-01T00:00:00.000Z
  23. Frank Herbert |Dune |604 |1965-06-01T00:00:00.000Z
  24. Alastair Reynolds|Revelation Space |585 |2000-03-15T00:00:00.000Z
  25. James S.A. Corey |Leviathan Wakes |561 |2011-06-02T00:00:00.000Z
  26. --------------------------------------------------
  27. // TESTRESPONSE[s/\|/\\|/ s/\+/\\+/]
  28. // TESTRESPONSE[_cat]
  29. While the `text/plain` format is nice for humans, computers prefer something
  30. more structured. You can replace the value of `format` with:
  31. - `json` aka `application/json`
  32. - `yaml` aka `application/yaml`
  33. - `smile` aka `application/smile`
  34. - `cbor` aka `application/cbor`
  35. - `txt` aka `text/plain`
  36. - `csv` aka `text/csv`
  37. - `tsv` aka `text/tab-separated-values`
  38. Alternatively you can set the `Accept` HTTP header to the appropriate media
  39. format. The GET parameter takes precedence over the header. If neither is
  40. specified then the response is returned in the same format as the request.
  41. [source,js]
  42. --------------------------------------------------
  43. POST /_xpack/sql?format=json
  44. {
  45. "query": "SELECT * FROM library ORDER BY page_count DESC",
  46. "fetch_size": 5
  47. }
  48. --------------------------------------------------
  49. // CONSOLE
  50. // TEST[setup:library]
  51. Which returns:
  52. [source,js]
  53. --------------------------------------------------
  54. {
  55. "columns": [
  56. {"name": "author", "type": "text"},
  57. {"name": "name", "type": "text"},
  58. {"name": "page_count", "type": "short"},
  59. {"name": "release_date", "type": "date"}
  60. ],
  61. "rows": [
  62. ["Peter F. Hamilton", "Pandora's Star", 768, "2004-03-02T00:00:00.000Z"],
  63. ["Vernor Vinge", "A Fire Upon the Deep", 613, "1992-06-01T00:00:00.000Z"],
  64. ["Frank Herbert", "Dune", 604, "1965-06-01T00:00:00.000Z"],
  65. ["Alastair Reynolds", "Revelation Space", 585, "2000-03-15T00:00:00.000Z"],
  66. ["James S.A. Corey", "Leviathan Wakes", 561, "2011-06-02T00:00:00.000Z"]
  67. ],
  68. "cursor": "sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWWWdrRlVfSS1TbDYtcW9lc1FJNmlYdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl+v///w8="
  69. }
  70. --------------------------------------------------
  71. // TESTRESPONSE[s/sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWWWdrRlVfSS1TbDYtcW9lc1FJNmlYdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl\+v\/\/\/w8=/$body.cursor/]
  72. You can continue to the next page by sending back the `cursor` field. In
  73. case of text format the cursor is returned as `Cursor` http header.
  74. [source,js]
  75. --------------------------------------------------
  76. POST /_xpack/sql?format=json
  77. {
  78. "cursor": "sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWYUpOYklQMHhRUEtld3RsNnFtYU1hQQ==:BAFmBGRhdGUBZgVsaWtlcwFzB21lc3NhZ2UBZgR1c2Vy9f///w8="
  79. }
  80. --------------------------------------------------
  81. // CONSOLE
  82. // TEST[continued]
  83. // TEST[s/sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWYUpOYklQMHhRUEtld3RsNnFtYU1hQQ==:BAFmBGRhdGUBZgVsaWtlcwFzB21lc3NhZ2UBZgR1c2Vy9f\/\/\/w8=/$body.cursor/]
  84. Which looks like:
  85. [source,js]
  86. --------------------------------------------------
  87. {
  88. "rows" : [
  89. ["Dan Simmons", "Hyperion", 482, "1989-05-26T00:00:00.000Z"],
  90. ["Iain M. Banks", "Consider Phlebas", 471, "1987-04-23T00:00:00.000Z"],
  91. ["Neal Stephenson", "Snow Crash", 470, "1992-06-01T00:00:00.000Z"],
  92. ["Frank Herbert", "God Emperor of Dune", 454, "1981-05-28T00:00:00.000Z"],
  93. ["Frank Herbert", "Children of Dune", 408, "1976-04-21T00:00:00.000Z"]
  94. ],
  95. "cursor" : "sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWODRMaXBUaVlRN21iTlRyWHZWYUdrdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl9f///w8="
  96. }
  97. --------------------------------------------------
  98. // TESTRESPONSE[s/sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWODRMaXBUaVlRN21iTlRyWHZWYUdrdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl9f\/\/\/w8=/$body.cursor/]
  99. Note that the `column` object is only part of the first page.
  100. You've reached the last page when there is no `cursor` returned
  101. in the results. Like Elasticsearch's <<search-request-scroll,scroll>>,
  102. SQL may keep state in Elasticsearch to support the cursor. Unlike
  103. scroll, receiving the last page is enough to guarantee that the
  104. Elasticsearch state is cleared.
  105. To clear the state earlier, you can use the clear cursor command:
  106. [source,js]
  107. --------------------------------------------------
  108. POST /_xpack/sql/close
  109. {
  110. "cursor": "sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWYUpOYklQMHhRUEtld3RsNnFtYU1hQQ==:BAFmBGRhdGUBZgVsaWtlcwFzB21lc3NhZ2UBZgR1c2Vy9f///w8="
  111. }
  112. --------------------------------------------------
  113. // CONSOLE
  114. // TEST[continued]
  115. // TEST[s/sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWYUpOYklQMHhRUEtld3RsNnFtYU1hQQ==:BAFmBGRhdGUBZgVsaWtlcwFzB21lc3NhZ2UBZgR1c2Vy9f\/\/\/w8=/$body.cursor/]
  116. Which will like return the
  117. [source,js]
  118. --------------------------------------------------
  119. {
  120. "succeeded" : true
  121. }
  122. --------------------------------------------------
  123. // TESTRESPONSE
  124. [[sql-rest-filtering]]
  125. You can filter the results that SQL will run on using a standard
  126. Elasticsearch query DSL by specifying the query in the filter
  127. parameter.
  128. [source,js]
  129. --------------------------------------------------
  130. POST /_xpack/sql?format=txt
  131. {
  132. "query": "SELECT * FROM library ORDER BY page_count DESC",
  133. "filter": {
  134. "range": {
  135. "page_count": {
  136. "gte" : 100,
  137. "lte" : 200
  138. }
  139. }
  140. },
  141. "fetch_size": 5
  142. }
  143. --------------------------------------------------
  144. // CONSOLE
  145. // TEST[setup:library]
  146. Which returns:
  147. [source,text]
  148. --------------------------------------------------
  149. author | name | page_count | release_date
  150. ---------------+------------------------------------+---------------+------------------------
  151. Douglas Adams |The Hitchhiker's Guide to the Galaxy|180 |1979-10-12T00:00:00.000Z
  152. --------------------------------------------------
  153. // TESTRESPONSE[s/\|/\\|/ s/\+/\\+/]
  154. // TESTRESPONSE[_cat]
  155. [[sql-rest-fields]]
  156. In addition to the `query` and `cursor` fields, the request can
  157. contain `fetch_size` and `time_zone`. `fetch_size` is a hint for how
  158. many results to return in each page. SQL might chose to return more
  159. or fewer results though. `time_zone` is the time zone to use for date
  160. functions and date parsing. `time_zone` defaults to `utc` and can take
  161. any values documented
  162. http://www.joda.org/joda-time/apidocs/org/joda/time/DateTimeZone.html[here].