Endpoint

Introduced 1.0

To send query request to SQL plugin, you can either use a request parameter in HTTP GET or request body by HTTP POST request. POST request is recommended because it doesn’t have length limitation and allows for other parameters passed to plugin for other functionality such as prepared statement. And also the explain endpoint is used very often for query translation and troubleshooting.

GET

Description

You can send HTTP GET request with your query embedded in URL parameter.

Example

SQL query:

  1. >> curl -H 'Content-Type: application/json' -X GET localhost:9200/_plugins/_sql?sql=SELECT * FROM accounts

POST

Description

You can also send HTTP POST request with your query in request body.

Example

SQL query:

  1. >> curl -H 'Content-Type: application/json' -X POST localhost:9200/_plugins/_sql -d '{
  2. "query" : "SELECT * FROM accounts"
  3. }'

Explain

Description

To translate your query, send it to explain endpoint. The explain output is OpenSearch domain specific language (DSL) in JSON format. You can just copy and paste it to your console to run it against OpenSearch directly.

Example

Explain query:

  1. >> curl -H 'Content-Type: application/json' -X POST localhost:9200/_plugins/_sql/_explain -d '{
  2. "query" : "SELECT firstname, lastname FROM accounts WHERE age > 20"
  3. }'

Explain:

  1. {
  2. "from": 0,
  3. "size": 200,
  4. "query": {
  5. "bool": {
  6. "filter": [{
  7. "bool": {
  8. "must": [{
  9. "range": {
  10. "age": {
  11. "from": 20,
  12. "to": null,
  13. "include_lower": false,
  14. "include_upper": true,
  15. "boost": 1.0
  16. }
  17. }
  18. }],
  19. "adjust_pure_negative": true,
  20. "boost": 1.0
  21. }
  22. }],
  23. "adjust_pure_negative": true,
  24. "boost": 1.0
  25. }
  26. },
  27. "_source": {
  28. "includes": [
  29. "firstname",
  30. "lastname"
  31. ],
  32. "excludes": []
  33. }
  34. }

Cursor

Description

To get back a paginated response, use the fetch_size parameter. The value of fetch_size should be greater than 0. The default value is 1,000. A value of 0 will fallback to a non-paginated response.

The fetch_size parameter is only supported for the JDBC response format.

Example

SQL query:

  1. >> curl -H 'Content-Type: application/json' -X POST localhost:9200/_plugins/_sql -d '{
  2. "fetch_size" : 5,
  3. "query" : "SELECT firstname, lastname FROM accounts WHERE age > 20 ORDER BY state ASC"
  4. }'

Result set:

  1. {
  2. "schema": [
  3. {
  4. "name": "firstname",
  5. "type": "text"
  6. },
  7. {
  8. "name": "lastname",
  9. "type": "text"
  10. }
  11. ],
  12. "cursor": "d:eyJhIjp7fSwicyI6IkRYRjFaWEo1UVc1a1JtVjBZMmdCQUFBQUFBQUFBQU1XZWpkdFRFRkZUMlpTZEZkeFdsWnJkRlZoYnpaeVVRPT0iLCJjIjpbeyJuYW1lIjoiZmlyc3RuYW1lIiwidHlwZSI6InRleHQifSx7Im5hbWUiOiJsYXN0bmFtZSIsInR5cGUiOiJ0ZXh0In1dLCJmIjo1LCJpIjoiYWNjb3VudHMiLCJsIjo5NTF9",
  13. "total": 956,
  14. "datarows": [
  15. [
  16. "Cherry",
  17. "Carey"
  18. ],
  19. [
  20. "Lindsey",
  21. "Hawkins"
  22. ],
  23. [
  24. "Sargent",
  25. "Powers"
  26. ],
  27. [
  28. "Campos",
  29. "Olsen"
  30. ],
  31. [
  32. "Savannah",
  33. "Kirby"
  34. ]
  35. ],
  36. "size": 5,
  37. "status": 200
  38. }

To fetch subsequent pages, use the cursor from last response:

  1. >> curl -H 'Content-Type: application/json' -X POST localhost:9200/_plugins/_sql -d '{
  2. "cursor": "d:eyJhIjp7fSwicyI6IkRYRjFaWEo1UVc1a1JtVjBZMmdCQUFBQUFBQUFBQU1XZWpkdFRFRkZUMlpTZEZkeFdsWnJkRlZoYnpaeVVRPT0iLCJjIjpbeyJuYW1lIjoiZmlyc3RuYW1lIiwidHlwZSI6InRleHQifSx7Im5hbWUiOiJsYXN0bmFtZSIsInR5cGUiOiJ0ZXh0In1dLCJmIjo1LCJpIjoiYWNjb3VudHMiLCJsIjo5NTF9"
  3. }'

The result only has the fetch_size number of datarows and cursor. The last page has only datarows and no cursor. The datarows can have more than the fetch_size number of records in case the nested fields are flattened.

  1. {
  2. "cursor": "d:eyJhIjp7fSwicyI6IkRYRjFaWEo1UVc1a1JtVjBZMmdCQUFBQUFBQUFBQU1XZWpkdFRFRkZUMlpTZEZkeFdsWnJkRlZoYnpaeVVRPT0iLCJjIjpbeyJuYW1lIjoiZmlyc3RuYW1lIiwidHlwZSI6InRleHQifSx7Im5hbWUiOiJsYXN0bmFtZSIsInR5cGUiOiJ0ZXh0In1dLCJmIjo1LCJpIjoiYWNjb3VudHMabcde12345",
  3. "datarows": [
  4. [
  5. "Abbey",
  6. "Karen"
  7. ],
  8. [
  9. "Chen",
  10. "Ken"
  11. ],
  12. [
  13. "Ani",
  14. "Jade"
  15. ],
  16. [
  17. "Peng",
  18. "Hu"
  19. ],
  20. [
  21. "John",
  22. "Doe"
  23. ]
  24. ]
  25. }

The cursor context is automatically cleared on the last page. To explicitly clear cursor context, use the _plugins/_sql/close endpoint operation.

  1. >> curl -H 'Content-Type: application/json' -X POST localhost:9200/_plugins/_sql/close -d '{
  2. "cursor": "d:eyJhIjp7fSwicyI6IkRYRjFaWEo1UVc1a1JtVjBZMmdCQUFBQUFBQUFBQU1XZWpkdFRFRkZUMlpTZEZkeFdsWnJkRlZoYnpaeVVRPT0iLCJjIjpbeyJuYW1lIjoiZmlyc3RuYW1lIiwidHlwZSI6InRleHQifSx7Im5hbWUiOiJsYXN0bmFtZSIsInR5cGUiOiJ0ZXh0In1dLCJmIjo1LCJpIjoiYWNjb3VudHMiLCJsIjo5NTF9"
  3. }'

Sample response

  1. {"succeeded":true}