test "search finds relevant posts" do results = Blog.search_posts("elixir search") assert length(results) > 0 assert Enum.any?(results, &String.contains?(&1.title, "Elixir")) end
defp apply_filters(query, %category: category) do from q in query, where: q.category == ^category end defp apply_filters(query, _), do: query uni ecto plugin
def handle_event("search", %"query" => query, socket) do results = Blog.search_posts(query) test "search finds relevant posts" do results = Blog
Using PostgreSQL's Full-Text Search # lib/my_app/repo.ex defmodule MyApp.Repo do use Ecto.Repo, otp_app: :my_app Custom full-text search function def full_text_search(queryable, search_term, fields) do search_term = search_term |> String.trim() |> String.replace(~r/\s+/, " & ") # Convert spaces to & for AND operator 0 assert Enum.any?(results
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