Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Meilisearch

livebooks/meilisearch.livemd

Meilisearch

Section

keys =
  Req.new(
    base_url: "http://localhost:7700",
    auth: {:bearer, System.fetch_env!("LB_MEILISEARCH_MASTER_KEY")}
  )
  |> Req.get!(url: "/keys")
use Pathex

search_key = Pathex.get(keys, path(:body / "results" / 0 / "key"))
Req.new(
  base_url: "http://localhost:7700",
  auth: {:bearer, System.fetch_env!("LB_MEILISEARCH_MASTER_KEY")}
)
|> Req.get!(url: "/indexes")
searchResult =
  Req.new(
    base_url: "http://localhost:7700",
    auth: {:bearer, search_key}
  )
  |> Req.post!(
    url: "/indexes/blog-posts/search",
    json: %{
      attributesToHighlight: ["*"],
      facets: [],
      highlightPreTag: "",
      highlightPostTag: "",
      limit: 21,
      offset: 0,
      q: "pc"
    }
  )
searchResult.body["estimatedTotalHits"]

Settings

Req.new(
  base_url: "http://localhost:7700",
  auth: {:bearer, System.fetch_env!("LB_MEILISEARCH_MASTER_KEY")}
)
|> Req.get!(url: "/indexes/blog-posts/settings")

Multi Search

search_results =
  Req.new(
    base_url: "http://localhost:7700",
    auth: {:bearer, search_key}
  )
  |> Req.post!(
    url: "/multi-search",
    json: %{
      queries: [
        %{
          indexUid: "blog-posts",
          q: "c",
          attributesToHighlight: ["*"],
          highlightPreTag: "",
          highlightPostTag: "",
          showRankingScore: true
        },
        %{
          indexUid: "blog-tags",
          q: "c",
          attributesToHighlight: ["*"],
          highlightPreTag: "",
          highlightPostTag: "",
          limit: 10
        },
        %{
          indexUid: "blog-authors",
          q: "c",
          attributesToHighlight: ["*"],
          highlightPreTag: "",
          highlightPostTag: "",
          limit: 10
        },
        %{
          indexUid: "routes",
          q: "c",
          attributesToHighlight: ["*"],
          highlightPreTag: "",
          highlightPostTag: "",
          limit: 10
        }
      ]
    }
  )
search_results.body["results"]
|> Enum.reduce({0, 0}, fn
  result, {total_hits, processing_time} ->
    total_hits = total_hits + result["estimatedTotalHits"]
    processing_time = max(processing_time, result["processingTimeMs"])
    {total_hits, processing_time}
end)

Filters

search_results =
  Req.new(
    base_url: "http://localhost:7700",
    auth: {:bearer, search_key}
  )
  |> Req.post!(
    url: "/indexes/blog-posts/search",
    json: %{
      attributesToHighlight: ["*"],
      facets: [],
      highlightPreTag: "",
      highlightPostTag: "",
      limit: 21,
      offset: 0,
      showRankingScore: true,
      q: "com",
      filter: "tags = \"Command Line Tools\" AND release_date > 1674777600"
    }
  )

Facets

search_results =
  Req.new(
    base_url: "http://localhost:7700",
    auth: {:bearer, search_key}
  )
  |> Req.post!(
    url: "/indexes/blog-posts/search",
    json: %{
      attributesToHighlight: ["*"],
      highlightPreTag: "",
      highlightPostTag: "",
      limit: 21,
      offset: 0,
      showRankingScore: true,
      q: "com",
      facets: [
        "tags"
      ],
      filter: "tags = \"Command Line Tools\" AND release_date > 1674777600"
    }
  )
stats =
  Req.new(
    base_url: "http://localhost:7700",
    auth: {:bearer, System.fetch_env!("LB_MEILISEARCH_MASTER_KEY")}
  )
  |> Req.get!(url: "/stats")

stats.body

Tasks

Req.new(
  base_url: "http://localhost:7700",
  auth: {:bearer, System.fetch_env!("LB_MEILISEARCH_MASTER_KEY")}
)
|> Req.get!(url: "/tasks")

Change Index

Req.new(
  base_url: "http://localhost:7700",
  auth: {:bearer, System.fetch_env!("LB_MEILISEARCH_MASTER_KEY")}
)
|> Req.get!(
  url: "/indexes/routes/search",
  json: %{
    attributesToHighlight: ~w(name),
    highlightPreTag: "",
    highlightPostTag: "",
    q: ""
  }
)