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:
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)
|
||||
|
||||
Reference in New Issue
Block a user