Elasticsearch for Gophers

This project contains an example that showcases different features from the official Go Client for Elasticsearch that you can use as a reference about how to get started with Elasticsearch in your Go apps. It is not intended to provide the full spectrum of what the client is capable of — but it certainly puts you on the right track.

Elasticsearch for Gophers

You can run this code with an Elasticsearch instance running locally, to which you can leverage the Docker Compose code available in the project. Alternatively, you can also run this code with an Elasticsearch from Elastic Cloud that can be easily created using the Terraform code also available in the project.

Examples available in this project:

Movies Loading

The data model from this project is a collection of movies from the file movies.json. This file will be loaded in memory and made available within the context, which the other functions will work with. Here is an example of a movie:

    "year": 2012,
    "title": "The Avengers",
    "info": {
        "directors": [
            "Joss Whedon"
        "release_date": "2012-04-11T00:00:00Z",
        "rating": 8.2,
        "genres": [
        "image_url": "http://ia.media-imdb.com/images/M/[email protected]@._V1_SX400_.jpg",
        "plot": "Nick Fury of S.H.I.E.L.D. assembles a team of superhumans to save the planet from Loki and his army.",
        "rank": 48,
        "running_time_secs": 8580,
        "actors": [
            "Robert Downey Jr.",
            "Chris Evans",
            "Scarlett Johansson"

Connection Handling

Once the movies are loaded, the code will create a connection with Elasticsearch and make this connection available within the context as well.

newClient, err := elasticsearch.NewClient(elasticsearch.Config{
	Addresses: []string{
if err != nil {

Document Indexing

All the movies will be indexed in Elasticsearch. The example uses the Bulk API to index documents, which is the equivalent to this:

POST movies/_bulk
{ "index" : { "_index" : "movies", "_id" : "1" } }
{ "Year" : "2012", "Title": "The Avengers" }

Document Lookup

An example of document lookup is also available. Out of all movies loaded, an ID will be randomly selected, and the document associated with this ID will be looked up. Just like you would do with:

<div class="highlight highlight-source-json position-relative" data-snippet-clipboard-copy-content="GET movies/_doc/

GET movies/_doc/