elasticsearch v1.0.0 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
Same as refresh/1
, but raises an error on failure
Refreshes a given index with recently added data
Returns all indexes which start with a given string
Link to this section Functions
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
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"
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, []}
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
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"
}
}
}
}
}
hot_swap(Cluster.t(), alias :: String.t() | atom()) :: :ok | {:error, Elasticsearch.Exception.t()}
Creates an index using a zero-downtime hot-swap technique.
- Build an index for the given
alias
, with a timestamp:alias-12323123
- Bulk upload data to that index using
store
andsources
. - Alias the
alias
toalias-12323123
. - Remove old indexes beginning with
alias
. - 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
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}
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
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
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"]}