Elasticsearch 简明教程

Elasticsearch - SQL Access

它是一个组件,允许在实时中针对 Elasticsearch 执行类似于 SQL 的查询。你可以将 Elasticsearch SQL 视为一个翻译器,它既了解 SQL 也了解 Elasticsearch,并且可以轻松地按比例利用 Elasticsearch 的功能,实时读取和处理数据。

Advantages of Elasticsearch SQL

  1. It has native integration − 根据底层存储,每个查询根据相关节点有效执行。

  2. No external parts − 不需要额外的硬件、流程、运行时或库来查询 Elasticsearch。

  3. Lightweight and efficient − 它引入并公开 SQL 来允许适当的全文本搜索,并支持实时功能。

Example

PUT /schoollist/_bulk?refresh
   {"index":{"_id": "CBSE"}}
   {"name": "GleanDale", "Address": "JR. Court Lane", "start_date": "2011-06-02",
   "student_count": 561}
   {"index":{"_id": "ICSE"}}
   {"name": "Top-Notch", "Address": "Gachibowli Main Road", "start_date": "1989-
   05-26", "student_count": 482}
   {"index":{"_id": "State Board"}}
   {"name": "Sunshine", "Address": "Main Street", "start_date": "1965-06-01",
   "student_count": 604}

在运行以上代码时,我们得到响应,如下所示:-

{
   "took" : 277,
   "errors" : false,
   "items" : [
      {
         "index" : {
            "_index" : "schoollist",
            "_type" : "_doc",
            "_id" : "CBSE",
            "_version" : 1,
            "result" : "created",
            "forced_refresh" : true,
            "_shards" : {
               "total" : 2,
               "successful" : 1,
               "failed" : 0
            },
            "_seq_no" : 0,
            "_primary_term" : 1,
            "status" : 201
         }
      },
      {
         "index" : {
            "_index" : "schoollist",
            "_type" : "_doc",
            "_id" : "ICSE",
            "_version" : 1,
            "result" : "created",
            "forced_refresh" : true,
            "_shards" : {
               "total" : 2,
               "successful" : 1,
               "failed" : 0
            },
            "_seq_no" : 1,
            "_primary_term" : 1,
            "status" : 201
         }
      },
      {
         "index" : {
            "_index" : "schoollist",
            "_type" : "_doc",
            "_id" : "State Board",
            "_version" : 1,
            "result" : "created",
            "forced_refresh" : true,
            "_shards" : {
               "total" : 2,
               "successful" : 1,
               "failed" : 0
            },
            "_seq_no" : 2,
            "_primary_term" : 1,
            "status" : 201
         }
      }
   ]
}

SQL Query

以下示例显示了构造 SQL 查询的方式 −

POST /_sql?format=txt
{
   "query": "SELECT * FROM schoollist WHERE start_date < '2000-01-01'"
}

在运行以上代码时,我们得到响应,如下所示:-

Address             | name          | start_date             | student_count
--------------------+---------------+------------------------+---------------
Gachibowli Main Road|Top-Notch      |1989-05-26T00:00:00.000Z|482
Main Street         |Sunshine       |1965-06-01T00:00:00.000Z|604

Note − 通过更改上述 SQL 查询,你可以获得不同的结果集。