What is Elasticsearch?

Tram Ho

1. Introduction

Hello everyone, today I will introduce a pretty cool and popular search technology that is elasticsearch. In this article, we will explore the concepts, implementation, APIs & operations of this search technique.

2. Introducing elasticsearch

Elasticsearch is an open source technology that helps us to create servers that support full text search.
Elasticsearch is a search engine based on Lucene software. It provides a full-featured, distributed search engine with an HTTP web interface that supports JSON data. Elasticsearch is developed in Java and is released open source under the Apache license.

3. Installation

There are many ways to install Elasticsearch, you can consult the documentation installed on home Elasticsearch in here .
In this article, I will choose how to install using docker .
I often choose to install by docker because in this way, I can actively turn on / off the server if not necessary. In case of multiple projects it is also possible to create different elasticsearch containers to work with independent servers. In addition, it helps me avoid many errors when a traditional installation has problems (lack of libraries, related packages …)
This way, with a little knowledge of docker, we can easily set up it. First pull the image of elasticsearch with the command:

After successfully pulling the elasticsearch image, we can start the elasticsearch server using the docker container with the command:

After starting 1 Elasticsearch server with docker successfully, at this time, the container will bind 2 ports 9200 & 9300 respectively to the local machine.
Port 9200 will be used for all APIs over the HTTP protocol, while port 9300 is a custom binary protocol used to communicate between nodes in a cluster.
We can visit http: // localhost: 9200 in our browser to view information about Elasticsearch server.

4. Concepts

In the next section, we will learn about some of the concepts of Elasticsearch

Node

A node is a single server that is part of the cluster, which stores your data, and is involved in indexing and search functions.

Cluster

Cluster is a set of connections of nodes in Elasticsearch, If you start a single node (single node) like the command start Elasticsearch container above, you already have a cluster with 1 node in it.

Index

An index is a collection of documents that contain similar properties. Index is also identified by a name, which is used when performing operations such as adding, modifying, deleting, or updating documents within it.

Document

Document is the basic unit of information that needs to be indexed. For each type of index, you can have an infinite number of documents in it.

5. Elasticseach API

Next, we will explore the basic APIs of elasticsearch.

5.1. Check the status

First, let’s check the status of our elasticsearch server using the following API:

Use the curl command to test the following:

The status green shows that our elasticsearch server is up and running.

5.2. API indexes

In Elasticsearch, the index acts as a database table in SQL, it is a collection of data from which we can query for the desired information. In essence here, Elasticsearch is based on how we organize and put data in according to the indexes and from there Elasticsearch will assist us in querying and searching based on that data.

Get index

First we test the indexes with the following API

First maybe the server currently does not have any indexes, it’s okay, we continue to the index creation API and will check again later.

Create index

Create an index product using the following API:

The curl command:

The response returning the above result means that the index product was successfully created. The pretty parameter to the output displays the pretty JSON result for easier testing
Now let’s check out the API get index above:

Thus we have seen information 1 index has just been created. The ?v parameter to display the title makes checking in easier.

Delete index

Delete the index using the following API

Execute the curl command:

The result shown above means the index product has been successfully deleted

5.3. Document APIs

In the next section, we will see how to manipulate data on indexes (which are documents)? How do the CRUD document APIs & retrieve document information?
First, let’s create a new index book with the following command:

Create document

Create a new document in the index using the following API:

For example, create a new document in index book with id = 1 with the following command

Normally, we will have to specify the id of the document, in case we don’t pass the parameter document_id , the server will automatically generate an id for that document.

Update document

Similar to the API create document above, the API updates the document using the PUT method.

The update, we are forced to specify the id of that document.

Get document by ID

Do a search for document information by id = the following API:

For example, use curl to search for document in index book with id = 1 created above:

We can check document information in the _source part of the output.

Show all document

Besides, we can also check all documents with the following command:

Delete document

Performing delete document using API

5.4. Query on Elasticsearch

At this point, we have grasped the concepts and some basic APIs for manipulating documents & indexes in Elasticsearch.
In this next section, we will learn about the very important feature of Elasticsearch which is some of the ways to query how to get data at will in Elasticsearch.
In Elasticsearch there are many querying techniques to get the desired data, but in this section I only mention some basic queries that help you to understand, apply and learn from that. another query
First, to make it easy to visualize this query example we need to prepare a sample data set. In this example I will use the sample data elasticoffee-data on Github repo elastic / examples .
Specifically, I will insert 20 records in the example data above into the elasticoffee index with the following command:

To query, we will use the following API:

We will change the rquest body for the query, I will give a few examples to easily visualize, remember to pay attention to the request body part.

Example 1 – Search all documents, paginate 2 documents / page & start from document 5

The response receives 2 documents with ids 6, 7

Example 2 – Search all documents, paging 2 documents / page sort by beverageIndex DESC

=> Response is displayed with 2 documents with id = 15 & 17 with the highest beverageIndex.

Example 3 – Search all documents, paginate 2 documents / page with condition entityID = “zwave.quad4”

=> Response returns 2 documents with id = 7, 8 with value entityID = “zwave.quad4”

Example 4 – Search for documents with the conditional entityID = “zwave.quad4” & beverageIndex = “5”

=> Use the query bool.must :

=> Response will return document with id = 9 satisfying both the conditions entityID = “zwave.quad4” & beverageIndex = “5”

Example 5 – Search for documents with the condition entityID = “zwave.quad4” or beverageIndex = “5”

=> Use query bool.should :

=> Response returns documents with id = 9, 7, 8, 14, 15, 1 with 1 of 2 conditions satisfying entityID = “zwave.quad4” or beverageIndex = “5”

6. Conclusion

Thus, through this article I briefly introduced about Elasticsearch, how to install and use some basic APIs of this search technique. In addition to the basic queries I gave in the example above, there are still many other types of queries available to Elasticsearch, you can see more documentation on the Elasticsearch homepage here . Hope this article will help you in study as well as work, thank you for your attention.

Share the news now

Source : Viblo