|
| 1 | +[role="xpack"] |
| 2 | +[testenv="basic"] |
| 3 | +[[sql-search-api]] |
| 4 | +=== SQL search API |
| 5 | +++++ |
| 6 | +<titleabbrev>SQL search</titleabbrev> |
| 7 | +++++ |
| 8 | + |
| 9 | +Returns results for an <<sql-rest-overview,SQL search>>. |
| 10 | + |
| 11 | +[source,console] |
| 12 | +---- |
| 13 | +POST _sql?format=txt |
| 14 | +{ |
| 15 | + "query": "SELECT * FROM library ORDER BY page_count DESC LIMIT 5" |
| 16 | +} |
| 17 | +---- |
| 18 | +// TEST[setup:library] |
| 19 | + |
| 20 | +[[sql-search-api-request]] |
| 21 | +==== {api-request-title} |
| 22 | + |
| 23 | +`GET _sql` |
| 24 | + |
| 25 | +`POST _sql` |
| 26 | + |
| 27 | +[[sql-search-api-prereqs]] |
| 28 | +==== {api-prereq-title} |
| 29 | + |
| 30 | +* If the {es} {security-features} are enabled, you must have the `read` |
| 31 | +<<privileges-list-indices,index privilege>> for the data stream, index, |
| 32 | +or alias you search. |
| 33 | + |
| 34 | +[[sql-search-api-limitations]] |
| 35 | +===== Limitations |
| 36 | + |
| 37 | +See <<sql-limitations>>. |
| 38 | + |
| 39 | +[[search-api-query-params]] |
| 40 | +==== {api-query-parms-title} |
| 41 | + |
| 42 | +`delimiter`:: |
| 43 | +(Optional, string) Separator for CSV results. Defaults to `,`. The API only |
| 44 | +supports this parameter for CSV responses. |
| 45 | + |
| 46 | +`format`:: |
| 47 | +(Optional, string) Format for the response. For valid values, see |
| 48 | +<<sql-rest-format>>. |
| 49 | ++ |
| 50 | +You can also specify a format using the `Accept` HTTP header. If you specify |
| 51 | +both this parameter and the `Accept` HTTP header, this parameter takes |
| 52 | +precedence. |
| 53 | + |
| 54 | +[role="child_attributes"] |
| 55 | +[[sql-search-api-request-body]] |
| 56 | +==== {api-request-body-title} |
| 57 | + |
| 58 | +`columnar`:: |
| 59 | +(Optional, Boolean) If `true`, returns results in a columnar format. Defaults to |
| 60 | +`false`. The API only supports this parameter for CBOR, JSON, SMILE, and YAML |
| 61 | +responses. See <<sql-rest-columnar>>. |
| 62 | + |
| 63 | +`cursor`:: |
| 64 | +(Optional, string) <<sql-pagination,Cursor>> used to retrieve a set of paginated |
| 65 | +results. If you specify a `cursor`, the API only uses the `columnar` and |
| 66 | +`time_zone` request body parameters. It ignores other request body parameters. |
| 67 | + |
| 68 | +[[sql-search-api-fetch-size]] |
| 69 | +`fetch_size`:: |
| 70 | +(Optional, integer) Maximum number of rows to return in the response. Defaults |
| 71 | +to `1000`. |
| 72 | + |
| 73 | +[[sql-search-api-field-multi-value-leniency]] |
| 74 | +`field_multi_value_leniency`:: |
| 75 | +(Optional, Boolean) If `false`, the API returns an error for fields containing |
| 76 | +<<array,array values>>. If `true`, the API returns the first value from the |
| 77 | +array with no guarantee of consistent results. Defaults to `false`. |
| 78 | + |
| 79 | +`filter`:: |
| 80 | +(Optional, object) <<query-dsl,Query DSL>> used to filter documents for the SQL |
| 81 | +search. See <<sql-rest-filtering>>. |
| 82 | + |
| 83 | +`index_include_frozen`:: |
| 84 | +(Optional, Boolean) If `true`, the search can run on frozen indices. Defaults to |
| 85 | +`false`. |
| 86 | + |
| 87 | +`keep_alive`:: |
| 88 | +(Optional, <<time-units,time value>>) Retention period for an |
| 89 | +<<sql-async,async>> or <<sql-store-searches,saved synchronous search>>. Defaults |
| 90 | +to `5d` (five days). |
| 91 | + |
| 92 | +`keep_on_completion`:: |
| 93 | +(Optional, Boolean) If `true`, {es} <<sql-store-searches,stores synchronous |
| 94 | +searches>> if you also specify the `wait_for_completion_timeout` parameter. If |
| 95 | +`false`, {es} only stores <<sql-async,async searches>> that don't finish before |
| 96 | +the `wait_for_completion_timeout`. Defaults to `false`. |
| 97 | + |
| 98 | +`page_timeout`:: |
| 99 | +(Optional, <<time-units,time value>>) Timeout before a |
| 100 | +<<sql-pagination,pagination request>> fails. Defaults to `45s` (45 seconds). |
| 101 | + |
| 102 | +`params`:: |
| 103 | +(Optional, array) Values for parameters in the `query`. For syntax, see |
| 104 | +<<sql-rest-params>>. |
| 105 | + |
| 106 | +`query`:: |
| 107 | +(Required, object) SQL query to run. For syntax, see <<sql-spec>>. |
| 108 | + |
| 109 | +`request_timeout`:: |
| 110 | +(Optional, <<time-units,time value>>) Timeout before the request fails. Defaults |
| 111 | +to `90s` (90 seconds). |
| 112 | + |
| 113 | +include::{es-repo-dir}/search/search.asciidoc[tag=runtime-mappings-def] |
| 114 | + |
| 115 | +[[sql-search-api-time-zone]] |
| 116 | +`time_zone`:: |
| 117 | +(Optional, string) ISO-8601 time zone ID for the search. Several |
| 118 | +<<sql-functions-datetime,SQL date/time functions>> use this time zone. Defaults |
| 119 | +to `Z` (UTC). |
| 120 | + |
| 121 | +`wait_for_completion_timeout`:: |
| 122 | +(Optional, <<time-units,time value>>) Period to wait for complete results. |
| 123 | +Defaults to no timeout, meaning the request waits for complete search results. |
| 124 | +If the search doesn’t finish within this period, the search becomes |
| 125 | +<<sql-async,async>>. |
| 126 | ++ |
| 127 | +To <<sql-store-searches,save a synchronous search>>, you must specify this |
| 128 | +parameter and the `keep_on_completion` parameter. |
| 129 | + |
| 130 | +[role="child_attributes"] |
| 131 | +[[sql-search-api-response-body]] |
| 132 | +==== {api-response-body-title} |
| 133 | + |
| 134 | +The SQL search API supports <<sql-rest-format,multiple response formats>>. Most |
| 135 | +response formats use a tabular layout. JSON responses contain the following |
| 136 | +properties: |
| 137 | + |
| 138 | +`id`:: |
| 139 | +(string) Identifier for the search. This value is only returned for |
| 140 | +<<sql-async,async>> and <<sql-store-searches,saved synchronous searches>>. For |
| 141 | +CSV, TSV, and TXT responses, this value is returned in the `Async-ID` HTTP |
| 142 | +header. |
| 143 | + |
| 144 | +`is_running`:: |
| 145 | +(Boolean) If `true`, the search is still running. If `false`, the search has |
| 146 | +finished. This value is only returned for <<sql-async,async>> and |
| 147 | +<<sql-store-searches,saved synchronous searches>>. For CSV, TSV, and TXT |
| 148 | +responses, this value is returned in the `Async-partial` HTTP header. |
| 149 | + |
| 150 | +`is_partial`:: |
| 151 | +(Boolean) If `true`, the response does not contain complete search results. If |
| 152 | +`is_partial` is `true` and `is_running` is `true`, the search is still running. |
| 153 | +If `is_partial` is `true` but `is_running` is `false`, the results are partial |
| 154 | +due to a failure or timeout. |
| 155 | ++ |
| 156 | +This value is only returned for <<sql-async,async>> and |
| 157 | +<<sql-store-searches,saved synchronous searches>>. For CSV, TSV, and TXT |
| 158 | +responses, this value is returned in the `Async-partial` HTTP header. |
| 159 | + |
| 160 | +`rows`:: |
| 161 | +(array of arrays) |
| 162 | +Values for the search results. |
| 163 | + |
| 164 | +`columns`:: |
| 165 | +(array of objects) |
| 166 | +Column headings for the search results. Each object is a column. |
| 167 | ++ |
| 168 | +.Properties of `columns` objects |
| 169 | +[%collapsible%open] |
| 170 | +==== |
| 171 | +`name`:: |
| 172 | +(string) Name of the column. |
| 173 | +
|
| 174 | +`type`:: |
| 175 | +(string) Data type for the column. |
| 176 | +==== |
| 177 | + |
| 178 | +`cursor`:: |
| 179 | +(string) <<sql-pagination,Cursor>> for the next set of paginated results. For |
| 180 | +CSV, TSV, and TXT responses, this value is returned in the `Cursor` HTTP header. |
0 commit comments