SQL 入门
编辑SQL 入门
编辑要开始使用 Elasticsearch SQL,请创建一个包含一些数据的索引以进行实验。
resp = client.bulk( index="library", refresh=True, operations=[ { "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 } ], ) print(resp)
response = client.bulk( index: 'library', refresh: true, body: [ { 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 } ] ) puts response
const response = await client.bulk({ index: "library", refresh: "true", operations: [ { 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.log(response);
PUT /library/_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}
现在您可以使用SQL 搜索 API执行 SQL。
resp = client.sql.query( format="txt", query="SELECT * FROM library WHERE release_date < '2000-01-01'", ) print(resp)
response = client.sql.query( format: 'txt', body: { query: "SELECT * FROM library WHERE release_date < '2000-01-01'" } ) puts response
const response = await client.sql.query({ format: "txt", query: "SELECT * FROM library WHERE release_date < '2000-01-01'", }); console.log(response);
POST /_sql?format=txt { "query": "SELECT * FROM library WHERE release_date < '2000-01-01'" }
这应该返回类似以下内容的结果:
author | name | page_count | release_date ---------------+---------------+---------------+------------------------ Dan Simmons |Hyperion |482 |1989-05-26T00:00:00.000Z Frank Herbert |Dune |604 |1965-06-01T00:00:00.000Z
您也可以使用SQL CLI。Elasticsearch 的 bin
目录中附带了一个启动脚本。
$ ./bin/elasticsearch-sql-cli
从那里您可以运行相同的查询。
sql> SELECT * FROM library WHERE release_date < '2000-01-01'; author | name | page_count | release_date ---------------+---------------+---------------+------------------------ Dan Simmons |Hyperion |482 |1989-05-26T00:00:00.000Z Frank Herbert |Dune |604 |1965-06-01T00:00:00.000Z