Basic Elasticsearch CURD Operations

Once our ES cluster is set up, how can I see the status of each node in the cluster, as well as the master node and health status? Below I’ll explain how to use the curl command to interact with the ES cluster, covering checking the master node, the cluster status, creating an index and viewing indexes, viewing shards, and sending query requests to the ES cluster, and so on.

Explanation of CURL syntax

RESTful API:

    curl  -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'
    -X: specifies the request method

    <VERB>: GET, POST, PUT, DELETE   Generally GET for requests, POST for submitting changes, PUT for uploading files, DELETE for delete operations

    <PROTOCOL>: the protocol, generally HTTP

    <HOST>: the host, which can be the host's IP address or hostname

    <PORT>: the host port

    <PATH>: the path, the path after the host port, such as the following paths: /_cat, /_search, /_cluster   The /_cat path covers most of the information content, /_search searches all indexes and document types

    <QUERY_STRING>: the query string matching rules

    -d: specifies the body content

    <BODY>: the request body in JSON format

Basic Elasticseearch query statements

    //View the information supported by _cat
    kibana: GET /_cat
    bash: curl -XGET -u elastic:26tBktGolYCyZD2pPISW 'http://192.168.31.215:9201/_cat'        

    //View master node information
    kibana: GET /_cat/master?v
    bash: curl -XGET -u elastic:26tBktGolYCyZD2pPISW 'http://192.168.31.215:9201/_cat/master?v'        

    //View all nodes in the cluster
    kibana: GET /_cat/nodes?v
    bash: curl -XGET -u elastic:26tBktGolYCyZD2pPISW 'http://192.168.31.215:9201/_cat/nodes?v'        

    //View all index information
    kibana: GET /_cat/indices?v
    bash: curl -XGET -u elastic:26tBktGolYCyZD2pPISW 'http://192.168.31.215:9201/_cat/indices?v'        

    //View a single index's information
    kibana: GET /_cat/indices/movies?v
    bash: curl -XGET -u elastic:26tBktGolYCyZD2pPISW 'http://192.168.31.215:9201/_cat/indices/movies?v'        

    //View all shard information
    kibana: GET /_cat/shards?v
    bash: curl -XGET -u elastic:26tBktGolYCyZD2pPISW 'http://192.168.31.215:9201/_cat/shards?v'        

    //View a single index's shard information
    kibana: GET /_cat/shards/movies?v
    bash: curl -XGET -u elastic:26tBktGolYCyZD2pPISW 'http://192.168.31.215:9201/_cat/shards/movies?v'        

    //View cluster health status
    kibana: GET /_cat/health?v
    kibana: GET _cluster/health
    bash: curl -XGET -u elastic:26tBktGolYCyZD2pPISW 'http://192.168.31.215:9201/_cat/health?v'
    bash: curl -XGET -u elastic:26tBktGolYCyZD2pPISW 'http://192.168.31.215:9201/_cluster/health?pretty'        

    //View plugins
    kibana: GET /_cat/plugins?v
    bash: curl -XGET -u elastic:26tBktGolYCyZD2pPISW 'http://192.168.31.215:9201/_cat/plugins?v'        

    //View the total document count of all indexes
    kibana: GET _all/_count
    bash: curl -XGET -u elastic:26tBktGolYCyZD2pPISW 'http://192.168.31.215:9201/_all/_count?pretty'        

    //View the total document count of a specified index
    kibana: GET movies/_count
    bash: curl -XGET -u elastic:26tBktGolYCyZD2pPISW 'http://192.168.31.215:9201/movies/_count?pretty'        

    //View all templates
    kibana: GET _cat/templates
    bash: curl -XGET -u elastic:26tBktGolYCyZD2pPISW 'http://192.168.31.215:9201/_cat/templates?v'        

    //View indexes whose status is green
    kibana: GET /_cat/indices?v&health=green
    bash: curl -XGET -u elastic:26tBktGolYCyZD2pPISW 'http://192.168.31.215:9201/_cat/indices?v&health=green'        

    //View the movies index metadata
    kibana: GET movies
    bash: curl -XGET -u elastic:26tBktGolYCyZD2pPISW 'http://192.168.31.215:9201/movies?pretty'        

    //Sort indexes by document count
    kibana: GET _cat/indices?v&s=docs.count:desc
    bash: curl -XGET -u elastic:26tBktGolYCyZD2pPISW 'http://192.168.31.215:9201/_cat/indices?v&s=docs.count:desc'        

    //View the memory size used by each index and sort them
    kibana: 
    bash: curl -XGET -u elastic:26tBktGolYCyZD2pPISW 'http://192.168.31.215:9201/_cat/indices?v&h=i,tm&s=tm:desc'

Elasticsearch CURD syntax

CURD Request Method Body Description
Create PUT /index/_create/id Specify the Document ID to create a document; if the ID already exists, it fails
Create POST /index/_create/id Specify the Document ID to create a document; if the ID already exists, it fails
Create POST /index/_doc Automatically generates an ID, which will not be duplicated; repeated submissions create multiple documents, all with version 1
Index PUT /index/_doc/id If the ID does not exist, a new document is created; if the ID exists, the existing document is deleted and a new document is created, version +1, same ID
Index POST /index/_doc/id If the ID does not exist, a new document is created; if the ID exists, the existing document is deleted and a new document is created, version +1, same ID
Read GET /index/_doc/id View the document whose Document ID is 1
Update POST /index/_doc/id The document must exist, otherwise the update fails; you can only incrementally modify fields, not remove fields; field values can be modified freely, version increments by 1
Delete Delete /index/_doc/id The document must exist, otherwise the delete returns “not_found”
Delete Delete /index Deletes the index; the documents inside the index are also deleted along with it; the index to be deleted must exist, otherwise it returns “404”

Original article: https://mp.weixin.qq.com/s/qZq_EV9q1LUUzOZeTzhA-g