Dowser. Elasticsearch. Repo
(Dowser.Elasticsearch v0.6.0)
View Source
An Elasticsearch cluster an application talks to — a Dowser.Repo.
defmodule MyApp.Search do
use Dowser.Elasticsearch.Repo, otp_app: :my_app
end
# config/runtime.exs
config :my_app, MyApp.Search,
endpoint: System.fetch_env!("ELASTICSEARCH_URL"),
decoder: Dowser.Elasticsearch.Codec,
encoder: Dowser.Elasticsearch.Codec
# the repo used when a call names none
config :dowser_elasticsearch, default_repo: MyApp.Search
# application.ex
children = [MyApp.Search]Every API function takes the repo as its :repo option, and falls back to
the default repo without one:
Dowser.Elasticsearch.Search.search(%{query: %{match_all: %{}}}, index: "posts")
Dowser.Elasticsearch.Search.search(%{query: %{match_all: %{}}}, index: "logs-*", repo: MyApp.Logs)See Dowser.Repo for the configuration, init/2, and the HTTP verbs a repo
exposes (MyApp.Search.post("/_ml/trained_models/_search", query)).
The same module can be started again under other names — a cluster per tenant, say — and each process picks the instance it talks to:
{:ok, _pid} = MyApp.Search.start_link(name: :tenant_a, endpoint: "https://a.internal:9200")
MyApp.Search.put_dynamic_repo(:tenant_a)
Dowser.Elasticsearch.Search.search(query, index: "posts", repo: MyApp.Search)
Dowser.Elasticsearch.Search.search(query, index: "posts", repo: {MyApp.Search, :tenant_a})See "Static and dynamic repos" in Dowser.Repo.
Migrations
A repo's indices can be changed with migrations — mix dowser.gen.migration,
mix dowser.migrate; see Dowser.Migration and Dowser.Migrator. They run
through Dowser.Elasticsearch.Migration.Adapter.
Checking the engine
With verify: true, the repo checks at startup that its endpoint is an
Elasticsearch cluster, and fails to start otherwise, with
{:unexpected_engine, version}. GET / must carry no distribution (which
OpenSearch sets to "opensearch") and Elasticsearch's own signature: the
x-elastic-product: Elasticsearch header (7.14 and later), or its tagline
(earlier releases). engine/0 then returns what it found, e.g.
{:elasticsearch, "8.15.0"}. It is off by default: a cluster that is down
would stop the application from booting.
Summary
Functions
Checks that repo points at an Elasticsearch cluster, returning
{:ok, {:elasticsearch, version}}, or {:error, reason} —
{:unexpected_engine, version} for another engine, or why GET / failed.
Functions
@spec verify(Dowser.Repo.t()) :: {:ok, {:elasticsearch, String.t()}} | {:error, term()}
Checks that repo points at an Elasticsearch cluster, returning
{:ok, {:elasticsearch, version}}, or {:error, reason} —
{:unexpected_engine, version} for another engine, or why GET / failed.
repo is anything Dowser.Client takes as :repo.