This project contains an example that showcases different features from the official Go Client for Elasticsearch

Overview

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": [
            "Action",
            "Fantasy"
        ],
        "image_url": "http://ia.media-imdb.com/images/M/MV5BMTk2NTI1MTU4N15BMl5BanBnXkFtZTcwODg0OTY0Nw@@._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{
		"http://localhost:9200",
	},
})
if err != nil {
	panic(err)
}

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:

GET movies/_doc/

You Know, for Search

Obviously, this project couldn't leave behind an example of a search. The implemented search look for all the best action movies from Keanu Reeves from 1995 to 2005. This search is the equivalent to:

GET movies/_search
{
  "query": {
    "bool": {
      "must": {
        "match": {
          "Actors.en": "keanu reeves"
        }
      },
      "filter": [
        {
          "term": {
            "Genres.keyword": "Action"
          }
        },
        {"range": {
          "Rating": {
            "gte": 7.0
          }
        }},
        {"range": {
          "Year": {
            "gte": 1995,
            "lte": 2005
          }
        }}
      ]
    }
  }
}

Aggregation Analytics

Finally, the project also runs a very interesting aggregation to find out the top five genres and their respective movie counts. Just like you would do with:

GET movies/_search
{
  "size": 0,
  "aggs": {
    "movieCountPerGenre": {
      "terms": {
        "field": "Genres.keyword",
        "size": 5
      }
    }
  }
}

License

This project is licensed under the Apache 2.0 License.

You might also like...
Example code for Go generics

go-generics-example Example code for Go generics. Usage $ go build -gcflags=-G=3 Requirements Go 1.17 or later Advertise Go 言語にやってくる Generics は我々に何をも

gormuuid array field example

gormuuid - Examples An testing repo for https://github.com/ubgo/gormuuid module How to test Install the postgres and create a new database testdb Upda

A Go utility to convert Go example tests into jupyter notebooks.

go2colab Scientists (my main project's users) love jupyter notebook tutorials pkg.dev.go's runnable playground doesn't support file IO but I love exam

Cpu-profiling - Basic example of CPU Profiling in Golang which shows the bottlenecks and how much time is spent per function

cpu-profiling Basic example of CPU Profiling in Golang which shows the bottlenec

Simple project to demonstrate the loading of eBPF programs via florianl/go-tc.

tc-skeleton Simple project to demonstrate the loading of eBPF programs via florianl/go-tc.

MemBi is all the members of bugbounty and infosec project.
MemBi is all the members of bugbounty and infosec project.

Member of Bugbounty and Infosec 🤔 What is MemBi? MemBi is all the members of bugbounty and infosec project. If you don't know who to follow, see! 🌏

This project provides some working examples using Go and Hotwire Turbo.

hotwire-golang-website This project provides some working examples using Go the hotwire/turbo library published by basecamp.

This is an open source project for commonly used functions for the Go programming language.

Common Functions This is an open source project for commonly used functions for the Go programming language. This package need = go 1.3 Code Conventi

This is a Pub/Sub for the Watermill project which uses the Bolt database.
This is a Pub/Sub for the Watermill project which uses the Bolt database.

Watermill Bolt Pub/Sub This is a Pub/Sub for the Watermill project which uses the Bolt database.

Owner
Ricardo Ferreira
Ricardo Ferreira
An idiomatic Go query builder for ElasticSearch

esquery A non-obtrusive, idiomatic and easy-to-use query and aggregation builder for the official Go client for ElasticSearch. Table of Contents Descr

Aqua Security 258 Jan 5, 2023
randstr is a module that contains functions for generating random strings.

randstr is a module that contains functions for generating random strings. The functions in this module uses the crypto/rand package. Installa

Henrik Christensen 3 Nov 13, 2021
A lib of golang which contains many funny api;

A lib of golang which contains many funny api; I created it cause I could not find these more-effient apis from other repo.

acehinnnqru 1 Oct 27, 2021
Creates Prometheus Metrics for PolicyReports and ClusterPolicyReports. It also sends PolicyReportResults to different Targets like Grafana Loki or Slack

PolicyReporter Motivation Kyverno ships with two types of validation. You can either enforce a rule or audit it. If you don't want to block developers

Frank Jogeleit 0 Aug 6, 2021
Yet another StructTag, with some features liked cache and alias.

Yet another StructTag, with some features liked cache and alias.

Ryan Liu 2 Nov 1, 2021
A set of instrumentation features for collecting relevant tracing data as well as secure an application

Go Agent goagent provides a set of instrumentation features for collecting relevant tracing data as well as secure an application by blocking requests

null 0 Dec 21, 2021
xgen generator with extend features

XGen Plus Generator Introduction xgen is a library written in pure Go providing a set of functions that allow you to parse XSD (XML schema definition)

ZaharovII 0 Dec 8, 2021
A rule-based tunnel in Go with experimental features.

This repository is for archiving only Experimental-Clash A rule-based tunnel in Go with experimental features. Features Local HTTP/HTTPS/SOCKS server

夜棂依 4 Dec 3, 2021
A library that provides Go Generics friendly "optional" features.

go-optional A library that provides Go Generics friendly "optional" features. Synopsis some := optional.Some[int](123) fmt.Printf("%v\n", some.IsSome(

moznion 52 Dec 20, 2022
Like tools/cmd/stringer with bitmask features

Bitmasker Bitmasker is a tool used to automate the creation of helper methods when dealing with bitmask-type constant flags. Given the name of an unsi

Go Curses 0 Nov 25, 2021