Elasticsearch Getting Started with Go

Juliardi Fajar Hidayatullah
5 min readNov 1, 2022

--

Go x Elasticsearch

Hi everyone, this is my first article that shares how to get started with Elasticsearch using Go. It covers building a simple HTTP server that can perform basic CRUD operations. After reading this article, I hope you can develop cool projects with Elasticsearch x Go. So, without any further ado, let’s Go for it!

What is Elasticsearch

Elasticsearch is an open-source service used to build your own search engine. It’s scalable and robust for handling high search traffic. Many big tech companies (like Netflix, Microsoft, Etc.) already implement Elasticsearch for many use cases. Therefore, if you want to build a powerful search engine, you have to try this one.

Running with Docker

There are many ways to run an Elasticsearch server. Using docker is the simplest way. It is also easy to uninstall by removing its container. If you don’t install docker yet, don’t worry; you can follow the installation steps from the docker docs below :

Once the installation has done, you can type the command below to run the Elasticsearch server :

docker run -d --name es01 -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -it docker.elastic.co/elasticsearch/elasticsearch:7.14.0

Open this URL http://localhost:9200 in your browser. If you got the response with the tagline “You Know, for Search” congratulation, you successfully ran the Elasticsearch server on your local machine!

You Know, for Search

You may turn off this server by running the docker command below :

docker stop es01

Set up our Client

We need to prepare a Go HTTP client to communicate with Elasticsearch. Let’s initiate the client object with the health check function for now :

Create an Index

After our Go client is ready, we can continue to create our first index. Index on Elasticsearch is equivalent to the schema on SQL database. But, before doing that, we must design our data model first. Let’s use employee as our model :

Now we are ready to create our new index. Let’s make our index by hitting PUT /employee from Elasticsearch API:

curl --location --request PUT 'http://localhost:9200/employee' \
--header 'Content-Type: application/json' \
--data-raw '{
"mappings": {
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "text"
},
"address": {
"type": "text"
},
"salary": {
"type": "float"
}
}
}
}'

Furthermore, to check employee index is whether exists or not, we can hit GET /_cat/indices.

Show Available Indices

Finally, we can add the CreateIndex function in our client code struct :

Insert Some Data

After we create the employee index, we can begin to insert some data.

We can use API PUT employee/_doc/1 with the payload as follow :

curl --location --request PUT 'http://localhost:9200/employee/_doc/1' \--header 'Content-Type: application/json' \--data-raw '{"id": 1,"name": "Abdullah","address": "Madinah, Saudi Arabia","salary": 1000000}'

To ensure the data exists, we can hit GET employee/_doc/1 to retrieve the data from Elasticsearch.

Get First Employee Data

To be more realistic, we have to insert several new employees by repeating the call to PUT employee/_doc/{id} API with a random id. Let’s simplify this by wrapping the API insert into the InsertData and SeedingData functions for seeding several data :

Search the Data

The search operation is the strength of Elasticsearch. We can perform a complex search as a search engine does. But for now, we only do a simple process using a match :

curl --location --request GET 'http://localhost:9200/employee/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"match": {
"name": "abdullah"
}
}
}'

The query means to discover an employee from the given keyword name. It splits the input into words and then matches them with the data inside Elasticsearch.

The result should return the list of employee data if it matches the following Elasticsearch data.

Get the search result using postman

Then, let’s write the match query into our client code under the SearchData function :

Moreover, the search operation only supports a text type since a text type is a particular type with a unique full-text processing technique.

Update our Data

We can hit the endpoint POST employee/_update/{id} to update our employee’s data. We update partially with this endpoint. Therefore we can only write the data that we want to edit. On the following code, we only update the employee name :

curl --location --request POST 'http://localhost:9200/employee/_update/1' \--header 'Content-Type: application/json' \--data-raw '{"doc": {
"name": "Abdullah Ja'far"
}
}'

To ensure the data successfully update, we can hit GET employee/_doc/{id} to retrieve the newest data.

Then, let’s wrap it into our client code under the function UpdateData:

Data Deletion

We can call the DELETE employee/_doc/{id} endpoint if we want to delete employee data :

curl --location --request DELETE 'http://localhost:9200/employee/_doc/1'

Then, wrap it into our client code under the DeleteData function :

CRUD Server

Finally, let’s wire up all CRUD operations into our HTTP server :

The server is open in port 8080. Try accessing http://localhost:8080/health to ensure the server is working.

Let’s try our CRUD server by doing the following actions.

Insert Employee Data

Update Employee Data

Search Employee Data

Delete Employee Data

Wrapping Up

Congratulations, our CRUD server is up and running! You can play around to catch some ideas about implementing Elasticsearch on your fantastic project. Also, for the complete code you can found on my GitHub repository.

Lastly, thanks for reading my article; I hope this helps you get an idea about Elasticsearch. Stay tuned because I will share more advanced topics in the following posts.

--

--