1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.User.Search do
10 @similarity_threshold 0.25
13 def search(query_string, opts \\ []) do
14 resolve = Keyword.get(opts, :resolve, false)
15 following = Keyword.get(opts, :following, false)
16 result_limit = Keyword.get(opts, :limit, @limit)
17 offset = Keyword.get(opts, :offset, 0)
19 for_user = Keyword.get(opts, :for_user)
21 query_string = format_query(query_string)
23 maybe_resolve(resolve, for_user, query_string)
26 Repo.transaction(fn ->
27 Ecto.Adapters.SQL.query(
29 "select set_limit(#{@similarity_threshold})",
34 |> search_query(for_user, following)
35 |> paginate(result_limit, offset)
42 defp format_query(query_string) do
43 # Strip the beginning @ off if there is a query
44 query_string = String.trim_leading(query_string, "@")
46 with [name, domain] <- String.split(query_string, "@"),
47 formatted_domain <- String.replace(domain, ~r/[!-\-|@|[-`|{-~|\/|:]+/, "") do
48 name <> "@" <> to_string(:idna.encode(formatted_domain))
54 defp search_query(query_string, for_user, following) do
56 |> base_query(following)
57 |> filter_blocked_user(for_user)
58 |> filter_blocked_domains(for_user)
59 |> search_subqueries(query_string)
62 |> boost_search_rank_query(for_user)
64 |> order_by(desc: :search_rank)
65 |> maybe_restrict_local(for_user)
68 defp base_query(_user, false), do: User
69 defp base_query(user, true), do: User.get_followers_query(user)
71 defp filter_blocked_user(query, %User{info: %{blocks: blocks}})
72 when length(blocks) > 0 do
73 from(q in query, where: not (q.ap_id in ^blocks))
76 defp filter_blocked_user(query, _), do: query
78 defp filter_blocked_domains(query, %User{info: %{domain_blocks: domain_blocks}})
79 when length(domain_blocks) > 0 do
80 domains = Enum.join(domain_blocks, ",")
84 where: fragment("substring(ap_id from '.*://([^/]*)') NOT IN (?)", ^domains)
88 defp filter_blocked_domains(query, _), do: query
90 defp paginate(query, limit, offset) do
91 from(q in query, limit: ^limit, offset: ^offset)
94 defp union_subqueries({fts_subquery, trigram_subquery}) do
95 from(s in trigram_subquery, union_all: ^fts_subquery)
98 defp search_subqueries(base_query, query_string) do
100 fts_search_subquery(base_query, query_string),
101 trigram_search_subquery(base_query, query_string)
105 defp distinct_query(q) do
106 from(s in subquery(q), order_by: s.search_type, distinct: s.id)
109 defp maybe_resolve(true, user, query) do
110 case {limit(), user} do
112 {:unauthenticated, %User{}} -> User.get_or_fetch(query)
113 {:unauthenticated, _} -> :noop
114 {false, _} -> User.get_or_fetch(query)
118 defp maybe_resolve(_, _, _), do: :noop
120 defp maybe_restrict_local(q, user) do
121 case {limit(), user} do
122 {:all, _} -> restrict_local(q)
123 {:unauthenticated, %User{}} -> q
124 {:unauthenticated, _} -> restrict_local(q)
129 defp limit, do: Pleroma.Config.get([:instance, :limit_to_local_content], :unauthenticated)
131 defp restrict_local(q), do: where(q, [u], u.local == true)
133 defp boost_search_rank_query(query, nil), do: query
135 defp boost_search_rank_query(query, for_user) do
136 friends_ids = User.get_friends_ids(for_user)
137 followers_ids = User.get_followers_ids(for_user)
139 from(u in subquery(query),
144 CASE WHEN (?) THEN 0.5 + (?) * 1.3
145 WHEN (?) THEN 0.5 + (?) * 1.2
146 WHEN (?) THEN (?) * 1.1
149 u.id in ^friends_ids and u.id in ^followers_ids,
151 u.id in ^friends_ids,
153 u.id in ^followers_ids,
161 @spec fts_search_subquery(User.t() | Ecto.Query.t(), String.t()) :: Ecto.Query.t()
162 defp fts_search_subquery(query, term) do
164 String.trim_trailing(term, "@" <> local_domain())
165 |> String.replace(~r/[!-\/|@|[-`|{-~|:-?]+/, " ")
168 |> Enum.map(&(&1 <> ":*"))
179 setweight(to_tsvector('simple', regexp_replace(?, '\\W', ' ', 'g')), 'A') ||
180 setweight(to_tsvector('simple', regexp_replace(coalesce(?, ''), '\\W', ' ', 'g')), 'B'),
181 to_tsquery('simple', ?),
193 (setweight(to_tsvector('simple', regexp_replace(?, '\\W', ' ', 'g')), 'A') ||
194 setweight(to_tsvector('simple', regexp_replace(coalesce(?, ''), '\\W', ' ', 'g')), 'B')) @@ to_tsquery('simple', ?)
201 |> User.restrict_deactivated()
204 @spec trigram_search_subquery(User.t() | Ecto.Query.t(), String.t()) :: Ecto.Query.t()
205 defp trigram_search_subquery(query, term) do
206 term = String.trim_trailing(term, "@" <> local_domain())
211 # ^1 gives 'Postgrex expected a binary, got 1' for some weird reason
212 search_type: fragment("?", 1),
215 "similarity(?, trim(? || ' ' || coalesce(?, '')))",
221 where: fragment("trim(? || ' ' || coalesce(?, '')) % ?", u.nickname, u.name, ^term)
223 |> User.restrict_deactivated()
226 defp local_domain, do: Pleroma.Config.get([Pleroma.Web.Endpoint, :url, :host])