fix(search): set gin_fuzzy_search_limit only for search queries
The limit was a connection parameter, so it applied to every GIN index scan, not only full-text search: recipient lookups and Oban job queries could return incomplete, random results. Set it with SET LOCAL around the search query only, configurable under Pleroma.Search.DatabaseSearch. The default stays 500 for search: measured on a 74M-object database, higher limits or none make searches for common words exceed the query timeout. Remote fetches for URL queries run outside the transaction. Based on Akkoma 811f0a468. Co-authored-by: Oneric <oneric@oneric.stub>
This commit is contained in:
parent
24038667b2
commit
fa8541309c
6 changed files with 98 additions and 17 deletions
|
|
@ -7,6 +7,7 @@ defmodule Pleroma.Search.DatabaseSearch do
|
|||
alias Pleroma.Config
|
||||
alias Pleroma.Object.Fetcher
|
||||
alias Pleroma.Pagination
|
||||
alias Pleroma.Repo
|
||||
alias Pleroma.User
|
||||
alias Pleroma.Web.ActivityPub.Visibility
|
||||
|
||||
|
|
@ -23,22 +24,49 @@ defmodule Pleroma.Search.DatabaseSearch do
|
|||
offset = Keyword.get(options, :offset, 0)
|
||||
author = Keyword.get(options, :author)
|
||||
|
||||
try do
|
||||
Activity
|
||||
|> Activity.with_preloaded_object()
|
||||
|> Activity.restrict_deactivated_users()
|
||||
|> restrict_public(user)
|
||||
|> query_with(index_type, search_query)
|
||||
|> maybe_restrict_local(user)
|
||||
|> maybe_restrict_author(author)
|
||||
|> maybe_restrict_blocked(user)
|
||||
|> Pagination.fetch_paginated(
|
||||
%{"offset" => offset, "limit" => limit, "skip_order" => index_type == :rum},
|
||||
:offset
|
||||
)
|
||||
|> maybe_fetch(user, search_query)
|
||||
rescue
|
||||
_ -> maybe_fetch([], user, search_query)
|
||||
activities =
|
||||
try do
|
||||
with_fuzzy_limit(fn ->
|
||||
Activity
|
||||
|> Activity.with_preloaded_object()
|
||||
|> Activity.restrict_deactivated_users()
|
||||
|> restrict_public(user)
|
||||
|> query_with(index_type, search_query)
|
||||
|> maybe_restrict_local(user)
|
||||
|> maybe_restrict_author(author)
|
||||
|> maybe_restrict_blocked(user)
|
||||
|> Pagination.fetch_paginated(
|
||||
%{"offset" => offset, "limit" => limit, "skip_order" => index_type == :rum},
|
||||
:offset
|
||||
)
|
||||
end)
|
||||
rescue
|
||||
_ -> []
|
||||
end
|
||||
|
||||
# Outside the query transaction, as it may fetch over the network.
|
||||
maybe_fetch(activities, user, search_query)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Runs `fun` with `gin_fuzzy_search_limit` set, if configured under
|
||||
`Pleroma.Search.DatabaseSearch`. The limit makes GIN scans faster but their
|
||||
results incomplete and random, so it is only set for search queries.
|
||||
"""
|
||||
def with_fuzzy_limit(fun) do
|
||||
case Config.get([__MODULE__, :gin_fuzzy_search_limit]) do
|
||||
limit when is_integer(limit) and limit > 0 ->
|
||||
{:ok, result} =
|
||||
Repo.transaction(fn ->
|
||||
# SET LOCAL takes no parameters; limit is an integer.
|
||||
Repo.query!("SET LOCAL gin_fuzzy_search_limit TO #{limit}")
|
||||
fun.()
|
||||
end)
|
||||
|
||||
result
|
||||
|
||||
_ ->
|
||||
fun.()
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue