ES-2 Basic Operation

On a last talked about, we can operate on elasticsearch through some rich Rest API.
These RestAPI the following format:

<HTTP Verb> /<Index>/<Endpoint>/<ID>

This section demonstrates some, mainly has the following operations:

First, view the health status

We can view the health status of the entire nodes in the cluster by restapi:

(A) shows the overall health status of the entire cluster

curl -X GET "localhost: 9200 / _cat / health v?"
effect is as follows:

epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1558560046 21:20:46  elasticsearch green           1         1      0   0    0    0        0             0                  -                100.0%

status displayed here is "green", in fact, may occur following three parameters:

  • green: everything is fine (fully functional clustering)
  • yellow: all data is available, but has not been assigned a number of copies (functional clustering)
  • red: some data is not available for some reason (cluster some features)

Note: When the cluster is red, it will continue to provide a search request from the available slices, but you may need to repair it as quickly as possible, because there is fragmentation unallocated.

(Ii) A list of respective nodes check the condition of

curl -X GET "localhost:? 9200 / _cat / nodes v"
effect is as follows:

ip        heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
127.0.0.1           13          96   1    0.02    0.04     0.05 mdi       *      www.disconf.com

It can be seen here showing us the name of each node, as this is the default www.disconf.com

Second, show all indexes

curl -X GET “localhost:9200/_cat/indices?v”

health status index uuid pri rep docs.count docs.deleted store.size pri.store.size

At first installation, there is no index.

Third, create an index

  1. Create an index the Customer
    curl -X PUT "localhost:? 9200 / the Customer Pretty"
  2. See all index again
    curl -X GET "localhost:? 9200 / _cat / indices v"
health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   customer BjeAF7BbRYaMQAK7VX_yqA   1   1          0            0       230b           230b

You will find that there is an index, called the customer, this index has a slice, a copy, but without any documents.
In addition, the index marked yellow health. Recall our previous discussion, yellow indicates that some copies have not yet (yet) allocation. This index is the reason this happens because by default Elasticsearch this indexed create a copy. Since we currently only one node running, so before adding another node cluster later point in time, still can not be assigned a copy (for high availability). After assigning a copy to the second node, the health of this index will change to green.

Fourth, the Document Actions

(A) to add documents to the index

the PUT the -X-curl "localhost:? 9200 / Customer / _doc / Pretty. 1" -H 'the Type-the Content: file application / JSON' -d '
{
"name": "John Doe"
}
'
display is as follows:

[2019-05-23T05:58:32,384][INFO ][o.e.c.m.MetaDataMappingService] [www.disconf.com] [customer/BjeAF7BbRYaMQAK7VX_yqA] create_mapping [_doc]
{
  "_index" : "customer",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

This represents has successfully created a document in the index below customer, his id is 1, so we specify at the time of indexing.
Note: Elasticsearch does not need to explicitly create the index before the document is indexed. In the previous example, if the client beforehand index does not yet exist, it will automatically create customer Elasticsearch index.

Above and then inserted into the document when the document specified id, in fact, we can not specify the id, such as:
curl -X POST "localhost: 9200 / the Customer / _doc Pretty?" -H 'Content-Type: the Application / json' - D '
{
"name": "zhangsan"
}
'

{
  "_index" : "customer",
  "_type" : "_doc",
  "_id" : "FSnz4WoBuS8YEwKRSe-g",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}

It can be seen randomly generate a document id is "FSnz4WoBuS8YEwKRSe-g" here

(B) to find documents based on ID

curl -X GET “localhost:9200/customer/_doc/1?pretty”

{
  "_index" : "customer",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "John Doe"
  }
}

You can see, we find this document, the contents stored in _source property inside.

(C) the replacement of an ID document

When we use PUT or POST data insertion, if id corresponding document already exists, the old file will be overwritten
curl -X PUT "localhost:? 9200 / customer / _doc / 1 pretty" -H 'Content-Type : the Application / json '-d'
{
"name": "lisi"
}
'
again View: curl -X GET "localhost: 9200 / customer / _doc / 1 pretty?"

{
  "_index" : "customer",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,
  "_seq_no" : 2,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "lisi"
  }
}
[

Note that: the entire document will be updated here, plainly, is to delete the contents of the original document, and replaced by a new document content

(D) According to the document ID update

In addition to want to (c) which, like replacing the entire document, you can also use the following update documents in this way:
curl -X POST "localhost: 9200 / the Customer / _Update / 1 Pretty?" -H 'Content-Type: the Application / json '-d'
{
"DOC": { "name": "zhaoqi", "Age": 100}
}
'
is not necessarily place in that:

  • url more above a _Update;
  • More inside {} a "doc"

In addition, we can also carry out updates via script:
curl -X POST "localhost: 9200 / the Customer / _Update / 1 Pretty?" -H 'Content-Type: the Application / json' -d '
{
"Script": "ctx._source + =. 5 .age "
}
'
ctx._source.age point where the age field of the document
, of course, ES is also provided a plurality of documents simultaneously update function, see:
https://www.elastic.co/guide/en /elasticsearch/reference/7.1/docs-update-by-query.html
Note: this updated, there is no treatment for property, it will keep the original value

(E) delete a document based on the ID

Delete the document operation is very simple:
curl -X DELETE "localhost:? 9200 / the Customer / _doc / 1 Pretty"

{
  "_index" : "customer",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 8,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 9,
  "_primary_term" : 1
}

Find Again id of document 1: curl -X GET "localhost:? 9200 / customer / _doc / 1 pretty"

{
  "_index" : "customer",
  "_type" : "_doc",
  "_id" : "1",
  "found" : false
}

(Vi) bulk operations

  1. Batch into the document
    curl the -X-the POST "localhost: 9200 / Customer / _bulk Pretty?" -H 'the Type-the Content: file application / JSON' -d '
    { "index": { "_ ID": ". 1"}}
    { "name ":" wukong "," age ": 10000," sex ":" M "}
    {" index ": {" _ ID ":" 2 "}}
    {" name ":" bajie "}
    '
  2. Bulk insert or delete documents
    curl -X POST "localhost: 9200 / the Customer / _bulk Pretty?" -H 'Content-Type: the Application / json' -d '
    { "Update": { "_ the above mentioned id": "1"}}
    { "DOC": { "name": "qitiandasheng"}}
    { "Delete": { "_ ID": "2"}}
    '

(Vii) imported document

Here simulate 1000 json file into a document, and then introduced into the file from the es.

  1. Generate json file, you can www.json-generator.com own produce json file
    can also use the sample file provided by the official website es: accounts.json
  2. The accounts.json files in the current directory server
  3. Perform an import:
    curl -H "Content-Type: the Application / json" -XPOST "localhost: 9200 / Bank / _bulk Pretty & Refresh?" --Data-binary "@ accounts.json"
  4. View Document Information:
    curl "localhost:? 9200 / _cat / indices v"
health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   bank     TmeDSLXnTKG4h2eiw-gUYA   1   1       1000            0    414.2kb        414.2kb
yellow open   customer OlNjOQgRSwu2zUQNpMConA   1   1          1            1      4.8kb          4.8kb

You can see, there are 1000 documents are imported into the bank index.

Fifth, remove the index

curl -X DELETE “localhost:9200/customer?pretty”

[2019-05-23T06:09:43,618][INFO ][o.e.c.m.MetaDataDeleteIndexService] [www.disconf.com] [customer/BjeAF7BbRYaMQAK7VX_yqA] deleting index
{
  "acknowledged" : true
}

See all index again
curl -X GET "localhost:? 9200 / _cat / indices v"

health status index uuid pri rep docs.count docs.deleted store.size pri.store.size

Finding no index.

Guess you like

Origin blog.csdn.net/mail_liuxing/article/details/91827004