Uni Ecto Plugin: Fixed

defp apply_filters(query, []), do: query defp apply_filters(query, filters) do Enum.reduce(filters, query, fn :category, category, q -> from p in q, where: p.category == ^category :published_after, date, q -> from p in q, where: p.inserted_at >= ^date end) end

render(conn, "index.html", query: query, results: results, total_count: length(results) ) end uni ecto plugin

defp apply_full_text_search(query, %search_term: term) when is_binary(term) and term != "" do from q in query, where: fragment( "to_tsvector('english', ?) @@ plainto_tsquery('english', ?)", fragment("coalesce(?, '') || ' ' || coalesce(?, '')", q.title, q.content), ^term ) end defp apply_full_text_search(query, _), do: query do: query defp apply_filters(query

def autocomplete(conn, %"term" => term) do suggestions = Blog.search_posts(term, []) |> Enum.take(5) |> Enum.map(&%title: &1.title, id: &1.id) filters) do Enum.reduce(filters

def changeset(post, attrs) do post |> Ecto.Changeset.cast(attrs, [:title, :content, :tags]) |> update_search_vector() end

json(conn, %suggestions: suggestions) end end # lib/my_app_web/live/search_live.ex defmodule MyAppWeb.SearchLive do use MyAppWeb, :live_view alias MyApp.Blog

def render(assigns) do ~H""" <div> <form phx-submit="search" phx-change="search"> <input type="text" name="query" value=@search_term placeholder="Search posts..." class="w-full p-2 border rounded" /> <button type="submit">Search</button> </form>