2018-06-22 18:40:25 -04:00
|
|
|
[role="xpack"]
|
|
|
|
[testenv="basic"]
|
2017-12-13 10:19:31 -05:00
|
|
|
[[sql-getting-started]]
|
|
|
|
== Getting Started with SQL
|
|
|
|
|
2018-06-13 06:53:08 -04:00
|
|
|
To start using {es-sql}, create
|
2018-04-04 14:59:31 -04:00
|
|
|
an index with some data to experiment with:
|
2017-12-20 13:58:32 -05:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
PUT /library/book/_bulk?refresh
|
|
|
|
{"index":{"_id": "Leviathan Wakes"}}
|
|
|
|
{"name": "Leviathan Wakes", "author": "James S.A. Corey", "release_date": "2011-06-02", "page_count": 561}
|
|
|
|
{"index":{"_id": "Hyperion"}}
|
|
|
|
{"name": "Hyperion", "author": "Dan Simmons", "release_date": "1989-05-26", "page_count": 482}
|
|
|
|
{"index":{"_id": "Dune"}}
|
|
|
|
{"name": "Dune", "author": "Frank Herbert", "release_date": "1965-06-01", "page_count": 604}
|
|
|
|
--------------------------------------------------
|
|
|
|
// CONSOLE
|
|
|
|
|
|
|
|
And now you can execute SQL using the <<sql-rest>> right away:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2018-11-27 22:16:21 -05:00
|
|
|
POST /_sql?format=txt
|
2017-12-20 13:58:32 -05:00
|
|
|
{
|
|
|
|
"query": "SELECT * FROM library WHERE release_date < '2000-01-01'"
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
// CONSOLE
|
|
|
|
// TEST[continued]
|
|
|
|
|
|
|
|
Which should return something along the lines of:
|
|
|
|
|
|
|
|
[source,text]
|
|
|
|
--------------------------------------------------
|
|
|
|
author | name | page_count | release_date
|
2018-01-08 14:52:27 -05:00
|
|
|
---------------+---------------+---------------+------------------------
|
|
|
|
Dan Simmons |Hyperion |482 |1989-05-26T00:00:00.000Z
|
|
|
|
Frank Herbert |Dune |604 |1965-06-01T00:00:00.000Z
|
2017-12-20 13:58:32 -05:00
|
|
|
--------------------------------------------------
|
|
|
|
// TESTRESPONSE[s/\|/\\|/ s/\+/\\+/]
|
2019-06-10 09:33:32 -04:00
|
|
|
// TESTRESPONSE[non_json]
|
2017-12-20 13:58:32 -05:00
|
|
|
|
|
|
|
You can also use the <<sql-cli>>. There is a script to start it
|
|
|
|
shipped in x-pack's bin directory:
|
|
|
|
|
|
|
|
[source,bash]
|
|
|
|
--------------------------------------------------
|
2018-04-11 11:29:57 -04:00
|
|
|
$ ./bin/elasticsearch-sql-cli
|
2017-12-20 13:58:32 -05:00
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
From there you can run the same query:
|
|
|
|
|
|
|
|
[source,sqlcli]
|
|
|
|
--------------------------------------------------
|
|
|
|
sql> SELECT * FROM library WHERE release_date < '2000-01-01';
|
|
|
|
author | name | page_count | release_date
|
2018-01-08 14:52:27 -05:00
|
|
|
---------------+---------------+---------------+------------------------
|
|
|
|
Dan Simmons |Hyperion |482 |1989-05-26T00:00:00.000Z
|
|
|
|
Frank Herbert |Dune |604 |1965-06-01T00:00:00.000Z
|
2017-12-20 13:58:32 -05:00
|
|
|
--------------------------------------------------
|