karan

karan

How to implement Meilisearch in an Elixir project?

I am trying to implement Meilisearch in my elixir phoenix project, anyone has done this before any suggestions you have. MeiliSearch Elixir — meilisearch v0.20.0.

Most Liked

dmitriid

dmitriid

I used it in a side project. Millisearch has a very simple API, so I just called that.

Two helper functions to get and post:

def get(url) do
    case HTTPoison.get(url, Authorization: "Bearer aaaaa") do
      {:ok, %{status_code: 200, body: body} = response} ->
        # Logger.debug(response)
        {:ok, 200, body |> Jason.decode!()}

      {:ok, %{status_code: other, body: body} = error} ->
        # Logger.error(error)
        {:error, other, body |> Jason.decode!()}

      {:error, %HTTPoison.Error{} = error} ->
        # Logger.error(Exception.format(:error, error))
        {:error, -1, Exception.format(:error, error)}

      other ->
        Logger.error(other)
        {:error, -1, :unknown}
    end
  end

  defp post(url, body) do
    case HTTPoison.post(url, Jason.encode!(body),
           Authorization: "Bearer aaaaa",
           "Content-Type": "application/json"
         ) do
      {:ok, %{status_code: code, body: body} = response}
      when code == 200 or code == 201 or code == 202 or code == 301 or code == 302 ->
        # Logger.debug(response)
        {:ok, code, body |> Jason.decode!()}

      {:ok, %{status_code: other, body: body} = error} ->
        # Logger.error(error)
        {:error, other, body |> Jason.decode!()}

      {:error, %HTTPoison.Error{} = error} ->
        # Logger.error(Exception.format(:error, error))
        {:error, -1, Exception.format(:error, error)}

      other ->
        # Logger.error(other)
        {:error, -1, :unknown}
    end
  end

And then.

Creating an index:

def create_index(index) do
    case get("http://127.0.0.1:7700/indexes/#{index}/stats") do
      {:ok, _, response} = ok -> # the index is there
        ok

      {:error, _, _} = e ->
        post("http://127.0.0.1:7700/indexes", %{uid: index, primaryKey: :id})
    end
  end

Re-indexing:

defp reindex(documents, index) do
    post("http://127.0.0.1:7700/indexes/#{index}/documents", documents)
end

Search

def search(index, search, opts) do
    limit = opts[:limit] || 20
    page = opts[:page] || 0

    case post("http://127.0.0.1:7700/indexes/#{index}/search", %{
           q: search,
           limit: limit,
           offset: page * limit
         }) do
      {:error, _, e} ->
        # handle error

      {:ok, _, result} ->
         # do something with the result
    end
end

This is basically all you need in most cases

dimitarvp

dimitarvp

Just a note, I edited your title as you had a typo in it: Milisearch → Meilisearch

BTW have in mind that this library, while very good (I keep hearing), is not a replacement for an entire search cluster, should you predict that you will need one in the future. So know your tradeoffs.

(Though they have announced a cloud offering some time ago as well so that may be changing.)

brunoocasali

brunoocasali

I see what you meant, thanks!

I’m a member of Meilisearch and I query one of my colleagues about that subject…

Here you can see public discussions about it:

We’re working on this matter and I would love to had your insights about your use cases there!

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement