Protocol

For the protocol, SQL plugin provides multiple response formats for different purposes while the request format is same for all. Among them JDBC format is widely used because it provides schema information and more functionality such as pagination. Besides JDBC driver, various clients can benefit from the detailed and well formatted response.

Request Format

Description

The body of HTTP POST request can take a few more other fields with SQL query.

Example 1

Use filter to add more conditions to OpenSearch DSL directly.

SQL query:

  1. >> curl -H 'Content-Type: application/json' -X POST localhost:9200/_plugins/_sql -d '{
  2. "query" : "SELECT firstname, lastname, balance FROM accounts",
  3. "filter" : {
  4. "range" : {
  5. "balance" : {
  6. "lt" : 10000
  7. }
  8. }
  9. }
  10. }'

Explain:

  1. {
  2. "from": 0,
  3. "size": 200,
  4. "query": {
  5. "bool": {
  6. "filter": [{
  7. "bool": {
  8. "filter": [{
  9. "range": {
  10. "balance": {
  11. "from": null,
  12. "to": 10000,
  13. "include_lower": true,
  14. "include_upper": false,
  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. "balance"
  32. ],
  33. "excludes": []
  34. }
  35. }

Example 2

Use parameters for actual parameter value in prepared SQL query.

SQL query:

  1. >> curl -H 'Content-Type: application/json' -X POST localhost:9200/_plugins/_sql -d '{
  2. "query": "SELECT * FROM accounts WHERE age = ?",
  3. "parameters": [{
  4. "type": "integer",
  5. "value": 30
  6. }]
  7. }'

Explain:

  1. {
  2. "from": 0,
  3. "size": 200,
  4. "query": {
  5. "bool": {
  6. "filter": [{
  7. "bool": {
  8. "must": [{
  9. "term": {
  10. "age": {
  11. "value": 30,
  12. "boost": 1.0
  13. }
  14. }
  15. }],
  16. "adjust_pure_negative": true,
  17. "boost": 1.0
  18. }
  19. }],
  20. "adjust_pure_negative": true,
  21. "boost": 1.0
  22. }
  23. }
  24. }

JDBC Format

Description

By default, the plugin returns the JDBC standard format. This format is provided for JDBC driver and clients that need both schema and result set well formatted.

Example 1

Here is an example for normal response. The schema includes field name and its type and datarows includes the result set.

SQL query:

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

Result set:

  1. {
  2. "schema": [{
  3. "name": "firstname",
  4. "type": "text"
  5. },
  6. {
  7. "name": "lastname",
  8. "type": "text"
  9. },
  10. {
  11. "name": "age",
  12. "type": "long"
  13. }
  14. ],
  15. "total": 4,
  16. "datarows": [
  17. [
  18. "Nanette",
  19. "Bates",
  20. 28
  21. ],
  22. [
  23. "Amber",
  24. "Duke",
  25. 32
  26. ]
  27. ],
  28. "size": 2,
  29. "status": 200
  30. }

Example 2

If any error occurred, error message and the cause will be returned instead.

SQL query:

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

Result set:

  1. {
  2. "error": {
  3. "reason": "Invalid SQL query",
  4. "details": "Field [unknown] cannot be found or used here.",
  5. "type": "SemanticAnalysisException"
  6. },
  7. "status": 400
  8. }

OpenSearch DSL

Description

The json format returns original response from OpenSearch in JSON. Because this is the native response from OpenSearch, extra efforts are needed to parse and interpret it.

Example

SQL query:

  1. >> curl -H 'Content-Type: application/json' -X POST localhost:9200/_plugins/_sql?format=json -d '{
  2. "query" : "SELECT firstname, lastname, age FROM accounts ORDER BY age LIMIT 2"
  3. }'

Result set:

  1. {
  2. "_shards": {
  3. "total": 5,
  4. "failed": 0,
  5. "successful": 5,
  6. "skipped": 0
  7. },
  8. "hits": {
  9. "hits": [{
  10. "_index": "accounts",
  11. "_type": "account",
  12. "_source": {
  13. "firstname": "Nanette",
  14. "age": 28,
  15. "lastname": "Bates"
  16. },
  17. "_id": "13",
  18. "sort": [
  19. 28
  20. ],
  21. "_score": null
  22. },
  23. {
  24. "_index": "accounts",
  25. "_type": "account",
  26. "_source": {
  27. "firstname": "Amber",
  28. "age": 32,
  29. "lastname": "Duke"
  30. },
  31. "_id": "1",
  32. "sort": [
  33. 32
  34. ],
  35. "_score": null
  36. }
  37. ],
  38. "total": {
  39. "value": 4,
  40. "relation": "eq"
  41. },
  42. "max_score": null
  43. },
  44. "took": 100,
  45. "timed_out": false
  46. }

CSV Format

Description

You can also use CSV format to download result set as CSV.

Example

SQL query:

  1. >> curl -H 'Content-Type: application/json' -X POST localhost:9200/_plugins/_sql?format=csv -d '{
  2. "query" : "SELECT firstname, lastname, age FROM accounts ORDER BY age"
  3. }'

Result set:

  1. firstname,lastname,age
  2. Nanette,Bates,28
  3. Amber,Duke,32
  4. Dale,Adams,33
  5. Hattie,Bond,36

Raw Format

Description

Additionally raw format can be used to pipe the result to other command line tool for post processing.

Example

SQL query:

  1. >> curl -H 'Content-Type: application/json' -X POST localhost:9200/_plugins/_sql?format=raw -d '{
  2. "query" : "SELECT firstname, lastname, age FROM accounts ORDER BY age"
  3. }'

Result set:

  1. Nanette|Bates|28
  2. Amber|Duke|32
  3. Dale|Adams|33
  4. Hattie|Bond|36