Go: ElasticSearch Clients Study Case
--
Choosing a package to consume ElasticSearch is the first step when setting a project up that uses it. This choice could impact the performance of your application if it is an important part of your workflow. However, there are few options with Go for this case: olivere/elastic is the most famous one, and the elastic/go-elasticsearch is the official client. Let’s review the pros and cons of each package.
Design
The libraries are designed in pretty different ways and, as consumers, we need to feel comfortable with the provided design. Let’s take a query in example and build in with both in order to see the difference. Here is the query:
The package olivere/elastic provides a full Query DSL (Domain Specific Language) that allows a total abstraction of the real query. Here is the built query:
And here is the query built with the official client:
This query is more verbose but more explicit. Building the payload for ElasticSearch is the transformation of the original JSON to a map in Go.
Both the designs can suit different kinds of developers. Developers that do not want to know how ElasticSearch queries work could prefer the first approach, while a developer that would like to control the exact query sent without learning an extra layer could go the second approach. Let’s now review the performance of each of them.
Benchmarks
For this example, I have created a simple structure to store along with a simple query to run against the index. Here is an example of the documents stored:
The query used is the one introduced in the previous section. The benchmarks will cover:
- building of the query