Youssef Ameachaq's Blog

Youssef Ameachaq

Elasticsearch Integration with Go


Elasticsearch is a distributed search and analytics engine designed to store, search, and analyze large amounts of data quickly and in near real time. To integrate Elasticsearch into a Go project, let’s break it down step by step.


1. Philosophy of Elasticsearch


2. Key Concepts



4. Integration with Go

To integrate Elasticsearch with Go, you can use the official Go client elasticsearch-go.

Setup

  1. Install the client:

    go get github.com/elastic/go-elasticsearch/v8
  2. Initialize the client:

    package main
    
    import (
        "fmt"
        "log"
        "github.com/elastic/go-elasticsearch/v8"
    )
    
    func main() {
        es, err := elasticsearch.NewDefaultClient()
        if err != nil {
            log.Fatalf("Error creating the client: %s", err)
        }
    
        // Ping Elasticsearch to ensure the connection works
        res, err := es.Info()
        if err != nil {
            log.Fatalf("Error getting response: %s", err)
        }
        defer res.Body.Close()
    
        fmt.Println(res)
    }

CRUD Operations

1. Create a Document

package main

import (
    "bytes"
    "context"
    "encoding/json"
    "log"

    "github.com/elastic/go-elasticsearch/v8"
)

func main() {
    es, _ := elasticsearch.NewDefaultClient()

    // Document to index
    doc := map[string]interface{}{
        "title":  "Elasticsearch with Go",
        "author": "Youssef",
        "tags":   []string{"go", "elasticsearch"},
    }

    // Serialize the document to JSON
    var buf bytes.Buffer
    json.NewEncoder(&buf).Encode(doc)

    // Index the document
    res, err := es.Index("books", &buf)
    if err != nil {
        log.Fatalf("Error indexing document: %s", err)
    }
    defer res.Body.Close()
    log.Println(res)
}

2. Read a Document

res, err := es.Get("books", "1") // "1" is the document ID
if err != nil {
    log.Fatalf("Error getting document: %s", err)
}
defer res.Body.Close()
log.Println(res)

3. Update a Document

updateDoc := map[string]interface{}{
    "doc": map[string]interface{}{
        "title": "Updated Title",
    },
}
var buf bytes.Buffer
json.NewEncoder(&buf).Encode(updateDoc)

res, err := es.Update("books", "1", &buf)
if err != nil {
    log.Fatalf("Error updating document: %s", err)
}
defer res.Body.Close()
log.Println(res)

4. Delete a Document

res, err := es.Delete("books", "1")
if err != nil {
    log.Fatalf("Error deleting document: %s", err)
}
defer res.Body.Close()
log.Println(res)

Search Operations

Simple Search Query

searchQuery := map[string]interface{}{
    "query": map[string]interface{}{
        "match": map[string]interface{}{
            "title": "Elasticsearch",
        },
    },
}
var buf bytes.Buffer
json.NewEncoder(&buf).Encode(searchQuery)

res, err := es.Search(
    es.Search.WithIndex("books"),
    es.Search.WithBody(&buf),
)
if err != nil {
    log.Fatalf("Error searching documents: %s", err)
}
defer res.Body.Close()
log.Println(res)

5. Best Practices