forked from AkkomaGang/akkoma
meilisearch: enable using search_key
Using only the admin key works as well currently and Akkoma needs to know the admin key to be able to add new entries etc. However the Meilisearch key descriptions suggest the admin key is not supposed to be used for searches, so let’s not. For compatibility with existings configs, search_key remains optional.
This commit is contained in:
parent
59685e25d2
commit
fc7e07f424
3 changed files with 34 additions and 16 deletions
|
@ -14,6 +14,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
||||||
## Added
|
## Added
|
||||||
- Implement [FEP-67ff](https://codeberg.org/fediverse/fep/src/branch/main/fep/67ff/fep-67ff.md) (federation documentation)
|
- Implement [FEP-67ff](https://codeberg.org/fediverse/fep/src/branch/main/fep/67ff/fep-67ff.md) (federation documentation)
|
||||||
|
|
||||||
|
## Added
|
||||||
|
- Meilisearch: it is now possible to use separate keys for search and admin actions
|
||||||
|
|
||||||
## Fixed
|
## Fixed
|
||||||
- Meilisearch: order of results returned from our REST API now actually matches how Meilisearch ranks results
|
- Meilisearch: order of results returned from our REST API now actually matches how Meilisearch ranks results
|
||||||
|
|
||||||
|
|
|
@ -33,6 +33,7 @@ indexes faster when it can process many posts in a single batch.
|
||||||
> config :pleroma, Pleroma.Search.Meilisearch,
|
> config :pleroma, Pleroma.Search.Meilisearch,
|
||||||
> url: "http://127.0.0.1:7700/",
|
> url: "http://127.0.0.1:7700/",
|
||||||
> private_key: "private key",
|
> private_key: "private key",
|
||||||
|
> search_key: "search key",
|
||||||
> initial_indexing_chunk_size: 100_000
|
> initial_indexing_chunk_size: 100_000
|
||||||
|
|
||||||
Information about setting up meilisearch can be found in the
|
Information about setting up meilisearch can be found in the
|
||||||
|
@ -45,7 +46,7 @@ is hardly usable on a somewhat big instance.
|
||||||
### Private key authentication (optional)
|
### Private key authentication (optional)
|
||||||
|
|
||||||
To set the private key, use the `MEILI_MASTER_KEY` environment variable when starting. After setting the _master key_,
|
To set the private key, use the `MEILI_MASTER_KEY` environment variable when starting. After setting the _master key_,
|
||||||
you have to get the _private key_, which is actually used for authentication.
|
you have to get the _private key_ and possibly _search key_, which are actually used for authentication.
|
||||||
|
|
||||||
=== "OTP"
|
=== "OTP"
|
||||||
```sh
|
```sh
|
||||||
|
@ -57,7 +58,11 @@ you have to get the _private key_, which is actually used for authentication.
|
||||||
mix pleroma.search.meilisearch show-keys <your master key here>
|
mix pleroma.search.meilisearch show-keys <your master key here>
|
||||||
```
|
```
|
||||||
|
|
||||||
You will see a "Default Admin API Key", this is the key you actually put into your configuration file.
|
You will see a "Default Admin API Key", this is the key you actually put into
|
||||||
|
your configuration file as `private_key`. You should also see a
|
||||||
|
"Default Search API key", put this into your config as `search_key`.
|
||||||
|
If your version of Meilisearch only showed the former,
|
||||||
|
just leave `search_key` completely unset in Akkoma's config.
|
||||||
|
|
||||||
### Initial indexing
|
### Initial indexing
|
||||||
|
|
||||||
|
|
|
@ -8,11 +8,24 @@ defmodule Pleroma.Search.Meilisearch do
|
||||||
|
|
||||||
@behaviour Pleroma.Search.SearchBackend
|
@behaviour Pleroma.Search.SearchBackend
|
||||||
|
|
||||||
defp meili_headers do
|
defp meili_headers(key) do
|
||||||
private_key = Pleroma.Config.get([Pleroma.Search.Meilisearch, :private_key])
|
key_header =
|
||||||
|
if is_nil(key), do: [], else: [{"Authorization", "Bearer #{key}"}]
|
||||||
|
|
||||||
[{"Content-Type", "application/json"}] ++
|
[{"Content-Type", "application/json"} | key_header]
|
||||||
if is_nil(private_key), do: [], else: [{"Authorization", "Bearer #{private_key}"}]
|
end
|
||||||
|
|
||||||
|
defp meili_headers_admin do
|
||||||
|
private_key = Pleroma.Config.get([Pleroma.Search.Meilisearch, :private_key])
|
||||||
|
meili_headers(private_key)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp meili_headers_search do
|
||||||
|
search_key =
|
||||||
|
Pleroma.Config.get([Pleroma.Search.Meilisearch, :search_key]) ||
|
||||||
|
Pleroma.Config.get([Pleroma.Search.Meilisearch, :private_key])
|
||||||
|
|
||||||
|
meili_headers(search_key)
|
||||||
end
|
end
|
||||||
|
|
||||||
def meili_get(path) do
|
def meili_get(path) do
|
||||||
|
@ -21,7 +34,7 @@ def meili_get(path) do
|
||||||
result =
|
result =
|
||||||
Pleroma.HTTP.get(
|
Pleroma.HTTP.get(
|
||||||
Path.join(endpoint, path),
|
Path.join(endpoint, path),
|
||||||
meili_headers()
|
meili_headers_admin()
|
||||||
)
|
)
|
||||||
|
|
||||||
with {:ok, res} <- result do
|
with {:ok, res} <- result do
|
||||||
|
@ -29,14 +42,14 @@ def meili_get(path) do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def meili_post(path, params) do
|
defp meili_search(params) do
|
||||||
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
|
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
|
||||||
|
|
||||||
result =
|
result =
|
||||||
Pleroma.HTTP.post(
|
Pleroma.HTTP.post(
|
||||||
Path.join(endpoint, path),
|
Path.join(endpoint, "/indexes/objects/search"),
|
||||||
Jason.encode!(params),
|
Jason.encode!(params),
|
||||||
meili_headers()
|
meili_headers_search()
|
||||||
)
|
)
|
||||||
|
|
||||||
with {:ok, res} <- result do
|
with {:ok, res} <- result do
|
||||||
|
@ -52,7 +65,7 @@ def meili_put(path, params) do
|
||||||
:put,
|
:put,
|
||||||
Path.join(endpoint, path),
|
Path.join(endpoint, path),
|
||||||
Jason.encode!(params),
|
Jason.encode!(params),
|
||||||
meili_headers(),
|
meili_headers_admin(),
|
||||||
[]
|
[]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -69,7 +82,7 @@ def meili_delete!(path) do
|
||||||
:delete,
|
:delete,
|
||||||
Path.join(endpoint, path),
|
Path.join(endpoint, path),
|
||||||
"",
|
"",
|
||||||
meili_headers(),
|
meili_headers_admin(),
|
||||||
[]
|
[]
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
@ -80,10 +93,7 @@ def search(user, query, options \\ []) do
|
||||||
author = Keyword.get(options, :author)
|
author = Keyword.get(options, :author)
|
||||||
|
|
||||||
res =
|
res =
|
||||||
meili_post(
|
meili_search(%{q: query, offset: offset, limit: limit})
|
||||||
"/indexes/objects/search",
|
|
||||||
%{q: query, offset: offset, limit: limit}
|
|
||||||
)
|
|
||||||
|
|
||||||
with {:ok, result} <- res do
|
with {:ok, result} <- res do
|
||||||
hits = result["hits"] |> Enum.map(& &1["ap"])
|
hits = result["hits"] |> Enum.map(& &1["ap"])
|
||||||
|
|
Loading…
Reference in a new issue