Learning is really a happy thing. After I shared the introduction of Redis last time, I received a lot of encouragement from my friends, such as: “Oh, not bad, brother, it is easy to understand, and it really started in ten minutes”. Check it out. I decided to go one step further and get a start on Elasticsearch, because my company’s shopping system has been upgraded and I need to use Elasticsearch for items.

However, I want to make it clear that I didn’t go into Elasticsearch very deeply, just to use it and learn it. However, as a responsible technical blogger, I have my heart in it. For this reason, I have specially bought a video course written by Ruan Yiming on XXX time. To be honest, his bald head made me really interested in the course.

After three days and three nights of studying, I finally got into Elasticsearch and decided to share my experience with others who are interested. If there is something wrong in the article, don’t go easy on me. Just don’t hit me in the face.

What is Elasticsearch

Elasticsearch is a distributed, RESTful search and data analysis engine that addresses a variety of emerging use cases. At the heart of the Elastic Stack, it stores your data centrally, helping you find what you expect and what you don’t expect.

The above quote comes from an official and, I have to say, rather artistic interpretation. Expected and unexpected, these two words remind me of a certain year’s college entrance examination essay question (reasonable and unexpected).

What’s an Elastic Stack? The overall architecture is shown in the figure below (from the network, intrusion and deletion).

It’s a lot of information, right? Elasticsearch is at the heart of the Elastic Stack.

Elasticsearch is used by many famous companies in China and abroad, such as Didi, Toutiao, Google, Microsoft and so on. Elasticsearch has a lot of great features like full text search, shopping recommendations, nearby location recommendations, and more.

Theoretical aspects of the content will not say too much, I am afraid that partners will feel boring. After all, the entry, the actual combat is important.

Install Elasticsearch

Elasticsearch was developed in Java, so earlier versions required JDK installation on your computer to support it. Later versions have a Java environment built in, so you can just download it. Elasticsearch has different packages for different operating systems, so we’ll use Windows as an example for this introductory article.

Download address:

www.elastic.co/cn/download…

The latest version is 7.6.2, around 280MB. But IT took me 10 minutes to finish the download. I don’t know whether the connected 200M bandwidth is not strong, or the download speed of the official website itself is slow. Anyway, I haven’t finished the download after washing 6 grapes.

Elasticsearch is install-free, you just need to unzip the zip package.

1) The bin directory contains some scripts, including the startup execution file of Elasticsearch.

2) The config directory contains some configuration files.

3) JDK directory is the built-in Java runtime environment.

4) The lib directory contains Java class library files.

5) Logs directory will generate some log files.

6) The modules directory contains some Elasticsearch modules.

7) You can put some Elasticsearch plugins in the plugins directory.

To start elasticSearch, double-click the elasticSearch. bat file in the bin directory.

There is too much log information in the output. If started is displayed, it indicates that the startup is successful. To verify that Elasticsearch has started successfully, enter http://localhost:9200 in the address box of your browser (9200 is the default port number for Elasticsearch).

You see, for Search.

So how do you stop service? You can directly press Ctrl+C key combination – rough, wall dong.

03. Install Kibana

With Kibana, you can visualize Elasticsearch as if you were installing a graphical interface on Linux.

Download address:

www.elastic.co/cn/download…

The latest version is 7.6.2, around 284M, and is about the same size as Elasticsearch. Choose to download Windows version, zip format, after the completion of direct decompression on the line. Download the process went to wash six grapes to eat, dog head.

Go to the bin directory and double-click the kibana.bat file to start the Kibana service. [Kibana][HTTP] HTTP Server running [Kibana][HTTP] HTTP Server running

Enter http://localhost:5601 in the browser address bar to view the Kibana graphical interface.

As there is no Data available on the Elasticsearch server, you can select “Try Our Sample Data” to import the simulation Data provided by Kibana. The picture below is the kanban page imported into the e-commerce database, isn’t it very rich?

Open the Dev Tools panel, and you’ll see a simple DSL query (a domain-specific language based entirely on JSON). Click the “Run” button and you’ll see the jSON-formatted data.

Key concepts for Elasticsearch

Before you go any further, you need to understand some of the key concepts in Elasticsearch, such as what an index is, what a type is, what a document is, etc. Elasticsearch is a data engine, and some of its concepts are related to MySQL.

After looking at the picture above, is it immediately clear? Select * from Elasticsearch; select * from Elasticsearch; select * from Elasticsearch;

Use Elasticsearch in Java

I’m a Java programmer. How do I use Elasticsearch in Java? That’s a good question. I’m on my way. I’m on my way.

Elasticsearch has a Java runtime built in, so there are a number of apis for you to use.

Step 1 add Elasticsearch client dependencies to your project:

<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> The < version > 7.6.2 < / version > < / dependency >Copy the code

Create test class ElasticsearchTest

public class ElasticsearchTest {
    public static void main(String[] args) throws IOException {

        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("localhost".9200."http")));

        IndexRequest indexRequest = new IndexRequest("writer")
                .id("1")
                .source("name"."Silent King II."."age".18."memo"."An interesting programmer.");
        IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);

        GetRequest getRequest = new GetRequest("writer"."1"); GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT); String sourceAsString = getResponse.getSourceAsString(); System.out.println(sourceAsString); client.close(); }}Copy the code

1) RestHighLevelClient Is a REST client provided for Elasticsearch. It can connect to the Elasticsearch server through HTTP. The parameters are the host name and port number.

With the RestHighLevelClient client, we can send a request to the Elasticsearch server and get a response.

2) IndexRequest is used to add an index to Elasticsearch server using the index keyword, such as “writer”, and the specified ID. You can add a document data source (in the form of key-value pairs) to the current index by source.

Once you have an IndexRequest object, you can call the index() method on the client side to add an index to the Elasticsearch server.

GetRequest is used to send a get request to the Elasticsearch server with the index key and id.

Once you have the GetRequest object, you can call the get() method on the client side to get the index from the Elasticsearch server. GetSourceAsString () is used to retrieve the document data source (as a JSON string) from the response.

Ok, let’s look at the output of the program:

{"name":"Silent King II."."age": 18."memo":"An interesting programmer."}
Copy the code

Exactly what we expected, Perfect!

You can also view the “Writer” index on the Kibana Dev Tools panel, as shown in the figure below.

06, thanks

That’s it for Elasticsearch, if you want to get your hands on Elasticsearch! If you have any problems in the process of learning, please feel free to communicate with me. Although I am a rookie, I am enthusiastic.

Also, if you want to write an entry level article, this is the perfect example.

I am silent King 2, an interesting programmer. If you think this article is helpful to you, please search “Silent King ii” on wechat and read it for the first time. Reply [666] there is also a 500G HIGH-DEFINITION teaching video (classified) that I prepared for you.

Interview on GitHub

Original is not easy, do not want a free ticket, please point a praise for this article, it will be the strongest power FOR me to write more high-quality articles.