mazz / meilisearch-elixir

Elixir client for MeiliSearch

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MeiliSearch Elixir

Tests

A lightweight Meilisearch client for Elixir.

Installation

If available in Hex, the package can be installed by adding meilisearch to your list of dependencies in mix.exs:

def deps do
  [
    {:meilisearch, "~> 0.20.0"}
  ]
end

Usage

# Create Index
Meilisearch.Index.create("index_name")

# Create Index and set primary key
Meilisearch.Index.create("index_name", primary_key: "key_name")

# Insert documents
documents = [
  %{
    "id" => 1,
    "tagline" => "In space no one can hear you scream",
    "title" => "Alien"
  },
  %{
    "id" => 2,
    "tagline" => "You'll never go in the water again",
    "title" => "Jaws"
  },
  %{
    "id" => 3,
    "tagline" => "Be Afraid. Be very afraid.",
    "title" => "The Fly"
  }
]
Meilisearch.Document.add_or_replace("index_name", documents)

# Search
Meilisearch.Search.search("water")

Async Tasks

Meilisearch enqueues most of the commands you execute.

Because of this, it might at times be necessary to "wait" until Meilisearch has finished processing the given task before you can perform further operations.

For example, when adding Documents, the return object is:

{:ok, %{
    "taskUid" => 1,
    "indexUid" => "movies",
    "status" => "enqueued",
    "type" => "documentAdditionOrUpdate",
    "enqueuedAt" => "2021-08-11T09:25:53.000000Z"
}}

In order to block your program while you wait for the resolution of this enqueued task, you can use the Tasks.await_result/2 method, which will continue to execute get requests to the Tasks endpoint until the status of the response is either succeeded or failed.

Migrating from 0.20.0

Most if not all endpoints in Meilisearch now return the status of an enqueued task.

Whereas before methods like Indexes.create would immediately return information on the created index, now it is necessary to wait for the task to be resolved before accessing or further modifying a given entity - for this, use the Tasks.await_result/2 function.

Because of this, some functions do not return errors as expected - notably, Indexes.create when creating indexes with duplicate uid's, as well as Indexes.delete and Indexes.update in some cases.

Make sure to check the finalized task for errors that might have been caused by these methods (using Task.await_result/2).

As well - the Settings API has changed. Whereas before one could access facetableAttributes, now this setting has been renamed filterableAttributes. In turn, facets in the Settings endpoint simply refers to the maxValuesPerFacet setting.

Available Modules

  • Index
  • Health
  • Stats
  • Version
  • Documents
  • Search
  • Tasks
  • Keys
  • Settings

Config

Client settings can be configured in your application config or with environment variables.

Note: environment variables will override values in the application config.

Application Config

config :meilisearch,
  endpoint: "http://127.0.0.1:7700",
  api_key: "test_api_key"

Environment Variables

$ MEILISEARCH_ENDPOINT=http://localhost:7700 MEILISEARCH_API_KEY=test_api_key mix test

Compatibility

The 0.30.X versions of this client have been teseted against the following versions of Meilisearch:

  • v0.30.0

Development

You will need Meilisearch running locally for development and testing. You can do this via Docker:

$ docker run -it -rm -p 7700:7700 getmeili/meilisearch:v0.30.0 meilisearch --master-key=test_api_key --no-analytics

License

meilisearch-elixir is released under the MIT license. Please refer to LICENSE for details.

About

Elixir client for MeiliSearch

License:MIT License


Languages

Language:Elixir 99.5%Language:Shell 0.5%