View Source Meilisearch (meilisearch_ex v1.1.2)

A client for MeiliSearch. The following Modules are provided for interacting with Meilisearch:

usage

Usage

You can create a client when you needs it.

[endpoint: "https://search.mydomain.com", key: "replace_me"]
|> Meilisearch.Client.new()
|> Meilisearch.Health.get()

# %Meilisearch.Health{status: "available"}

But you can also start a client alongside your application to access it whenever you need it.

Meilisearch.start_link(:main, [endpoint: "https://search.mydomain.com", key: "replace_me"])

:main
|> Meilisearch.client()
|> Meilisearch.Health.get()

# %Meilisearch.Health{status: "available"}

Within a Phoenix app you would do like this:

defmodule MyApp.Application do
  # ...

  @impl true
  def start(_type, _args) do
    children = [
      # ...
      {Meilisearch, name: :search_admin, endpoint: "https://search.mydomain.com", key: "key_admin"},
      {Meilisearch, name: :search_user, endpoint: "https://search.mydomain.com", key: "key_user"}
    ]

    # ...
  end

  # ...
end

defmodule MyApp.MyContext do
  def create_search_index() do
    :search_admin
    |> Meilisearch.client()
    |> Meilisearch.Index.create(%{uid: "items", primaryKey: "id"})
  end

  def add_documents_to_search_index(documents) do
    :search_admin
    |> Meilisearch.client()
    |> Meilisearch.Document.create_or_replace("items", documents)
  end

  def search_document(query) do
    :search_user
    |> Meilisearch.client()
    |> Meilisearch.Search.search("items", %{q: query})
  end
end

Link to this section Summary

Functions

Returns a specification to start this module under a supervisor.

Link to this section Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

@spec client(atom()) :: Tesla.Client.t()
@spec start_link(atom(),
  endpoint: String.t(),
  key: String.t(),
  timeout: integer(),
  log_level: :info | :warn | :error
) :: :ignore | {:error, any()} | {:ok, pid()}