GithubHelp home page GithubHelp logo

snap's Introduction

Snap

Hex pm

Snap is an Elasticsearch client. It provides a flexible, performant API on top of your Elasticsearch cluster, supporting high level features like versioned index management, while also providing a convenient interface into low level operations.

See the full API docs.

Features

  • Versioned index management with zero-downtime hotswapping (compatible with elasticsearch)
  • Streaming bulk operations
  • Connection pooling
  • Telemetry events

Installation

The package can be installed by adding snap to your list of dependencies in mix.exs:

def deps do
  [
    {:snap, "~> 0.6"},
    {:finch, "~> 0.8"}, # By default, Snap uses Finch to make HTTP requests
  ]
end

Snap supports Elixir 1.11 or later.

Usage

Implement your own cluster module, similar to an Ecto.Repo:

defmodule MyApp.Cluster do
  use Snap.Cluster, otp_app: :my_app
end

Configure it:

config :my_app, MyApp.Cluster,
  url: "http://localhost:9200",
  username: "my_username",
  password: "my_password"

Then wire it into your application supervisor:

def start(_type, _args) do
  children = [
    {MyApp.Cluster, []}
  ]

  opts = [strategy: :one_for_one, name: MyApp.Supervisor]
  Supervisor.start_link(children, opts)
end

Now you can perform operations on your cluster:

{:ok, %{"count" => count}} = MyApp.Cluster.get("/my-index/_count")

Testing

If you want to test your app that uses this library, but don't want to have integration tests with a Elasticsearch instance running in you local dev environment, you can mock the responses using a custom HTTP client adapter.

Supposing you are using mox, you can do something like this:

# in test_helper.exs
Mox.defmock(HTTPClientMock, for: Snap.HTTPClient)
Mox.stub(HTTPClientMock, :child_spec, fn _config -> :skip end)

# in config/test.exs
config :my_app, MyApp.Cluster, http_client_adapter: HTTPClientMock

# in a test file
Mox.expect(HTTPClientMock, :request, fn _cluster, _method, _url, _headers, _body, _opts
  body = "{}" # valid json
  {:ok, %Snap.HTTPClient.Response{status: 200, headers: [], body: body}}
end)

See the API documentation for more advanced features.

snap's People

Contributors

tomtaylor avatar jotaviobiondo avatar megalithic avatar danielxvu avatar edds avatar gabrielpra1 avatar jeroenvisser101 avatar hauptbenutzer avatar tt67wq 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.