Protocol

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

    Example 1

    Use to add more conditions to OpenSearch DSL directly.

    SQL query:

    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. "size": 200,
    3. "query": {
    4. "bool": {
    5. "filter": [{
    6. "bool": {
    7. "must": [{
    8. "term": {
    9. "age": {
    10. "value": 30,
    11. "boost": 1.0
    12. }
    13. }],
    14. "adjust_pure_negative": true,
    15. "boost": 1.0
    16. }
    17. }],
    18. "adjust_pure_negative": true,
    19. "boost": 1.0
    20. }
    21. }
    22. }

    Description

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

    SQL query:

    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. "status": 400
    7. }

    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

    Result set:

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

    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

    Description

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

    Example

    SQL query:

    Result set:

    1. Nanette|Bates|28
    2. Amber|Duke|32
    3. Hattie|Bond|36