项目作者: werbitzky

项目描述 :
A simple Elasticsearch REST client written in Elixir.
高级语言: Elixir
项目地址: git://github.com/werbitzky/elastix.git
创建时间: 2015-07-09T20:50:07Z
项目社区:https://github.com/werbitzky/elastix

开源协议:

下载


Elastix

Build Status
Hex Version
Hex Docs
Hex Downloads
WTFPL
Last Updated

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:

  1. def deps do
  2. [
  3. {:elastix, ">= 0.0.0"}
  4. ]
  5. end

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

Examples

Creating an Elasticsearch index

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

Map, Index, Search and Delete

  1. elastic_url = "http://localhost:9200"
  2. data = %{
  3. user: "kimchy",
  4. post_date: "2009-11-15T14:12:12",
  5. message: "trying out Elastix"
  6. }
  7. mapping = %{
  8. properties: %{
  9. user: %{type: "text"},
  10. post_date: %{type: "date"},
  11. message: %{type: "text"}
  12. }
  13. }
  14. Elastix.Mapping.put(elastic_url, "twitter", "tweet", mapping)
  15. Elastix.Document.index(elastic_url, "twitter", "tweet", "42", data)
  16. Elastix.Search.search(elastic_url, "twitter", ["tweet"], %{})
  17. 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)
  • httpoison_options configuration directly passed to httpoison methods. Same options that can be passed on config file
  1. lines = [
  2. %{index: %{_id: "1"}},
  3. %{field: "value1"},
  4. %{index: %{_id: "2"}},
  5. %{field: "value2"}
  6. ]
  7. Elastix.Bulk.post(elastic_url, lines, index: "my_index", type: "my_type", httpoison_options: [timeout: 180_000])
  8. # You can also send raw data:
  9. data = Enum.map(lines, fn line -> Poison.encode!(line) <> "\n" end)
  10. Elastix.Bulk.post_raw(elastic_url, data, index: "my_index", type: "my_type")

Configuration

Shield

  1. config :elastix,
  2. shield: true,
  3. username: "username",
  4. password: "password",

Poison (or any other JSON library) and HTTPoison

  1. config :elastix,
  2. json_options: [keys: :atoms!],
  3. 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

  1. config :elastix,
  2. 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:

  1. defmodule MyModule do
  2. def add_aws_signature(request, region) do
  3. [{"Authorization", generate_aws_signature(request, region)} | request.headers]
  4. end
  5. defp generate_aws_signature(request, region) do
  6. # See: https://github.com/bryanjos/aws_auth or similar
  7. end
  8. end

Running tests

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

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

Then clone the repo and fetch its dependencies:

  1. $ git clone git@github.com:werbitzky/elastix.git
  2. $ cd elastix
  3. $ mix deps.get
  4. $ mix test