Implement search with PostgreSQL trigrams
Signed-off-by: Thomas Citharel <tcit@tcit.fr> Rename function to reflect that we only get one result Signed-off-by: Thomas Citharel <tcit@tcit.fr> Add loggers and make Ecto call parallels during search Signed-off-by: Thomas Citharel <tcit@tcit.fr> Implement trigrams for events & replace pg similarity operator % with <% Signed-off-by: Thomas Citharel <tcit@tcit.fr> Fix tests Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -467,6 +467,11 @@ defmodule Mobilizon.Actors do
|
||||
|> Repo.preload(:organized_events)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Getting an actor from url, eventually creating it
|
||||
"""
|
||||
# TODO: Move this to Mobilizon.Service.ActivityPub
|
||||
@spec get_or_fetch_by_url(String.t(), bool()) :: {:ok, Actor.t()} | {:error, String.t()}
|
||||
def get_or_fetch_by_url(url, preload \\ false) do
|
||||
with {:ok, actor} <- get_actor_by_url(url, preload) do
|
||||
{:ok, actor}
|
||||
@@ -498,16 +503,32 @@ defmodule Mobilizon.Actors do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Find local users by it's username
|
||||
Find local users by their username
|
||||
"""
|
||||
# TODO: This doesn't seem to be used anyway
|
||||
def find_local_by_username(username) do
|
||||
actors =
|
||||
Repo.all(
|
||||
from(
|
||||
a in Actor,
|
||||
where:
|
||||
(ilike(a.preferred_username, ^like_sanitize(username)) or
|
||||
ilike(a.name, ^like_sanitize(username))) and is_nil(a.domain)
|
||||
fragment(
|
||||
"f_unaccent(?) <% f_unaccent(?) or
|
||||
f_unaccent(coalesce(?, '')) <% f_unaccent(?)",
|
||||
a.preferred_username,
|
||||
^username,
|
||||
a.name,
|
||||
^username
|
||||
),
|
||||
where: is_nil(a.domain),
|
||||
order_by:
|
||||
fragment(
|
||||
"word_similarity(?, ?) + word_similarity(coalesce(?, ''), ?) desc",
|
||||
a.preferred_username,
|
||||
^username,
|
||||
a.name,
|
||||
^username
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
@@ -526,48 +547,31 @@ defmodule Mobilizon.Actors do
|
||||
from(
|
||||
a in Actor,
|
||||
where:
|
||||
ilike(a.preferred_username, ^like_sanitize(username)) or
|
||||
ilike(a.name, ^like_sanitize(username))
|
||||
fragment(
|
||||
"f_unaccent(?) %> f_unaccent(?) or
|
||||
f_unaccent(coalesce(?, '')) %> f_unaccent(?)",
|
||||
a.preferred_username,
|
||||
^username,
|
||||
a.name,
|
||||
^username
|
||||
),
|
||||
order_by:
|
||||
fragment(
|
||||
"word_similarity(?, ?) + word_similarity(coalesce(?, ''), ?) desc",
|
||||
a.preferred_username,
|
||||
^username,
|
||||
a.name,
|
||||
^username
|
||||
)
|
||||
)
|
||||
|> paginate(page, limit)
|
||||
)
|
||||
end
|
||||
|
||||
# Sanitize the LIKE queries
|
||||
defp like_sanitize(value) do
|
||||
"%" <> String.replace(value, ~r/([\\%_])/, "\\1") <> "%"
|
||||
end
|
||||
|
||||
@email_regex ~r/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
|
||||
@spec search(String.t()) :: {:ok, list(Actor.t())} | {:ok, []} | {:error, any()}
|
||||
def search(name) do
|
||||
# find already saved accounts
|
||||
case find_actors_by_username_or_name(name) do
|
||||
[] ->
|
||||
# no accounts found, let's test if it's an username@domain.tld
|
||||
with true <- Regex.match?(@email_regex, name),
|
||||
# creating the actor in that case
|
||||
{:ok, actor} <- ActivityPub.find_or_make_actor_from_nickname(name) do
|
||||
{:ok, [actor]}
|
||||
else
|
||||
false ->
|
||||
{:ok, []}
|
||||
|
||||
# error fingering the actor
|
||||
{:error, err} ->
|
||||
{:error, err}
|
||||
end
|
||||
|
||||
actors = [_ | _] ->
|
||||
# actors already saved found !
|
||||
{:ok, actors}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Find a group by its actor id
|
||||
Get a group by its actor id
|
||||
"""
|
||||
def find_group_by_actor_id(actor_id) do
|
||||
def get_group_by_actor_id(actor_id) do
|
||||
case Repo.get_by(Actor, id: actor_id, type: :Group) do
|
||||
nil -> {:error, :group_not_found}
|
||||
actor -> {:ok, actor}
|
||||
|
||||
@@ -45,7 +45,7 @@ defmodule Mobilizon.Actors.User do
|
||||
:password,
|
||||
min: 6,
|
||||
max: 100,
|
||||
message: "The choosen password is too short."
|
||||
message: "The chosen password is too short."
|
||||
)
|
||||
|
||||
if Map.has_key?(attrs, :default_actor) do
|
||||
|
||||
@@ -248,7 +248,19 @@ defmodule Mobilizon.Events do
|
||||
|
||||
query =
|
||||
from(e in Event,
|
||||
where: e.visibility == ^:public and ilike(e.title, ^like_sanitize(name)),
|
||||
where:
|
||||
e.visibility == ^:public and
|
||||
fragment(
|
||||
"f_unaccent(?) %> f_unaccent(?)",
|
||||
e.title,
|
||||
^name
|
||||
),
|
||||
order_by:
|
||||
fragment(
|
||||
"word_similarity(?, ?) desc",
|
||||
e.title,
|
||||
^name
|
||||
),
|
||||
preload: [:organizer_actor]
|
||||
)
|
||||
|> paginate(page, limit)
|
||||
@@ -256,11 +268,6 @@ defmodule Mobilizon.Events do
|
||||
Repo.all(query)
|
||||
end
|
||||
|
||||
# Sanitize the LIKE queries
|
||||
defp like_sanitize(value) do
|
||||
"%" <> String.replace(value, ~r/([\\%_])/, "\\1") <> "%"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a event.
|
||||
|
||||
|
||||
99
lib/mobilizon_web/api/search.ex
Normal file
99
lib/mobilizon_web/api/search.ex
Normal file
@@ -0,0 +1,99 @@
|
||||
defmodule MobilizonWeb.API.Search do
|
||||
alias Mobilizon.Service.ActivityPub
|
||||
alias Mobilizon.Actors
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Events
|
||||
alias Mobilizon.Events.{Event, Comment}
|
||||
|
||||
require Logger
|
||||
|
||||
@doc """
|
||||
Search
|
||||
"""
|
||||
@spec search(String.t(), integer(), integer()) ::
|
||||
{:ok, list(Actor.t())} | {:ok, []} | {:error, any()}
|
||||
def search(search, page \\ 1, limit \\ 10) do
|
||||
do_search(search, page, limit, %{events: true, actors: true})
|
||||
end
|
||||
|
||||
@doc """
|
||||
Not used at the moment
|
||||
"""
|
||||
# TODO: Use me
|
||||
@spec search_actors(String.t(), integer(), integer()) ::
|
||||
{:ok, list(Actor.t())} | {:ok, []} | {:error, any()}
|
||||
def search_actors(search, page \\ 1, limit \\ 10) do
|
||||
do_search(search, page, limit, %{actors: true})
|
||||
end
|
||||
|
||||
@doc """
|
||||
Not used at the moment
|
||||
"""
|
||||
# TODO: Use me
|
||||
@spec search_events(String.t(), integer(), integer()) ::
|
||||
{:ok, list(Event.t())} | {:ok, []} | {:error, any()}
|
||||
def search_events(search, page \\ 1, limit \\ 10) do
|
||||
do_search(search, page, limit, %{events: true})
|
||||
end
|
||||
|
||||
# Do the actual search
|
||||
@spec do_search(String.t(), integer(), integer(), map()) :: {:ok, list(any())}
|
||||
defp do_search(search, page, limit, opts) do
|
||||
search = String.trim(search)
|
||||
|
||||
cond do
|
||||
search == "" ->
|
||||
{:error, "Search can't be empty"}
|
||||
|
||||
String.match?(search, ~r/@/) ->
|
||||
{:ok, process_from_username(search)}
|
||||
|
||||
String.starts_with?(search, "https://") ->
|
||||
{:ok, process_from_url(search)}
|
||||
|
||||
String.starts_with?(search, "http://") ->
|
||||
{:ok, process_from_url(search)}
|
||||
|
||||
true ->
|
||||
events =
|
||||
Task.async(fn ->
|
||||
if Map.get(opts, :events, false),
|
||||
do: Events.find_events_by_name(search, page, limit),
|
||||
else: []
|
||||
end)
|
||||
|
||||
actors =
|
||||
Task.async(fn ->
|
||||
if Map.get(opts, :actors, false),
|
||||
do: Actors.find_actors_by_username_or_name(search, page, limit),
|
||||
else: []
|
||||
end)
|
||||
|
||||
{:ok, Task.await(events) ++ Task.await(actors)}
|
||||
end
|
||||
end
|
||||
|
||||
# If the search string is an username
|
||||
@spec process_from_username(String.t()) :: Actor.t() | nil
|
||||
defp process_from_username(search) do
|
||||
with {:ok, actor} <- ActivityPub.find_or_make_actor_from_nickname(search) do
|
||||
actor
|
||||
else
|
||||
{:error, _err} ->
|
||||
Logger.debug("Unable to find or make actor '#{search}'")
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
# If the search string is an URL
|
||||
@spec process_from_url(String.t()) :: Actor.t() | Event.t() | Comment.t() | nil
|
||||
defp process_from_url(search) do
|
||||
with {:ok, object} <- ActivityPub.fetch_object_from_url(search) do
|
||||
object
|
||||
else
|
||||
{:error, _err} ->
|
||||
Logger.debug("Unable to find or make object from URL '#{search}'")
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -2,7 +2,6 @@ defmodule MobilizonWeb.Resolvers.Event do
|
||||
@moduledoc """
|
||||
Handles the event-related GraphQL calls
|
||||
"""
|
||||
alias Mobilizon.Service.ActivityPub
|
||||
alias Mobilizon.Activity
|
||||
alias Mobilizon.Events.{Event, Participant}
|
||||
alias Mobilizon.Actors.User
|
||||
@@ -122,39 +121,6 @@ defmodule MobilizonWeb.Resolvers.Event do
|
||||
{:error, "You need to be logged-in to leave an event"}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Search events by title
|
||||
"""
|
||||
def search_events(_parent, %{search: search, page: page, limit: limit}, _resolution) do
|
||||
{:ok, Mobilizon.Events.find_events_by_name(search, page, limit)}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Search events and actors by title
|
||||
"""
|
||||
def search_events_and_actors(_parent, %{search: search, page: page, limit: limit}, _resolution) do
|
||||
search = String.trim(search)
|
||||
|
||||
found =
|
||||
case String.contains?(search, "@") do
|
||||
true ->
|
||||
with {:ok, actor} <- ActivityPub.find_or_make_actor_from_nickname(search) do
|
||||
actor
|
||||
else
|
||||
{:error, _err} ->
|
||||
nil
|
||||
end
|
||||
|
||||
_ ->
|
||||
Mobilizon.Events.find_events_by_name(search, page, limit) ++
|
||||
Mobilizon.Actors.find_actors_by_username_or_name(search, page, limit)
|
||||
end
|
||||
|
||||
require Logger
|
||||
Logger.debug(inspect(found))
|
||||
{:ok, found}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Create an event
|
||||
"""
|
||||
|
||||
@@ -81,7 +81,7 @@ defmodule MobilizonWeb.Resolvers.Group do
|
||||
}
|
||||
}
|
||||
) do
|
||||
with {:ok, %Actor{} = group} <- Actors.find_group_by_actor_id(group_id),
|
||||
with {:ok, %Actor{} = group} <- Actors.get_group_by_actor_id(group_id),
|
||||
{:is_owned, true, _} <- User.owns_actor(user, actor_id),
|
||||
{:ok, %Member{} = member} <- Member.get_member(actor_id, group.id),
|
||||
{:is_admin, true} <- Member.is_administrator(member),
|
||||
|
||||
13
lib/mobilizon_web/resolvers/search.ex
Normal file
13
lib/mobilizon_web/resolvers/search.ex
Normal file
@@ -0,0 +1,13 @@
|
||||
defmodule MobilizonWeb.Resolvers.Search do
|
||||
@moduledoc """
|
||||
Handles the event-related GraphQL calls
|
||||
"""
|
||||
alias MobilizonWeb.API.Search
|
||||
|
||||
@doc """
|
||||
Search events and actors by title
|
||||
"""
|
||||
def search_events_and_actors(_parent, %{search: search, page: page, limit: limit}, _resolution) do
|
||||
Search.search(search, page, limit)
|
||||
end
|
||||
end
|
||||
@@ -122,7 +122,7 @@ defmodule MobilizonWeb.Schema do
|
||||
arg(:search, non_null(:string))
|
||||
arg(:page, :integer, default_value: 1)
|
||||
arg(:limit, :integer, default_value: 10)
|
||||
resolve(&Resolvers.Event.search_events_and_actors/3)
|
||||
resolve(&Resolvers.Search.search_events_and_actors/3)
|
||||
end
|
||||
|
||||
import_fields(:user_queries)
|
||||
|
||||
@@ -66,6 +66,7 @@ defmodule Mobilizon.Service.ActivityPub do
|
||||
@doc """
|
||||
Fetch an object from an URL, from our local database of events and comments, then eventually remote
|
||||
"""
|
||||
# TODO: Make database calls parallel
|
||||
@spec fetch_object_from_url(String.t()) :: {:ok, %Event{}} | {:ok, %Comment{}} | {:error, any()}
|
||||
def fetch_object_from_url(url) do
|
||||
Logger.info("Fetching object from url #{url}")
|
||||
@@ -73,6 +74,7 @@ defmodule Mobilizon.Service.ActivityPub do
|
||||
with true <- String.starts_with?(url, "http"),
|
||||
nil <- Events.get_event_by_url(url),
|
||||
nil <- Events.get_comment_from_url(url),
|
||||
{:error, :actor_not_found} <- Actors.get_actor_by_url(url),
|
||||
{:ok, %{body: body, status_code: code}} when code in 200..299 <-
|
||||
HTTPoison.get(
|
||||
url,
|
||||
@@ -97,12 +99,16 @@ defmodule Mobilizon.Service.ActivityPub do
|
||||
"Note" ->
|
||||
{:ok, Events.get_comment_full_from_url!(activity.data["object"]["id"])}
|
||||
|
||||
"Actor" ->
|
||||
{:ok, Actors.get_actor_by_url!(activity.data["object"]["id"], true)}
|
||||
|
||||
other ->
|
||||
{:error, other}
|
||||
end
|
||||
else
|
||||
%Event{url: event_url} -> {:ok, Events.get_event_by_url!(event_url)}
|
||||
%Comment{url: comment_url} -> {:ok, Events.get_comment_full_from_url!(comment_url)}
|
||||
%Actor{url: actor_url} -> {:ok, Actors.get_actor_by_url!(actor_url, true)}
|
||||
e -> {:error, e}
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user