1
0
Fork 0

meilisearch: respect meili’s result ranking

Meilisearch is already configured to return results sorted by a
particular ranking configured in the meilisearch CLI task.
Resorting the returned top results by date partially negates this and
runs counter to what someone with tweaked settings expects.

Issue and fix identified by AdamK2003 in
AkkomaGang/akkoma#579
But instead of using a O(n^2) resorting, this commit directly
retrieves results in the correct order from the database.

Resolves: AkkomaGang/akkoma#579
This commit is contained in:
Oneric 2024-05-02 22:44:48 +02:00
parent 852d11bedf
commit 031e764eaf
3 changed files with 20 additions and 3 deletions

View File

@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## Unreleased
## Fixed
- Meilisearch: order of results returned from our REST API now actually matches how Meilisearch ranks results
## 2024.04
## Added

View File

@ -258,6 +258,22 @@ defmodule Pleroma.Activity do
def get_create_by_object_ap_id(_), do: nil
@spec create_by_object_ap_id([String.t()]) :: Ecto.Queryable.t()
def get_presorted_create_by_object_ap_id(ap_ids) do
from(
a in Activity,
join:
ids in fragment(
"SELECT * FROM UNNEST(?::text[]) WITH ORDINALITY AS ids(ap_id, ord)",
^ap_ids
),
on:
ids.ap_id == fragment("?->>'object'", a.data) and
fragment("?->>'type'", a.data) == "Create",
order_by: [asc: ids.ord]
)
end
@doc """
Accepts `ap_id` or list of `ap_id`.
Returns a query.

View File

@ -5,7 +5,6 @@ defmodule Pleroma.Search.Meilisearch do
alias Pleroma.Activity
import Pleroma.Search.DatabaseSearch
import Ecto.Query
@behaviour Pleroma.Search.SearchBackend
@ -91,14 +90,13 @@ defmodule Pleroma.Search.Meilisearch do
try do
hits
|> Activity.create_by_object_ap_id()
|> Activity.get_presorted_create_by_object_ap_id()
|> Activity.with_preloaded_object()
|> Activity.restrict_deactivated_users()
|> maybe_restrict_local(user)
|> maybe_restrict_author(author)
|> maybe_restrict_blocked(user)
|> maybe_fetch(user, query)
|> order_by([object: obj], desc: obj.data["published"])
|> Pleroma.Repo.all()
rescue
_ -> maybe_fetch([], user, query)