Elasticsearch Curl Operation

Source: Internet
Author: User

Operation of Curl
Curl is an open source file Transfer tool that works with URL syntax in the command line mode, and using curl makes it easy to implement common get/post requests. Simply think of a tool that can access the URL below the command line. In the CentOS default library there are curl tools, if not please install Yum.
Curl
-x Specifies HTTP request method with head GET POST PUT DELETE
-d Specifies the data to transfer
-h Specifies HTTP request header information
Browse ES servers
Curl-xget http://master:9200 <=> Access in the browser
Create an index library
Curl-xput http://master:9200/bigdata_p
This creates an index library in es bigdata_p

Both post and put can be used to create the difference between the two:
Put is a idempotent method, and post is not. So put user update, post for new more appropriate.
Es points of note when creating index libraries and indexes
1) The index library name must be all lowercase, cannot begin with an underscore, and cannot contain commas
2) If the ID of the index data is not explicitly specified, then ES will automatically generate a random ID, which requires the use of the post parameter
Curl-xpost http://localhost:9200/bigdata/product/-d ' {"Author": "Doug Cutting"} '
Add data to the index library
In the specific type, add the relevant document
Curl-xput http://master:9200/bigdata_p/product/-d ' {"name": "Hadoop", "Author": "Doug cutting", "c_version": "2.7.3"} '
Querying data in an index library
Querying the entire index library: Curl-xget Http://master:9200/bigdata_p/_search?pretty
Adding a pretty after the URL will format the returned result.
Query a type:curl-xget http://master:9200/bigdata_p/product/_search?pretty
Query for a specific record: Curl-xget Http://master:9200/bigdata_p/product/1?pretty
Query for a specific field in an indexed document: Curl-xget Http://master:9200/bigdata_p/product/1?_source=name&pretty
If you want to query multiple fields, use "," to separate them. eg.
Curl-xget Http://master:9200/bigdata_p/product/1?_source=name,author&pretty
Get Source All data
Curl-xget Http://master:9200/bigdata_p/product/1?_source&pretty
Query based on criteria
Curl-xget Http://master:9200/bigdata_p/product/_search?q=name:hbase,hive&pretty
-------------------
ES update
ES can use put or post to update a document, and if a document with the specified ID already exists, the update operation is performed
Note: When performing an update, ES first marks the old document as deleted, and then adds a new document, the old
The file will not disappear immediately, but you cannot access it, and ES will continue to add more data when the background cleanup has been marked as deleted.
Document except the state.
Local updates
You can add a new field or update a field that already exists (you must use post)
Curl-xpost http://master:9200/bigdata_p/product/2/_update-d ' {"Doc": {"c_version": "2.0.0", "Publish_time": " 2017-03-23 "}} '
Query Result:
"Hits": [{
"_index": "Bigdata_p",
"_type": "Product",
"_id": "2",
"_score": 0.30685282,
"_source": {
"Name": "HBase",
"Author": "Apache",
"C_version": "2.0.0",
"Publish_time": "2017-03-23"
}
} ]
Normal Delete, delete according to primary key
Curl-xdelete http://master:9200/bigdata_p/product/3/
Description: If the document exists, the ES property of the Found:true,successful:1,_version property has the value +1.
If the document does not exist, the ES property found is false, but the version value, versions, is still +1, which is the internal
Management, a bit like the SVN version number, guarantees that the order of our different operations across multiple nodes is correctly marked.
Note: After a document is deleted, it does not take effect immediately and he is only marked as deleted. ES will be added after you
More indexes will be deleted in the background.

Bulk Operation-bulk
Bulk API can help us execute multiple requests at the same time
Format:
Action:[index|create|update|delete]
metadata:_index,_type,_id
Request Body:_source (delete operation not required)
{action:{metadata}}\n
{Request body}\n
{action:{metadata}}\n
{Request body}\n
The difference between create and index
If the data exists, using the create operation fails, prompting the document to already exist, and using index to execute successfully.

How files are used
Curl-xpost/put Http://master:9200/index/type/_bulk--data-binary @path
Like what
Curl-xpost ' Http://master:9200/bank/account/_bulk--data-binary @/home/uplooking/documents/accounts.json
Query Result:
Http://master:9200/bank/_search?pretty
{
"Took":----> Default Top 10
"Timed_out": false,
"_shards": {
"Total": 5,
"Successful": 5,
"Failed": 0
},
"Hits": {
"Total":-----> 1000 Records
"Max_score": 1.0,
Can look at the individual index library information

Curl ' http://localhost:9200/_cat/indices?v '

Brief introduction:

The Curl tool is a tool that can access URLs at the command line, supporting get and POST request methods. -x Specifies the method of the HTTP request, and-d specifies the data to transfer.

To create an index:

Put creation

Curl-xputhttp://localhost:9200/shb01/student/1-d ' {"name": "Jack", "age": +, "info": "Ilove You"} '

{"_index": "Shb01", "_type": "Student", "_id": "1", "_version": 1, "Created": true} Youhave New Mail In/var/spool/mail/root

There is a return value after executing put

_index Index Name

_type type name

_version Version number

The created:true represents a newly created.

The above command will add 1,-xput each time the version is executed, and the ID must be set.

Post CREATE Index

Curl-xposthttp://localhost:9200/shb01/student-d ' {"name": "Tom", "Age": +, "info": "Tom"} '

{"_index": "Shb01", "_type": "Student", "_id": "AVADZUNGXSKBS1RG2TDP", "_version": 1, "Created": true}

Using post to create index data,-xpost can specify an ID, at which time the same data is modified, the ID is randomly generated, and new data is generated each time the execution occurs.

If you need to generate new data each time you execute it, you can use the post command without specifying an ID.

If you use the put command you need to add create, the command format is as follows

Curl-xput http://localhost:9200/shb01/student/1/_create-d ' {"name": "Jackk", "Age": 31} '

Curl-xputhttp://localhost:9200/shb01/student/1?op_type=create-d ' {' name ': ' Jackk ', ' Age ': 31} '

The error message is given when the above two command executes if there is data with the same ID

{"Error": "Documentalreadyexistsexception[[shb01][2][student][1]: Document already exists]", "Status": 409}

The difference between post and put

Put is an idempotent operation, that is, no matter how many times the result is executed, such as Del no matter how many times the result of the index library is deleted, put specifies the ID and the data remains unchanged regardless of how many times the data in the index library is executed, only version changes.

Each execution of the post produces new data.

Inquire

1: Querying the type in the index library SHB01 student

Browser: Http://192.168.79.131:9200/shb01/student/_search?pretty

Curl:curl-xget Http://192.168.79.131:9200/shb01/student/_search?pretty

It displays the same results as the browser.

2: Querying the data in document 1

Http://192.168.79.131:9200/shb01/student/1?pretty

Http://192.168.79.131:9200/shb01/student/1?_source&pretty

The two results are the same

Http://192.168.79.131:9200/shb01/student/1?_source=name&pretty

You can specify which fields are displayed by source

3: Querying all Index library information

Browser: Http://192.168.79.131:9200/_search?pretty

Displays the data for the index library SHB01 and SHB02.

4: Search by condition

Browser: Http://192.168.79.131:9200/shb01/student/_search?q=name:zs&pretty

Querying for data with name ZS

5: Querying cluster status

Curl–xget Http://192.168.79.131:9200/_cluster/health?pretty

Http://192.168.79.131:9200/_cluster/health?pretty

6: Multi-index, multi-type query, paging query, time-out

Curl:curl-xget Http://192.168.79.131:9200/shb01,shb02/stu,tea/_search?pretty

Curl-xget Http://192.168.79.131:9200/_all/stu,tea/_search?pretty

Browser Remove Curl–xget can

Page out

Curl-xget http://192.168.79.131:9200/shb01/stu/_search?size=2&from=0

Timeout

Curl-xpost http://192.168.79.131:9200/_search?_timeout=100

Update

Es

Partial update

If you have a lot of fields in document 1 and we just need to update one or two of them, you can specify the fields you want to modify by using Doc. Other fields do not have to be modified.

Crul–xput

Http:192.168.79.131:9200/shb01/student/1/_update?version=1

–d ' {"Doc": {"name": "UpdateName"} '

Full Volume Update:

Update the contents of all fields in document 1.

CURL-XPUTHTTP://192.168.79.131:9200/SHB01/STUDENT/1-d ' {"name": "would", "age": +, "info": "Newonw"} '

Update process

Es marks the old document and then adds new data, and the old document is no longer accessible, and ES cleans up data that is already deleted when the data is added later.

Delete

Deleting a document does not take effect immediately, it will only be marked as deleted and will be deleted in the background when more indexes are added later.

Curl-xdelete HTTP://192.168.79.131:9200/SHB01/STUDENT/AVAD05EEXSKBS1RG2TDQ

Deleted according to the ID, delete successfully returned found:true, cannot find Found:false, the version number will be added 1.

Delete Index SHB01,SHB02 all documents with name ZS in Student,tea type, based on condition deletion

Curl-xdeletehttp://192.168.79.131:9200/shb01,shb02/student,tea/_query?q=name:zs

Delete all documents with the name Tom in the index library

Curl-xdelete Http://192.168.79.131:9200/_all/_query?q=name:tom

Batch Processing

Loading a batch of data into memory then interacting with ES once, and processing multiple requests concurrently with the Redis pipeline is similar.

Format:

Action:index/create/delete/update

metadata:_index/_type/_id

Create: An error if the data exists; index: If the data exists, it will still execute successfully.

Steps:

1: Create a file under Liunx request1,vi request1

{"Index": {"_index": "Shb01", "_type": "Student", "_id": "1"}}

{"Name": "St01", "Age": "Ten", "info": "ST01"}

{"Create": {"_index": "shb100", "_type": "Student", "_id": "2"}}

{"Name": "Tea01", "Age": "Ten", "info": "Tea01"}

{"Delete": {"_index": "Shb01", "_type": "Student", "_id": "AVADZUNGXSKBS1RG2TDP"}

{"Update": {"_index": "Shb02", "_type": "Tea", "_id": "1"}}

{"Doc": {"name": "Zszszszs"}}

In the file

Index indicates the type of operation

_INDEX Specifies the index library, _type the specified type, _id specifies the action document

2: Execute batch command, keyword _bulk

Curl-xputhttp://192.168.79.131:9200/_bulk--data-binary @/usr/local/request1

Note: There are spaces between [email protected], and I have no space in the experiment to indicate that the operation parameters are not correct.

3: Return value

{

"Took": 957, "errors": false, "items": [

{"Index": {"_index": "Shb01", "_type": "Student", "_id": "1", "_version": "Status": 200}},

{"Create": {"_index": "shb100", "_type": "Student", "_id": "2", "_version": 1, "Status": 201}},

{"Delete": {"_index": "Shb01", "_type": "Student", "_id": "AVADZUNGXSKBS1RG2TDP", "_version": 2, "status": $, "found": True},

{"Update": {"_index": "Shb02", "_type": "Tea", "_id": "1", "_version": 2, "status": 200}}

]

The return information in errors indicates that the batch has no errors, note the version and status, where shb100 is the newly created index library

Here is the second time I execute the return information of the Request1 file, errors is true, indicating that there is an operation failure in the batch, you can see that the create because the library already has the same ID document so error. However, although there is an error operation, other operations are still successfully executed. This is similar to transactional operations in Redis.

{

"Took": "Errors": true, "items": [

{"Index": {"_index": "Shb01", "_type": "Student", "_id": "1", "_version":, "status": 200}},

{"Create": {"_index": "shb100", "_type": "Student", "_id": "2", "status": 409, "error": "documentalreadyexistsexception[ [SHB100] [3] [Student] [2]: Document already exists] "}},

{"Delete": {"_index": "Shb01", "_type": "Student", "_id": "AVADZUNGXSKBS1RG2TDP", "_version": 1, "status": 404, "Found": False}},

{"Update": {"_index": "Shb02", "_type": "Tea", "_id": "1", "_version": 3, "status": 200}}

]

}

4: Specify the index library and type in the command

Create a file that does not have the index library and type configured in the file

{"Index": {"_id": "1"}}

{"Name": "St1_1", "Age": "Ten", "info": "St1_1"}

{"Create": {"_id": "200"}}

{"Name": "st200", "Age": "Ten", "info": "st200"}

Execute the following command, specifying the index library and type in the command

curl-xputhttp://192.168.79.131:9200/shb01/student/_bulk [Email Protected]/usr/local/request2

return information

{

"Took": "Errors": false, "items": [

{"Index": {"_index": "Shb01", "_type": "Student", "_id": "1", "_version": ~, "status": 200}},

{"Create": {"_index": "Shb01", "_type": "Student", "_id": "$", "_version": 1, "Status": 201}}

]

}

5: You can also replace-xput with-xpost

Elasticsearch Curl Operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.