elasticsearch v0.1.0 Elasticsearch View Source

An Elixir interface to the Elasticsearch JSON API.

Configuration

You can customize the API module used by this module to make requests to the Elasticsearch API. (Default: Elasticsearch.API.HTTP)

config :elasticsearch,
  api_module: MyApp.CustomAPI

Link to this section Summary

Functions

Deletes data at a given Elasticsearch URL

Same as delete/1, but returns the response and raises errors

Deletes a document from a given index

Gets the contents of a path from the Elasticsearch API

The same as get/1, but returns the response instead of a tuple. Raises on errors

Posts data or queries to a given Elasticsearch path. If you want to execute an Elasticsearch.Query, see execute/1 instead

The same as post/1, but returns the response. Raises on errors

Puts data to a given Elasticsearch API path

The same as put/2, but returns the response instead of a tuple. Raises on errors

Creates or updates a document in a given index

Same as put_document/2, but raises on errors

Waits for Elasticsearch to be available at the configured url

Link to this section Types

Link to this type response() View Source
response() :: {:ok, map()} | {:error, Elasticsearch.Exception.t()}

Link to this section Functions

Deletes data at a given Elasticsearch URL.

Examples

iex> Elasticsearch.Index.create_from_file("posts", "test/support/settings/posts.json")
...> Elasticsearch.delete("/posts")
{:ok, %{"acknowledged" => true}}

It returns an error if the given resource does not exist.

iex> Elasticsearch.delete("/nonexistent")
{:error,
 %Elasticsearch.Exception{col: nil, line: nil,
  message: "no such index", query: nil,
  raw: %{"error" => %{"index" => "nonexistent",
      "index_uuid" => "_na_", "reason" => "no such index",
      "resource.id" => "nonexistent",
      "resource.type" => "index_or_alias",
      "root_cause" => [%{"index" => "nonexistent",
         "index_uuid" => "_na_", "reason" => "no such index",
         "resource.id" => "nonexistent",
         "resource.type" => "index_or_alias",
         "type" => "index_not_found_exception"}],
      "type" => "index_not_found_exception"}, "status" => 404},
  status: 404, type: "index_not_found_exception"}}
Link to this function delete!(url) View Source
delete!(String.t()) :: map()

Same as delete/1, but returns the response and raises errors.

Examples

iex> Elasticsearch.Index.create_from_file("posts", "test/support/settings/posts.json")
...> Elasticsearch.delete!("/posts")
%{"acknowledged" => true}

Raises an error if the resource is invalid.

iex> Elasticsearch.delete!("/nonexistent")
** (Elasticsearch.Exception) (index_not_found_exception) no such index
Link to this function delete_document(document, index) View Source
delete_document(Elasticsearch.Document.t(), String.t()) :: response()

Deletes a document from a given index.

The document must implement the Elasticsearch.Document protocol.

Link to this function delete_document!(document, index) View Source
delete_document!(Elasticsearch.Document.t(), String.t()) :: map()

Same as delete_document/2, but raises on errors.

Gets the contents of a path from the Elasticsearch API.

Examples

iex> {:ok, resp} = Elasticsearch.get("/_cat/health?format=json")
...> is_list(resp)
true

iex> Elasticsearch.get("/nonexistent")
{:error,
 %Elasticsearch.Exception{col: nil, line: nil,
  message: "no such index", query: nil,
  raw: %{"error" => %{"index" => "nonexistent",
      "index_uuid" => "_na_", "reason" => "no such index",
      "resource.id" => "nonexistent",
      "resource.type" => "index_or_alias",
      "root_cause" => [%{"index" => "nonexistent",
         "index_uuid" => "_na_", "reason" => "no such index",
         "resource.id" => "nonexistent",
         "resource.type" => "index_or_alias",
         "type" => "index_not_found_exception"}],
      "type" => "index_not_found_exception"}, "status" => 404},
  status: 404, type: "index_not_found_exception"}}

The same as get/1, but returns the response instead of a tuple. Raises on errors.

Examples

iex> resp = Elasticsearch.get!("/_cat/health?format=json")
...> is_list(resp)
true

iex> Elasticsearch.get!("/nonexistent")
** (Elasticsearch.Exception) (index_not_found_exception) no such index
Link to this function post(url, data) View Source
post(String.t(), map()) :: response()

Posts data or queries to a given Elasticsearch path. If you want to execute an Elasticsearch.Query, see execute/1 instead.

Examples

iex> Elasticsearch.Index.create_from_file("posts", "test/support/settings/posts.json")
...> query = %{"query" => %{"match_all" => %{}}}
...> {:ok, resp} = Elasticsearch.post("/posts/_search", query)
...> resp["hits"]["hits"]
[]
Link to this function post!(url, data) View Source
post!(String.t(), map()) :: map()

The same as post/1, but returns the response. Raises on errors.

Examples

iex> Elasticsearch.Index.create_from_file("posts", "test/support/settings/posts.json")
...> query = %{"query" => %{"match_all" => %{}}}
...> resp = Elasticsearch.post!("/posts/_search", query)
...> is_map(resp)
true

Raises an error if the path is invalid or another error occurs:

iex> query = %{"query" => %{"match_all" => %{}}}
...> Elasticsearch.post!("/nonexistent/_search", query)
** (Elasticsearch.Exception) (index_not_found_exception) no such index
Link to this function put(url, data) View Source
put(String.t(), map() | binary()) :: response()

Puts data to a given Elasticsearch API path.

Examples

iex> Elasticsearch.Index.create_from_file("posts-1", "test/support/settings/posts.json")
...> Elasticsearch.put("/posts-1/post/id", %{"title" => "title", "author" => "author"})
{:ok,
  %{"_id" => "id", "_index" => "posts-1",
    "_shards" => %{"failed" => 0, "successful" => 1, "total" => 2},
    "_type" => "post", "_version" => 1, "created" => true,
    "result" => "created"}}

iex> Elasticsearch.put("/bad/url", %{"title" => "title", "author" => "author"})
{:error,
 %Elasticsearch.Exception{col: nil, line: nil,
  message: "No handler found for uri [/bad/url] and method [PUT]",
  query: nil, raw: nil, status: nil, type: nil}}
Link to this function put!(url, data) View Source
put!(String.t(), map()) :: map()

The same as put/2, but returns the response instead of a tuple. Raises on errors.

Examples

iex> Elasticsearch.Index.create_from_file("posts", "test/support/settings/posts.json")
...> Elasticsearch.put!("/posts/post/id", %{"name" => "name", "author" => "author"})
%{"_id" => "id", "_index" => "posts",
  "_shards" => %{"failed" => 0, "successful" => 1, "total" => 2},
  "_type" => "post", "_version" => 1, "created" => true,
  "result" => "created"}

iex> Elasticsearch.put!("/bad/url", %{"data" => "here"})
** (Elasticsearch.Exception) No handler found for uri [/bad/url] and method [PUT]
Link to this function put_document(document, index) View Source
put_document(Elasticsearch.Document.t(), String.t()) :: response()

Creates or updates a document in a given index.

The document must implement the Elasticsearch.Document protocol.

Example

iex> Elasticsearch.Index.create_from_file("posts-1", "test/support/settings/posts.json")
...> struct = %Post{id: 123, title: "Post", author: "Author"}
...> Elasticsearch.put_document(struct, "posts-1")
{:ok,
 %{"_id" => "123", "_index" => "posts-1",
   "_shards" => %{"failed" => 0, "successful" => 1, "total" => 2},
   "_type" => "post", "_version" => 1, "created" => true,
   "result" => "created"}}
Link to this function put_document!(document, index) View Source
put_document!(Elasticsearch.Document.t(), String.t()) :: map()

Same as put_document/2, but raises on errors.

Link to this function wait_for_boot(tries, count \\ 0) View Source

Waits for Elasticsearch to be available at the configured url.

It will try a given number of times, with 1sec delay between tries.

Example

iex> {:ok, resp} = Elasticsearch.wait_for_boot(15)
...> is_list(resp)
true