Elasticsearch.Index (elasticsearch v1.1.0) 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 with the given name, with settings loaded from a map or a JSON file (see create_from_file/3).

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(cluster, name, alias)

View Source

Specs

alias(Cluster.t(), 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(Cluster, "posts-1", "test/support/settings/posts.json")
...> Index.alias(Cluster, "posts-1", "posts")
:ok

Specs

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(cluster, prefix, num_to_keep)

View Source

Specs

clean_starting_with(Cluster.t(), 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(Cluster, "posts-1", "test/support/settings/posts.json")
...> Index.clean_starting_with(Cluster, "posts", 1)
...> Index.starting_with(Cluster, "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(Cluster, "posts-1", "test/support/settings/posts.json")
...> Index.clean_starting_with(Cluster, "posts", 0)
...> Index.starting_with(Cluster, "posts")
{:ok, []}
Link to this function

create(cluster, name, settings)

View Source

Specs

create(Cluster.t(), 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(Cluster, "posts-1", "{}")
:ok
Link to this function

create_from_file(cluster, name, file)

View Source

Specs

create_from_file(Cluster.t(), 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(Cluster, "posts-1", "test/support/settings/posts.json")
:ok

iex> Index.create_from_file(Cluster, "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

create_from_settings(cluster, name, settings)

View Source

Specs

create_from_settings(Cluster.t(), String.t(), map() | Path.t()) ::
  :ok | {:error, File.posix()} | {:error, Elasticsearch.Exception.t()}

Creates an index with the given name, with settings loaded from a map or a JSON file (see create_from_file/3).

Example

iex> Index.create_from_settings(Cluster, "posts-1", %{})
:ok

iex> Index.create_from_settings(Cluster, "posts-1", "nonexistent.json")
{:error, :enoent}
Link to this function

hot_swap(cluster, alias)

View Source

Specs

hot_swap(Cluster.t(), alias :: String.t() | atom()) ::
  :ok | {:error, Elasticsearch.Exception.t()} | {:error, :enoent}

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> Index.hot_swap(Cluster, "posts")
:ok
Link to this function

latest_starting_with(cluster, prefix)

View Source

Specs

latest_starting_with(Cluster.t(), 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(Cluster, "posts-1", "test/support/settings/posts.json")
...> Index.create_from_file(Cluster, "posts-2", "test/support/settings/posts.json")
...> Index.latest_starting_with(Cluster, "posts")
{:ok, "posts-2"}

If there are no indexes matching that prefix:

iex> Index.latest_starting_with(Cluster, "nonexistent")
{:error, :not_found}

Specs

refresh(Cluster.t(), String.t()) :: :ok | {:error, Elasticsearch.Exception.t()}

Refreshes a given index with recently added data.

Example

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

Specs

refresh!(Cluster.t(), String.t()) :: :ok

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

Examples

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

iex> Index.refresh!(Cluster, "nonexistent")
** (Elasticsearch.Exception) (index_not_found_exception) no such index
Link to this function

starting_with(cluster, prefix)

View Source

Specs

starting_with(Cluster.t(), 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(Cluster, "posts-1", "test/support/settings/posts.json")
...> Index.starting_with(Cluster, "posts")
{:ok, ["posts-1"]}