getting-started.asciidoc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. [role="xpack"]
  2. [[sql-getting-started]]
  3. == Getting Started with SQL
  4. To start using {es-sql}, create
  5. an index with some data to experiment with:
  6. [source,console]
  7. --------------------------------------------------
  8. PUT /library/_bulk?refresh
  9. {"index":{"_id": "Leviathan Wakes"}}
  10. {"name": "Leviathan Wakes", "author": "James S.A. Corey", "release_date": "2011-06-02", "page_count": 561}
  11. {"index":{"_id": "Hyperion"}}
  12. {"name": "Hyperion", "author": "Dan Simmons", "release_date": "1989-05-26", "page_count": 482}
  13. {"index":{"_id": "Dune"}}
  14. {"name": "Dune", "author": "Frank Herbert", "release_date": "1965-06-01", "page_count": 604}
  15. --------------------------------------------------
  16. And now you can execute SQL using the <<sql-search-api,SQL search API>>:
  17. [source,console]
  18. --------------------------------------------------
  19. POST /_sql?format=txt
  20. {
  21. "query": "SELECT * FROM library WHERE release_date < '2000-01-01'"
  22. }
  23. --------------------------------------------------
  24. // TEST[continued]
  25. Which should return something along the lines of:
  26. [source,text]
  27. --------------------------------------------------
  28. author | name | page_count | release_date
  29. ---------------+---------------+---------------+------------------------
  30. Dan Simmons |Hyperion |482 |1989-05-26T00:00:00.000Z
  31. Frank Herbert |Dune |604 |1965-06-01T00:00:00.000Z
  32. --------------------------------------------------
  33. // TESTRESPONSE[s/\|/\\|/ s/\+/\\+/]
  34. // TESTRESPONSE[non_json]
  35. You can also use the <<sql-cli>>. There is a script to start it
  36. shipped in x-pack's bin directory:
  37. [source,bash]
  38. --------------------------------------------------
  39. $ ./bin/elasticsearch-sql-cli
  40. --------------------------------------------------
  41. From there you can run the same query:
  42. [source,sqlcli]
  43. --------------------------------------------------
  44. sql> SELECT * FROM library WHERE release_date < '2000-01-01';
  45. author | name | page_count | release_date
  46. ---------------+---------------+---------------+------------------------
  47. Dan Simmons |Hyperion |482 |1989-05-26T00:00:00.000Z
  48. Frank Herbert |Dune |604 |1965-06-01T00:00:00.000Z
  49. --------------------------------------------------