Giter Club home page Giter Club logo

elastix's Introduction

Elastix Hex Version Hex Downloads Build Status WTFPL

A DSL-free Elasticsearch client for Elixir.

Documentation

Even though the documentation is pretty scarce right now, we're working on improving it. If you want to help with that you're definitely welcome ๐Ÿค—

This README contains most of the information you should need to get started, if you can't find what you're looking for, either look at the tests or file an issue!

Installation

Add elastix to your list of dependencies in mix.exs:

def deps do
  [{:elastix, ">= 0.0.0"}]
end

Then run mix deps.get to fetch the new dependency.

Examples

Creating an Elasticsearch index

Elastix.Index.create("http://localhost:9200", "twitter", %{})

Map, Index, Search and Delete

elastic_url = "http://localhost:9200"

data = %{
    user: "kimchy",
    post_date: "2009-11-15T14:12:12",
    message: "trying out Elastix"
}

mapping = %{
  properties: %{
    user: %{type: "text"},
    post_date: %{type: "date"},
    message: %{type: "text"}
  }
}

Elastix.Mapping.put(elastic_url, "twitter", "tweet", mapping)
Elastix.Document.index(elastic_url, "twitter", "tweet", "42", data)
Elastix.Search.search(elastic_url, "twitter", ["tweet"], %{})
Elastix.Document.delete(elastic_url, "twitter", "tweet", "42")

Bulk requests

Bulk requests take as parameter a list of the lines you want to send to the _bulk endpoint.

You can also specify the following options:

  • index the index of the request
  • type the document type of the request. (you can't specify type without specifying index)
lines = [
  %{index: %{_id: "1"}},
  %{field: "value1"},
  %{index: %{_id: "2"}},
  %{field: "value2"}
]

Elastix.Bulk.post(elastic_url, lines, index: "my_index", type: "my_type")

# You can also send raw data:
data = Enum.map(lines, fn line -> Poison.encode!(line) <> "\n" end)
Elastix.Bulk.post_raw(elastic_url, data, index: "my_index", type: "my_type")

Configuration

config :elastix,
  shield: true,
  username: "username",
  password: "password",

Poison (or any other JSON library) and HTTPoison

config :elastix,
  json_options: [keys: :atoms!],
  httpoison_options: [hackney: [pool: :elastix_pool]]

Note that you can configure Elastix to use any JSON library, see the "Custom JSON codec" page for more info.

Custom headers

config :elastix,
  custom_headers: {MyModule, :add_aws_signature, ["us-east"]}

custom_headers must be a tuple of the type {Module, :function, [args]}, where :function is a function that should accept the request (a map of this type: %{method: String.t, headers: [], url: String.t, body: String.t}) as its first parameter and return a list of the headers you want to send:

defmodule MyModule do
  def add_aws_signature(request, region) do
    [{"Authorization", generate_aws_signature(request, region)} | request.headers]
  end

  defp generate_aws_signature(request, region) do
    # See: https://github.com/bryanjos/aws_auth or similar
  end
end

Running tests

You need Elasticsearch running locally on port 9200. A quick way of doing so is via Docker:

$ docker run -p 9200:9200 -it --rm elasticsearch:5.1.2

Then clone the repo and fetch its dependencies:

$ git clone [email protected]:werbitzky/elastix.git
$ cd elastix
$ mix deps.get
$ mix test

License

Copyright ยฉ 2017 El Werbitzky [email protected]

This work is free. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2, as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.

elastix's People

Contributors

werbitzky avatar evuez avatar rockwood avatar manukall avatar fabienhenon avatar briankereszturi avatar rodrigues avatar alanpeabody avatar bglusman avatar walterbm avatar yellowaj avatar bravely avatar mlewis033 avatar ontofractal avatar balexand avatar

Watchers

Simon Warde 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.