Elasticsearch.Cluster (elasticsearch v1.1.0) View Source

Defines and holds configuration for your Elasticsearch cluster.

defmodule MyApp.ElasticsearchCluster do
  use Elasticsearch.Cluster
end

Once you have created your cluster, add it to your application's supervision tree:

children = [
  MyApp.ElasticsearchCluster
]

Finally, you can issue requests to Elasticsearch using it.

Elasticsearch.get(MyApp.ElasticsearchCluster, "/_cat/health")

Configuration

Clusters can be configured in several ways.

Via Mix

Clusters can read configuration from the mix config, if you pass the :otp_app option:

defmodule MyApp.ElasticsearchCluster do
  use Elasticsearch.Cluster, otp_app: :my_app
end

# In your config/config.exs...
config :my_app, MyApp.ElasticsearchCluster,
  url: "http://localhost:9200",
  # ...

Via init/1

When a cluster starts, you can override its configuration via the init/1 callback. This is a good place to read from environment variables.

defmodule MyApp.ElasticsearchCluster do
  use Elasticsearch.Cluster

  def init(config) do
    config =
      config
      |> Map.put(:url, System.get_env("ELASTICSEARCH_URL"))
      # ...

    {:ok, config}
  end
end

Via start_link/1

You can also pass configuration into the cluster directly when you start it with start_link/1.

MyApp.Elasticsearch.start_link(url: "http://localhost:9200", ...)

Configuration Options

The following options are available for configuration.

  • :url - The URL at which the Elasticsearch cluster is available.

  • :api - The API module to use to communicate with Elasticsearch. Must implement the Elasticsearch.API behaviour.

  • :bulk_page_size - When creating indexes via bulk upload, how many documents to include per request.

  • :bulk_wait_interval - The number of milliseconds to wait between bulk upload requests.

  • :indexes - A map of indexes. Used by mix elasticsearch.build to build indexes.

    • :settings: The file path of the JSON settings for the index.
    • :store: An Elasticsearch.Store module to use to load data for the index.
    • :sources: A list of sources you want to load for this index.
  • :json_library (Optional) - The JSON library to use. (E.g. Poison or Jason)

  • :username (Optional) - The HTTP Basic username for the Elasticsearch endpoint, if any.

  • :password (Optional) - The HTTP Basic password for the Elasticsearch endpoint, if any.

  • :default_headers (Optional) - A list of default headers to send with the each request.

  • :default_options (Optional) - A list of default HTTPoison/Hackney options to send with each request.

Configuration Example

%{
  api: Elasticsearch.API.HTTP,
  json_library: Poison,
  url: "http://localhost:9200",
  username: "username",
  password: "password",
  default_headers: [{"authorization", "custom-value"}],
  default_options: [ssl: [{:versions, [:'tlsv1.2']}],
  indexes: %{
    posts: %{
      settings: "priv/elasticsearch/posts.json",
      store: MyApp.ElasticsearchStore,
      sources: [MyApp.Post],
      bulk_page_size: 5000,
      bulk_wait_interval: 5000
    }
  }
}

Link to this section Summary

Types

Defines valid configuration for a cluster.

t()

A cluster is either a module defined with Elasticsearch.Cluster, or a map that has all the required configuration keys.

Link to this section Types

Specs

config() :: %{
  :url => String.t(),
  :api => module(),
  optional(:json_library) => module(),
  optional(:username) => String.t(),
  optional(:password) => String.t(),
  optional(:default_headers) => [{String.t(), String.t()}],
  optional(:default_options) => Keyword.t(),
  optional(:indexes) => %{
    optional(atom()) => %{
      settings: Path.t(),
      store: module(),
      sources: [module()],
      bulk_page_size: integer(),
      bulk_wait_interval: integer()
    }
  }
}

Defines valid configuration for a cluster.

Specs

t() :: module() | config()

A cluster is either a module defined with Elasticsearch.Cluster, or a map that has all the required configuration keys.