AshBackpex.BasicSearch (Ash Backpex v0.1.2)
View SourceProvides 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
endWith this configuration, searching for "hello" will find posts where either the title OR content contains "hello" (case-insensitive).
How It Works
- The Backpex UI sends search terms via the
searchquery parameter - The adapter passes params to
BasicSearch.apply/3 - This module finds all searchable fields from the LiveResource
- It builds an Ash filter with
containsexpressions joined by OR - 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
Functions
@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- AnAsh.Query.t()to filterparams- Map with optional"search"key containing the search termlive_resource- Module thatusesAshBackpex.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<...>