Elastic Search Installation Guide and Basic Operations of its REST API

Keywords: Big Data curl Java ElasticSearch Windows

This article aims to show you how to install ES in windows system and how to use its REST API to operate ES in windows system.

(1) Installation of ES under Windows

Make sure that the machine has java installed before installing es, preferably a newer version. I have 10 installed on my machine.

C:\>java -version
java version "10" 2018-03-20
Java(TM) SE Runtime Environment 18.3 (build 10+46)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10+46, mixed mode)

Download address of es official website https://www.elastic.co/downloads/elasticsearch

After finding the corresponding version to download, extract it, find elasticsearch.bat and double-click to start it.

To view the following information after successful execution

At this point, the installation startup is over.

It can be turned off with Ctrl+c.

Now that we have a working node (and cluster), the next step is to understand how to communicate with it. Fortunately, Elastic search provides a very comprehensive and powerful REST API that allows you to interact with your cluster. On Windows, we use git bash to execute related commands, not under cmd (students who don't have it can install it).

(1) Operation Cluster

Here are a few things you can do with this API:

  • Check the health status and various statistics of your clusters, nodes and indexes
  • //View cluster status
    $ curl 'localhost:9200/_cat/health?v'
    
    //Response data
      % Total    % Received % Xferd  Average Speed   Time    Time                                  Time  Current
                                     Dload  Upload   Total   Spent                                 Left  Speed
      0     0    0     0    0     0      0      0 --:--:-- --:--:--                              100   296  100   296    0     0   9548      0 --:--:-- --:--:--                              --:--:--  9548epoch      timestamp cluster       status node.tot                             al node.data shards pri relo init unassign pending_tasks max_tas                             k_wait_time active_shards_percent
    1539669345 13:55:45  elasticsearch green           1         1                                   0   0    0    0        0             0                  -                                             100.0%
    —————————————————————————————————————————————————————————————————————————————————————————
    //Get the list of nodes in the node cluster
    $ curl 'localhost:9200/_cat/nodes?v'
    //Response data
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100   175  100   175    0     0   5645      0 --:--:-- --:--:-- --:--:--  5645ip    percent ram.percent cpu load_1m load_5m load_15m node.role master name
    127.0.0.1           38          67   2                          mdi       *      QxN
    
    —————————————————————————————————————————————————————————————————————————————————————————
    //List all indexes
    $ curl 'localhost:9200/_cat/indices?v'
    //Response data (since we haven't indexed yet, the index is empty here)
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100    83  100    83    0     0   5533      0 --:--:-- --:--:-- --:--:--  5533health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
    
    

     

  • Manage your clusters, nodes, indexed data and metadata
  • CRUD (Create, Read, Update and Delete) and Search for Your Index
  • //Create an index called myindices that pretty attaches to the end of the call to print out the JSON response in a beautiful form
    $ curl -XPUT 'localhost:9200/myindices?pretty'
    //Response data
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100    85  100    85    0     0     38      0  0:00:02  0:00:02 --:--:--    38{
      "acknowledged" : true,
      "shards_acknowledged" : true,
      "index" : "myindices"
    }
    —————————————————————————————————————————————————————————————————————————————————————————
    //Index a document error example
    
    $ curl -XPUT 'localhost:9200/myindices/sutdent/1?pretty' -d '{"name":"yuhua"}'
    //When you execute the above command, you will find that the following errors are reported because the corresponding parameters are not in the correct format.
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100   125  100   109  100    16   7266   1066 --:--:-- --:--:-- --:--:--  8333{
      "error" : "Content-Type header [application/x-www-form-urlencoded] is not supported",
      "status" : 406
    }
    
    //A good example of indexing a document. I tried http://as if I could add it or not.
    $ curl -H "Content-Type:application/json" -XPUT 'http://localhost:9200/myindices/student/1?pretty' -d '{"name":"yuhua"}'
    //Response data
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100   242  100   226  100    16    965     68 --:--:-- --:--:-- --:--:--  1034{
      "_index" : "myindices",
      "_type" : "student",
      "_id" : "1",
      "_version" : 1,
      "result" : "created",
      "_shards" : {
        "total" : 2,
        "successful" : 1,
        "failed" : 0
      },
      "_seq_no" : 0,
      "_primary_term" : 1
    }
    
    —————————————————————————————————————————————————————————————————————————————————————————
    //Extracting indexed documents
    $ curl -XGET 'http://localhost:9200/myindices/student/1?pretty'
    //Response data
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100   145  100   145    0     0   9062      0 --:--:-- --:--:-- --:--:--  9062{
      "_index" : "myindices",
      "_type" : "student",
      "_id" : "1",
      "_version" : 1,
      "found" : true,
      "_source" : {
        "name" : "yuhua"
      }
    }
    —————————————————————————————————————————————————————————————————————————————————————————
    //Delete index
    $ curl -XDELETE 'http://localhost:9200/myindices?pretty'
    //Response data
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
      0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0curl: (6) Could not resolve host: E
    100    28  100    28    0     0     68      0 --:--:-- --:--:-- --:--:--    68{
      "acknowledged" : true
    }
    —————————————————————————————————————————————————————————————————————————————————————————
    
    
    curl -X<REST Verb> <Node>:<Port>/<Index>/<Type>/<ID>
    //This REST access pattern is generally applicable to all API commands

     

  • Perform advanced query operations such as paging, sorting, filtering, scripting, faceting, aggregations, and many others

Or that old saying, if you want to use a good tool, you need to develop the habit of inquiring its documents and api s. It is very difficult to remember hundreds of ways by our own heads. Of course, it is not excluded that there are such extraordinary classmates. Below are two good materials, you can spare.

ES Authoritative Guide

https://es.xiaoleilu.com/010_Intro/25_Tutorial_Indexing.html

ES API document

https://endymecy.gitbooks.io/elasticsearch-guide-chinese/content/java-api/index-api.html

 

If you have any questions, you are welcome to interact with me. You can comment and leave a message. In the future, I will insist on updating at least one blog article every week. If you like, you can pay more attention to it.

Posted by angel_cowgirl on Wed, 30 Jan 2019 02:06:15 -0800