rest.asciidoc 7.1 KB

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