Git Product home page Git Product logo

elasticsearch-for-gophers's Introduction

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/<DOCUMENT_ID>

✅ 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.

elasticsearch-for-gophers's People

Contributors

riferrei avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.