Skip to content

Commit

Permalink
https://github.com/bonfire-networks/bonfire-app/issues/765
Browse files Browse the repository at this point in the history
  • Loading branch information
mayel committed Jan 15, 2024
1 parent 63d5ccc commit 642878b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
22 changes: 14 additions & 8 deletions lib/search.ex
Expand Up @@ -22,13 +22,15 @@ defmodule Bonfire.Search do

defp none(e, _) do
debug(e)
[]
nil
end

def run_search_db(search, types, opts) do
limit = opts[:limit] || 20

do_search_db(opts[:query] || base_query(), search, types, opts ++ [skip_boundary_check: true])
|> debug()
# |> Bonfire.Tag.Tags.search_hashtagged_query(search, opts) # TODO: use do_search_db like other types
|> where([p], is_nil(p.deleted_at))
# |> limit(^limit)
|> debug()
Expand Down Expand Up @@ -68,18 +70,22 @@ defmodule Bonfire.Search do
defp maybe_order_override(query, text, _several) do
query
|> Ecto.Query.exclude(:order_by)
|> order_by([post_content: pc, profile: p, character: c], [
|> order_by([named: n, post_content: pc, profile: p, character: c], [
{:desc,
fragment(
"(? <% ?)::int + (? <% ?)::int + (? <% ?)::int + (? <% ?)::int",
"(? <% ?)::int + (? <% ?)::int + (? <% ?)::int + (? <% ?)::int + (? <% ?)::int + (? <% ?)::int",
^text,
n.name,
^text,
pc.name,
^text,
pc.summary,
^text,
c.username,
^text,
p.name
p.name,
^text,
p.summary
)}
])
end
Expand All @@ -99,7 +105,7 @@ defmodule Bonfire.Search do
case Types.maybe_to_module(type) do
nil ->
debug(type, "not a module")
[]
query

mod ->
do_search_db(query, search, mod, opts)
Expand All @@ -117,10 +123,10 @@ defmodule Bonfire.Search do
[:search_query, :search],
[search, Keyword.put(opts, :query, query)],
&none/2
)
) || query
else
debug("no module, so skip searching ")
[]
query
end
end

Expand Down Expand Up @@ -148,6 +154,6 @@ defmodule Bonfire.Search do

def default_types(_opts) do
# TODO: make default types generated/configurable
[Bonfire.Data.Identity.User, Bonfire.Data.Social.Post]
[Bonfire.Data.Identity.User, Bonfire.Data.Social.Post, Bonfire.Tag.Tagged]
end
end
8 changes: 5 additions & 3 deletions lib/web/form_live.sface
Expand Up @@ -3,13 +3,15 @@
<form
class="w-full"
role="search"
phx-change={if @__context__[:current_view] == Bonfire.Search.Web.SearchLive,
do: "Bonfire.Search:search"}
phx-submit={if @__context__[:current_view] != Bonfire.Search.Web.SearchLive,
do: "Bonfire.Search:go_search"}
do: "Bonfire.Search:go_search",
else: "Bonfire.Search:search"}
action="/search"
method="GET"
>
<!-- phx-change={if @__context__[:current_view] == Bonfire.Search.Web.SearchLive,
do: "Bonfire.Search:search"} -->

<span class="sr-only">{e(@search_placeholder, l("Search"))}</span>
<div class="relative w-full rounded">
<input type="hidden" name="search_limit" value={e(@search_limit, "")}>
Expand Down
25 changes: 24 additions & 1 deletion lib/web/live_handler.ex
Expand Up @@ -80,14 +80,37 @@ defmodule Bonfire.Search.LiveHandler do
[]
end

# tagged =
# with hashtags when is_list(hashtags) <-
# Bonfire.Tag.Tags.search_hashtag(
# q
# # fetch_collection: :async
# )
# |> debug("got_hashtags") do
# hashtags
# # {:noreply, socket |> redirect_to(path(federated_object_or_character))}
# else
# _ ->
# []
# end

{num_hits, hits, facets} =
do_search(q, facet_filters, opts)
|> debug("did_search1")

# + length(tagged)
num_hits = (num_hits || 0) + length(by_link_or_username)

# ++ tagged
hits =
(by_link_or_username ++ hits)
|> Enum.uniq_by(&Enums.id/1)
|> debug("search2 merged")

{:noreply,
assign_global(socket,
selected_facets: facet_filters,
hits: (by_link_or_username ++ hits) |> Enum.uniq_by(&Enums.id/1),
hits: hits,
facets: facets || e(socket.assigns, :facets, nil),
num_hits: num_hits,
search: q
Expand Down
4 changes: 2 additions & 2 deletions lib/web/search_live.sface
Expand Up @@ -39,7 +39,7 @@
</div>
</LiveRedirect>
{!-- POSTS --}
<LiveRedirect
<!-- <LiveRedirect
class="flex flex-1 pt-4 text-base capitalize hover:bg-base-content hover:bg-opacity-10 place-content-center"
to={"/search?facet[index_type]=Bonfire.Data.Social.Post&s=" <> e(assigns, :search, "")}
>
Expand All @@ -49,7 +49,7 @@
}>
{l("Posts")}
</div>
</LiveRedirect>
</LiveRedirect> -->
</nav>
<Bonfire.Search.Web.ResultsLive hits={e(@hits, [])} />
</div>
Expand Down

0 comments on commit 642878b

Please sign in to comment.