Getting started with Elastic Search in Golang

Kacper Bąk
4 min readDec 15, 2022

--

Photo by Ali Hajian on Unsplash

Elasticsearch is a distributed, open source search and analytics engine for all types of data, including textual, numerical, geospatial, structured, and unstructured. It is built on top of Apache Lucene and developed in Java. Elasticsearch is known for its simple REST APIs, scalability, speed, and its ability to handle a growing amount of data. It enables powerful real-time search and analytics capabilities that can be used to build powerful applications.

To get started with Elastic Search you need to follow these steps:

  1. Download and Install ElasticSearch: Download the latest version of ElasticSearch from Elastic’s official website and install it on your system.
  2. Create an Index: An index is a collection of documents. In Elasticsearch, an index is similar to a database in the world of relational databases. Use the create index API to create an index.
  3. Add Documents: You can add documents to an index using the index API. You can also bulk index documents using the bulk API.
  4. Search: Use the search API to search for documents in an index.
  5. Analyze: Use the analyze API to analyze text and get a list of tokens extracted from the text.
  6. Mapping: Use the mapping API to define the structure of documents in an index.
  7. Aggregations: Use the aggregations API to perform complex data analysis on documents in an index.
  8. Cluster Health: Use the cluster health API to get the health of the cluster.
  9. Monitor Performance: Use the cluster stats API to get performance metrics for nodes and indices in the cluster.
  10. Security: Use the X-Pack security plugin to secure your Elasticsearch cluster.

So for starters, I’ll start with these steps

  1. Download Elastic Search: Go to https://www.elastic.co/downloads/elasticsearch and click the download link for your operating system.
  2. Install the downloaded file: Unzip the file and run the installer.
  3. Verify that Elastic Search is running: Open a web browser and go to http://localhost:9200. You should see a response with some information about the Elastic Search cluster.
  4. Install plugins: If needed, install any plugins you require from the plugin repository.

You can also get things done using docker.

After enabling ElasticSearch, focus on creating Indexes. Indexes are an important component of Elastic Search because they are used to store the data that is indexed and to provide fast and efficient search results. Indexes enable users to quickly query and retrieve data from their database. By using an index, users can reduce the amount of time needed for a query, as well as increase the accuracy of the search results. Indexes also help to improve the performance of the search engine, allowing it to process more queries in a shorter amount of time.

To create Index in Elastic Search you have to:

  1. Log in to your Elasticsearch instance.
  2. Enter the following command: PUT /{index_name}
  3. Enter the required fields and settings for the index, and then press Enter.
  4. To verify that your index was created, enter the following command: GET /{index_name}
  5. If successful, the response will show the index’s settings and mappings.

For sending requests I personally use the Postman tool, however, using Insomnia or curl, is not at all inferior.

Using the following command you will be able to create a new index.

curl -X PUT localhost:9200/example_index123

Output:

{"acknowledged":true,"shards_acknowledged":true,"index":"example_index123"}

To check if your command definitely worked well, you can use such a command from the terminal position:

curl -X GET localhost:9200/example_index123

Output:

{"example_index123":{"aliases":{},"mappings":{},"settings":{"index":{"creation_date":"1670864329244","number_of_shards":"5","number_of_replicas":"1","uuid":"2xsAmXrBRKuxCOmfSYjh5g","version":{"created":"6040299"},"provided_name":"example_index123"}}}}

To connect the kibana, you will have to host it at your place, in my case I host it on the “elastic” network.

To test Elastic Search’s capabilities, I created an application that supports Elastic Search’s basic functions. Of course, I integrated this into the app in Go. By adding such sample code snippets, you will be able to implement various components of the application.

Create Elastic Search Client

 cfg := elasticsearch.Config{
Addresses: []string{"http://localhost:9200"},
}
es, err := elasticsearch.NewClient(cfg)
if err != nil {
return err
}
a = &App{es: es}

Create an Index in Elastic Search

res, err := a.es.Indices.Create(
"myindex",
a.es.Indices.Create.WithBody(strings.NewReader(`{"settings": {"number_of_shards": 1,"number_of_replicas": 0}}`)),
a.es.Indices.Create.WithContext(ctx),
)
if err != nil {
return err
}

The output should be HTTP Status 200 OK

Index a document in Elastic Search

 res, err = a.es.Index(
"myindex",
strings.NewReader(`{"title" : "Test"}`),
a.es.Index.WithDocumentID("1"),
a.es.Index.WithRefresh("true"),
)
if err != nil {
return err
}

The output should be HTTP Status 201 Created

Get a document in Elastic Search

 res, err = a.es.Get(
"myindex",
"1",
a.es.Get.WithPretty(),
)
if err != nil {
return err
}

The output should be HTTP Status 200 OK

Delete an index in Elastic Search

 res, err = a.es.Indices.Delete([]string{"myindex"})
if err != nil {
return err
}

The output should be HTTP Status 200 OK

The entire test application I used to demonstrate the basic capabilities of elastic-search can be found here: https://github.com/53jk1/elastic-search. It is very possible that I will develop it further in the future.

Elasticsearch is an open source search engine built on top of the Apache Lucene library. It provides a distributed, multitenant-capable full-text search engine with an HTTP web interface and schema-free JSON documents. Elasticsearch is becoming increasingly popular as a powerful, reliable and fast search engine. It is used by many large organizations to power their search capabilities, including eBay, Github, Etsy and Netflix. Its scalability, distributed nature and low-latency make it an ideal choice for search applications. It is also used for log analytics, application monitoring, and data analytics. Learning Elasticsearch can help developers build better search applications and make better use of the data they have.

References

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html

--

--