Dowser.OpenSearch.Repo (Dowser.OpenSearch v0.4.0)

View Source

An OpenSearch cluster an application talks to — a Dowser.Repo.

defmodule MyApp.Search do
  use Dowser.OpenSearch.Repo, otp_app: :my_app
end

# config/runtime.exs
config :my_app, MyApp.Search,
  endpoint: System.fetch_env!("OPENSEARCH_URL"),
  decoder: Dowser.OpenSearch.Codec,
  encoder: Dowser.OpenSearch.Codec

# the repo used when a call names none
config :dowser_opensearch, 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.OpenSearch.Search.search(%{query: %{match_all: %{}}}, index: "posts")
Dowser.OpenSearch.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("/_plugins/_ml/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.OpenSearch.Search.search(query, index: "posts", repo: MyApp.Search)

Dowser.OpenSearch.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.OpenSearch.Migration.Adapter.

Checking the engine

With verify: true, the repo checks at startup that its endpoint is an OpenSearch cluster — GET / reports "distribution": "opensearch" — and fails to start otherwise, with {:unexpected_engine, version}. engine/0 then returns what it found, e.g. {:opensearch, "2.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 OpenSearch cluster, returning {:ok, {:opensearch, version}}, or {:error, reason} — {:unexpected_engine, version} for another engine, or why GET / failed.

Functions

verify(repo)

@spec verify(Dowser.Repo.t()) :: {:ok, {:opensearch, String.t()}} | {:error, term()}

Checks that repo points at an OpenSearch cluster, returning {:ok, {:opensearch, version}}, or {:error, reason} — {:unexpected_engine, version} for another engine, or why GET / failed.

repo is anything Dowser.Client takes as :repo.