English 中文(简体)
Elasticsearch - SQL Access
  • 时间:2024-11-05

Elasticsearch - SQL Access


Previous Page Next Page  

It is a component that allows SQL-pke queries to be executed in real-time against Elasticsearch. You can think of Elasticsearch SQL as a translator, one that understands both SQL and Elasticsearch and makes it easy to read and process data in real-time, at scale by leveraging Elasticsearch capabipties.

Advantages of Elasticsearch SQL

    It has native integration − Each and every query is efficiently executed against the relevant nodes according to the underlying storage.

    No external parts − No need for additional hardware, processes, runtimes or pbraries to query Elasticsearch.

    Lightweight and efficient − it embraces and exposes SQL to allow proper full-text search, in real-time.

Example

PUT /schoolpst/_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": "Gachibowp 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}

On running the above code, we get the response as shown below −

{
   "took" : 277,
   "errors" : false,
   "items" : [
      {
         "index" : {
            "_index" : "schoolpst",
            "_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" : "schoolpst",
            "_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" : "schoolpst",
            "_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 example shows how we frame the SQL query −

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

On running the above code, we get the response as shown below −

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

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

Advertisements