How to use rummage for searching

I am trying to understand using rummage.

in docs.

rummage = %{
  search: %{field_1 => %{search_type: :like, search_term: "field_!"}},
  sort: %{field: :field1, order: :asc},
  paginate: %{per_page: 5, page: 1}

{queryable, rummage} = Product
  |> Rummage.Ecto.rummage(rummage)

then how can I build query parameters for this query to reach to my controller?
or is there anyway to do search in phoenix?
How do you guys do implement search?

How do you guys do implement search?

For searching, sorting, and filtering, I use Inquisitor with plain old Ecto queries. Inquisitor is a composable query builder. This approach gets me very far.

First, install Inquisitor and define a search module in your context:

defmodule Obramax.Estimates.CartFilters do
  use Inquisitor
  import Ecto.Query, warn: false

  def build_query(query, "term", value, _conn) when is_present(value) do
    value = String.trim(value)
    search_query = "%" <> value <> "%"
    from cart in query,
      where: fragment("address ->> 'recipient_cpf' LIKE ?", ^search_query),
      or_where: fragment("address ->> 'recipient_cnpj' LIKE ?", ^search_query),
      or_where: like(cart.reference_number, ^search_query)

Pass the params map to the search module from your controller. If any of the functions you define in the search module match the params pattern, the function will be executed and the query will be composed. Your index action might look something like this:

  def index(conn, params) do
    page =
      |> CartFilters.build_query(conn, params)
      |> Estimates.list_carts(params)
    render(conn, "index.html", page: page)

Finally, forward the query that comes back from the search module to the context. The context function might look like this:

  def list_carts(query \\ Cart, params \\ %{}) do
    from(c in query,
      preload: [:owner, [line_items: :product]])
    |> Repo.paginate(params)

For pagination, I use Scrivener. The pagination is carried out in the context module.

1 Like

Thanks for your reply.
This solves my problem!

How do you do pagination links?

<%= pagination_links @page %>

Search results may return several pages, and pagination links show how many pages. but link that generated has no query parameter info. It has only page number not search result.
How do you do this?

You need scrivener_html for pagination links.