English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Elasticsearch SQL Access

It is a component that allows for the real-time execution of similar sql in Elasticsearch Query. You can treat Elasticsearch SQL as a translator that understands both SQL and Elasticsearch and performs Elasticsearch features that can easily read and process data in real-time.

Advantages of Elasticsearch SQL

  • It has local integration −Efficiently executes each query on the relevant nodes based on the underlying storage.

  • No external components −No additional hardware, process, runtime, or library is required to query Elasticsearch.

  • Lightweight and efficient −It includes and exposes SQL for real-time appropriate full-text search.

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}

After running the above code, we get the following response:

{
   "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

The following examples demonstrate how to construct SQL queries-

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

After running the above code, we get the following response:

Address|name|start_date|student_count
--------------------+---------------+------------------------+---------------
Gachibowli Main Road|Top-Notch1989-05-26T00:00:00.000Z|482
Main Street|Sunshine1965-06-01T00:00:00.000Z|604

Note By changing the SQL query above, you can obtain different result sets.