To explore the cluster
REST API
Now that our cluster (node) is running, the next step is how to interact with it. Fortunately, Elasticsearch provides an easy-to-understand and powerful REST API for interacting with ES clusters. The API can do the following:
- Check the health, status, and statistics of clusters, nodes, and indexes.
- Manage data and metadata for your clusters, nodes, and indexes
- Provides CURD (Create, Read, Update, and Delete) and search operations on indexes.
- Perform advanced search operations such as paging, sorting, scripting, aggregation, and much more. (Translator’s note: In order to search performance, translators do not recommend some high-level operations such as scripts, put logical operations in the engine, similar to relational database stored procedures or functions, is not very efficient, and the use scenario can be optimized by business.)
Cluster health
Let’s start with a basic health check to see how the cluster works. We’ll use the curl command tool, but you can also use any tool that makes HTTP/REST protocol requests. Windows users will also need to install curl or use a browser plugin such as HttpRequester for Firefox or Sense for Chrome. I recommend Sense, which is made for ES.) Suppose we’re at the ES node we just started, and we open a new console. To check cluster health, use the _cat API, remember that our node port was’ 9200 ‘:
curl ‘localhost:9200/_cat/health? v’
The response is as follows:
epoch timestamp cluster status node.total node.data shards pri relo init unassign 1394735289 14:28:09 elasticsearch green 1 1 0 0 0 0 0
You can see that our cluster elasticSearch is green. Whenever we request cluster health status, we get green,yellow, or red. Green means that everything is OK (when the cluster is fully functional), yellow means that the cluster has all its data, but some copies have not been allocated (when the cluster is fully functional), and red means that some data has been lost for whatever reason. Note that even if the cluster is red, it is still partially available (search requests are still valid on available shards), but you still need to fix it as soon as possible because the data is incomplete.
We can also see from this that we have a node, and since we have no data our shard number is 0. Note that because we used the default cluster name ElasticSearch, and because ElasticSearch uses unicast node discovery by default to find other nodes on the same machine, it is possible that you will inadvertently start multiple nodes and add them to the cluster, so you may see more than one node in the corresponding cluster. We can get the node list by using the following command:
curl ‘localhost:9200/_cat/nodes? v’
The response is as follows:
Host IP heap.percent ram. Percent Load node. Role Master name mwubuntu1 127.0.1.1 8 4 0.00 D * New Goblin
We can see a node named New Goblin, the only node in the cluster.
Display all nodes
Let’s take a look at our index information
curl ‘localhost:9200/_cat/indices? v’
Response results:
health index pri rep docs.count docs.deleted store.size pri.store.size
This clearly means that there is no index in our cluster
Create indexes
Now we create an index named Customer and look at the index list:
curl -XPUT ‘localhost:9200/customer? pretty’ curl ‘localhost:9200/_cat/indices? v’
The first command uses the PUT request to create a “Customer” index, followed by the pretty parameter to allow json formatted output of the response. The responses are as follows:
{
"acknowledged" : true
}
Copy the code
health index pri rep docs.count docs.deleted store.size pri.store.size yellow customer 5 1 0 0 495b 495b
The second command tells us that we have a Customer index that contains five master shards and one copy (the default configuration), and that the index contains no documents.
You may have noticed, the cluster status is now marked the yellow, yellow discussed before there is a copy of the undistributed, ES is the cause of this phenomenon by default for the index to create a copy, because we have only one node cluster, in order to guarantee high availability, copy cannot be allocated, until there is a new node to join the cluster. Once the replica can be assigned to the second node, the health status of the node switches to green.
Index and query documents
Now let’s write some data into our Customer index, remember we said before that document insertion index needs to specify the type of index. Now insert a simple document of type external with ID 1 and json document {“name”: “John Doe”} as follows:
curl -XPUT ‘localhost:9200/customer/external/1? pretty’ -d ‘ { “name”: “John Doe” }’
The response is as follows:
{ “_index” : “customer”, “_type” : “external”, “_id” : “1”, “_version” : 1, “created” : true }
As you can see above, we created a document in the External type of the Customer index that has an internal ID specified during the index.
The key point to note is that ES does not force you to create an index for the document you show when creating it. If the Customer index does not exist, ES will create it automatically.
Let’s get back the document we just created:
curl -XGET ‘localhost:9200/customer/external/1? pretty’
The response is as follows:
{
"_index" : "customer"."_type" : "external"."_id" : "1"."_version" : 1."found" : true."_source" : { "name": "John Doe"}}Copy the code
There’s nothing special except for a found field, which means we’ve got a document with ID 1, and another field, _source, which contains the full contents of the document we indexed in the previous step.
Remove the index
Now let’s delete the index we just created and list all indexes again
curl -XDELETE ‘localhost:9200/customer? pretty’ curl ‘localhost:9200/_cat/indices? v’
The responses are as follows:
{
"acknowledged" : true
}
Copy the code
health index pri rep docs.count docs.deleted store.size pri.store.size
This indicates that we successfully deleted an index and the cluster is now back to where it was when it started.
Before we move on, let’s take a closer look at the API we learned earlier:
curl -XPUT ‘localhost:9200/customer’ curl -XPUT ‘localhost:9200/customer/external/1’ -d ‘ { “name”: “John Doe” }’ curl ‘localhost:9200/customer/external/1’ curl -XDELETE ‘localhost:9200/customer’
If we take a closer look at the above commands, we can find a pattern for accessing data in ES. This pattern can be summarized as:
curl -X :///
This REST command applies to all API commands in ES, and keeping this in mind will give you a good start in learning ES.
Install Elasticsearch
Alter data for Elasticsearch