AshBackpex.BasicSearch (Ash Backpex v0.1.6)

View Source

Provides basic text search functionality for AshBackpex LiveResources.

This module implements search by applying contains filters on fields marked as searchable: true in the field configuration. When multiple fields are searchable, they are combined with OR logic so a search term matches if it appears in any searchable field.

Usage

Search is automatically integrated when you mark fields as searchable:

defmodule MyAppWeb.Admin.PostLive do
  use AshBackpex.LiveResource

  backpex do
    resource MyApp.Blog.Post
    layout {MyAppWeb.Layouts, :admin}

    fields do
      field :title do
        searchable true
      end
      field :content do
        searchable true
      end
      field :author_name  # Not searchable
    end
  end
end

With this configuration, searching for "hello" will find posts where either the title OR content contains "hello" (case-insensitive).

How It Works

  1. The Backpex UI sends search terms via the search query parameter
  2. The adapter passes params to BasicSearch.apply/3
  3. This module finds all searchable fields from the LiveResource
  4. It builds an Ash filter with contains expressions joined by OR
  5. The filter is applied to the query via Ash.Query.filter_input/2

Direct Usage

While normally called automatically by the adapter, you can use this module directly if needed:

query = MyApp.Blog.Post |> Ash.Query.new()
params = %{"search" => "hello"}

filtered_query = AshBackpex.BasicSearch.apply(query, params, MyAppWeb.Admin.PostLive)

Summary

Functions

Applies search filters to an Ash query based on searchable fields.

Types

live_resource()

@type live_resource() :: module()

params()

@type params() :: map()

query()

@type query() :: Ash.Query.t()

Functions

apply(query, arg2, live_resource)

@spec apply(query(), params(), live_resource()) :: query()

Applies search filters to an Ash query based on searchable fields.

Takes a query, a params map (typically from assigns.params), and a LiveResource module. Returns the query with search filters applied.

If no search value is supplied in params, or the LiveResource has no searchable fields, the query is returned unmodified.

Parameters

  • query - An Ash.Query.t() to filter
  • params - Map with optional "search" key containing the search term
  • live_resource - Module that uses AshBackpex.LiveResource

Examples

iex> query = MyApp.Post |> Ash.Query.new()
iex> AshBackpex.BasicSearch.apply(query, %{"search" => "elixir"}, MyPostLive)
#Ash.Query<...>

iex> # No search term - query unchanged
iex> AshBackpex.BasicSearch.apply(query, %{}, MyPostLive)
#Ash.Query<...>