elasticsearch v0.1.1 Elasticsearch.Index View Source

Functions for manipulating Elasticsearch indexes.

Link to this section Summary

Functions

Assigns an alias to a given index, simultaneously removing it from prior indexes, with zero downtime

Generates a name for an index that will be aliased to a given alias. Similar to migrations, the name will contain a timestamp

Removes indexes starting with the given prefix, keeping a certain number

Creates an index with the given name from either a JSON string or Elixir map

Creates an index with the given name, with settings loaded from a JSON file

Creates an index using a zero-downtime hot-swap technique

Gets the most recent index name with the given prefix

Refreshes a given index with recently added data

Same as refresh/1, but raises an error on failure

Returns all indexes which start with a given string

Link to this section Functions

Link to this function alias(name, alias) View Source
alias(String.t(), String.t()) ::
  :ok |
  {:error, Elasticsearch.Exception.t()}

Assigns an alias to a given index, simultaneously removing it from prior indexes, with zero downtime.

Example

iex> Index.create_from_file("posts-1", "test/support/settings/posts.json")
...> Index.alias("posts-1", "posts")
:ok
Link to this function build_name(alias) View Source
build_name(String.t() | atom()) :: String.t()

Generates a name for an index that will be aliased to a given alias. Similar to migrations, the name will contain a timestamp.

Example

Index.build_name("main")
# => "main-1509581256"
Link to this function clean_starting_with(prefix, num_to_keep) View Source
clean_starting_with(String.t(), integer()) ::
  :ok |
  {:error, [Elasticsearch.Exception.t()]}

Removes indexes starting with the given prefix, keeping a certain number.

Can be used to garbage collect old indexes that are no longer used.

Examples

If there is only one index, and num_to_keep is >= 1, the index is not deleted.

iex> Index.create_from_file("posts-1", "test/support/settings/posts.json")
...> Index.clean_starting_with("posts", 1)
...> Index.starting_with("posts")
{:ok, ["posts-1"]}

If num_to_keep is less than the number of indexes, the older indexes are deleted.

iex> Index.create_from_file("posts-1", "test/support/settings/posts.json")
...> Index.clean_starting_with("posts", 0)
...> Index.starting_with("posts")
{:ok, []}
Link to this function create(name, settings) View Source
create(String.t(), map() | String.t()) ::
  :ok |
  {:error, Elasticsearch.Exception.t()}

Creates an index with the given name from either a JSON string or Elixir map.

Examples

iex> Index.create("posts-1", "{}")
:ok
Link to this function create_from_file(name, file) View Source
create_from_file(String.t(), Path.t()) ::
  :ok |
  {:error, File.posix()} |
  {:error, Elasticsearch.Exception.t()}

Creates an index with the given name, with settings loaded from a JSON file.

Example

iex> Index.create_from_file("posts-1", "test/support/settings/posts.json")
:ok

iex> Index.create_from_file("posts-1", "nonexistent.json")
{:error, :enoent}

The posts.json file contains regular index settings as described in the Elasticsearch documentation:

{
  "mappings": {
    "post": {
      "properties": {
        "title": {
          "type": "string"
        },
        "author": {
          "type": "string"
        }
      }
    }
  }
}
Link to this function hot_swap(alias, settings_file, store, sources) View Source
hot_swap(String.t() | atom(), String.t(), Elasticsearch.Store.t(), list()) ::
  :ok |
  {:error, Elasticsearch.Exception.t()}

Creates an index using a zero-downtime hot-swap technique.

  1. Build an index for the given alias, with a timestamp: alias-12323123
  2. Bulk upload data to that index using store and sources.
  3. Alias the alias to alias-12323123.
  4. Remove old indexes beginning with alias.
  5. Refresh alias-12323123.

This allows an old index to be served while a new index for alias is built.

Example

iex> file = "test/support/settings/posts.json"
...> store = Elasticsearch.Test.Store
...> Index.hot_swap("posts", file, store, [Post])
:ok
Link to this function latest_starting_with(prefix) View Source
latest_starting_with(String.t() | atom()) ::
  {:ok, String.t()} |
  {:error, :not_found} |
  {:error, Elasticsearch.Exception.t()}

Gets the most recent index name with the given prefix.

Examples

iex> Index.create_from_file("posts-1", "test/support/settings/posts.json")
...> Index.create_from_file("posts-2", "test/support/settings/posts.json")
...> Index.latest_starting_with("posts")
{:ok, "posts-2"}

If there are no indexes matching that prefix:

iex> Index.latest_starting_with("nonexistent")
{:error, :not_found}
Link to this function refresh(name) View Source
refresh(String.t()) :: :ok | {:error, Elasticsearch.Exception.t()}

Refreshes a given index with recently added data.

Example

iex> Index.create_from_file("posts-1", "test/support/settings/posts.json")
...> Index.refresh("posts-1")
:ok
Link to this function refresh!(name) View Source
refresh!(String.t()) :: :ok

Same as refresh/1, but raises an error on failure.

Examples

iex> Index.create_from_file("posts-1", "test/support/settings/posts.json")
...> Index.refresh!("posts-1")
:ok

iex> Index.refresh!("nonexistent")
** (Elasticsearch.Exception) (index_not_found_exception) no such index
Link to this function starting_with(prefix) View Source
starting_with(String.t() | atom()) ::
  {:ok, [String.t()]} |
  {:error, Elasticsearch.Exception.t()}

Returns all indexes which start with a given string.

Example

iex> Index.create_from_file("posts-1", "test/support/settings/posts.json")
...> Index.starting_with("posts")
{:ok, ["posts-1"]}