A tentative study of Elasticsearch (I.)

Source: Internet
Author: User

Elasticsearch's official website
https://www.elastic.co/

First, installation
Elasticsearch is based on lence, and Lence is an open source library written in Java that relies on Java's operating environment. The Elasticsearch version that is now in use is 1.6, and it requires a version of jdk1.7 or more.
This article uses the Linux system, installs the configuration good Java environment, the download down, the decompression after the direct execution starts is OK.


1. Install the boot elasticsearch:
CD-to-elasticsearch-1.6.0.tar.gz placed directory,
Decompression TAR-XVF elasticsearch-1.6.0.tar.gz
Starting./elasticsearch-1.6.0/bin/elasticsearch, viewing the boot information, will mention that the default port for HTTP is 9200,transport default port is 9300, which is very important.


Next you can enter a command in terminal to see some basic information
View Cluster
Curl ' localhost:9200/_cat/health?v '
View nodes
Curl ' localhost:9200/_cat/nodes?v '
View Index
Curl ' localhost:9200/_cat/indices?v '
All of the above information can be viewed in http://localhost:9200/_plugin/head/after installing the head plugin

2. Install the head plugin
CD to Elasticsearch-1.6.0/bin directory, run./plugin-install Mobz/elasticsearch-head,
After installing and starting Elasticsearch, you can see the information of ES clusters, nodes, indexes, data, and so on when the browser opens http://localhost:9200/_plugin/head/.


Second, the basic operation
Main operation: increase, delete, change, check, batch operation,

1. Build the index:
Curl-xput ' Localhost:9200/megacorp/employee/8?pretty '-d ' {
"First_Name": "Douglas",
"Last_Name": "Fir",
"Age": 35,
"About": "I like to build cabinets",
"Interests": ["forestry"]
}‘
Results
{
"_index": "Megacorp",
"_type": "Employee",
"_id": "8",
"_version": 1,
"Created": true
}
A document with an index of _index of Megacorp and a type _type of EMPLOYEE,_ID 8 is established. With a pretty parameter, meaning that the returned result is formatted as a JSON format for display. Also, please note that the returned results, there is a "_version" field, which represents the version number of the data, now this data is the first time, so its value is 1.
And the above three sections: _index, _type, _id is the metadata of the document and also the three metadata nodes that must be.

Note: The ID is just a string that, when combined with _index and _type, uniquely identifies a document in Elasticsearch. When creating a document, you can customize the _id, or let Elasticsearch help you generate it automatically. However, in a type, the ID must be unique. Run the above command repeatedly, the returned result version number will change, if the content has been modified, it will overwrite the original value.

2. Delete
Delete a document
Curl-xdelete ' Localhost:9200/megacorp/employee/3?pretty '
Delete Type
Curl-xdelete ' Localhost:9200/megacorp/employee
Delete Index
Curl-xdelete ' Localhost:9200/megacorp

Careful observation of the instructions above, you can find that the format of the instructions are as follows, it takes the rest of the style, so that users can easily learn this command.
Curl-x://

In addition, there is a way to delete the document, similar to the SQL Delete-where, the command is as follows
Curl-xdelete ' Localhost:9200/megacorp/employee/_query?pretty '-d ' {
"Query": {"match": {"first_name": "Douglas"}}
}‘
This removes all documents in the employee type, all first_name with the Douglas keyword.

3. View the documents in the index:
Curl-xget ' Localhost:9200/megacorp/employee/8?pretty '
Results:
{
"_index": "Megacorp",
"_type": "Employee",
"_id": "8",
"_version": 3,
"Found": true,
"_source": {
"First_Name": "Douglas",
"Last_Name": "Fir",
"Age": 35,
"About": "I like to build cabinets",
"Interests": ["forestry"]
}
}

4. Modify the document
1) You can repeatedly run the above indexed command, can overwrite the original document, the effect of the modification.
2) You can also use the following command:
Curl-xput ' Localhost:9200/megacorp/employee/8?pretty '-d ' {
"Doc": {
"First_Name": "Douglas",
"Last_Name": "Fir",
"Age": 35,
"About": "I like to build cabinets update this document",
"Interests": ["forestry"]
}
}‘
Using this method to update the data, Elasticsearch not overwrite the original document, but instead delete the original document and create a new document.

5. Batch Operation
All mentioned above is a command to perform a single operation, not one operation to send a request, which is very resource-intensive, and elasticsearch this point, provide a bulk operation of the interface, allow one request to contain multiple operations, and then return the results after all operations.
Such as:
Curl-xpost ' Localhost:9200/customer/external/_bulk?pretty '-d ' {"index": {"_id": "1"}} {"Name": "John Doe"} {"index": {" _id ":" 2 "}} {" Name ":" Jane Doe "} '
Two documents were created for the index.
Again such as:
Curl-xpost ' Localhost:9200/customer/external/_bulk?pretty '-d ' {"Update": {"_id": "1"}} {"Doc": {"name": "John Doe becom Es Jane Doe "}} {" Delete ": {" _id ":" 2 "}} '
Using the _bulk API to implement bulk operations, when a request contains multiple actions, those operations will be executed sequentially, and if an error occurs, then the next execution continues until all operations are completed and the results of all operations are returned, from which we can view the execution of each operation.
The following is the result of the execution of the second instruction above:
{
"Took": 6,
"Errors": false,
"Items": [{
"Update": {
"_index": "Customer",
"_type": "External",
"_id": "1",
"_version": 4,
"Status": 200
}
}, {
"Delete": {
"_index": "Customer",
"_type": "External",
"_id": "2",
"_version": 3,
"Status": 200,
"Found": true
}
} ]
}

You can also import data from a JSON file, put the above command-D message into a. json file, you can perform the relevant operations inside.
Curl-xpost ' Localhost:9200/bank/account/_bulk?pretty '--data-binary @accounts. JSON

Third, search
There are two basic ways to search, the main difference being the form of a send parameter, the use of a rest request URI in the form of a parameter, and the other way of using the rest of the request body to pass the parameter. Using the method of the request body, the expression of the parameter can be richer and more readable, which is generally recommended.

REST Quest URI:
Curl ' Localhost:9200/bank/_search?q=*&pretty '
The thing to do with this command is to search out all the documents in the Bank index.

REST Request Body:
Curl-xpost ' Localhost:9200/bank/_search?pretty '-d ' {"Query": {"Match_all": {}}} '

It is worth noting that when the search results are returned, Elasticsearch will end the request and will not cache any results on the server or keep the requested link.
In addition, Elasticsearch also provides a JSON-style domain-specific language for the user to implement the query, in ES can become a query DSL, the function is very powerful, now in this is not explained in detail.



References in this article are:
Https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
Http://learnes.net/getting_started/README.html

A tentative study of Elasticsearch (I.)

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.