View Source Dequel.Adapter.Ecto.Filter (Dequel v0.7.0)
Ecto adapter for Dequel queries. Converts parsed AST to Ecto dynamic queries.
Relational Query Handling
Relational queries like author.name:Tolkien are handled in three steps:
1. Path Detection & Join Tracking
When build_filter receives an AST node with a path (list of atoms like
[:author, :name]), it calls Context.ensure_joins/2 to register the needed
joins. The path is split: [:author] becomes the join path, :name is the
field to filter on. The context returns a binding name like :join_author.
2. Dynamic Expression with Named Binding
The build_dynamic functions create Ecto dynamic expressions that reference
the joined table via named binding:
dynamic([{^binding, x}], field(x, ^field) == ^value)The [{^binding, x}] syntax binds x to the table with that alias.
3. Query Assembly
The query/3 function assembles everything:
- Wraps base query with
:qalias viaensure_base_alias/1 - Applies LEFT JOINs in dependency order via
apply_joins/2 - Adds the WHERE clause with the dynamic expression
- Optionally adds preloads
Example Flow
For query "author.name:Tolkien":
- Parser produces:
{:==, [], [[:author, :name], "Tolkien"]} Context.ensure_joins/2registers join for[:author]→ returns:join_authorbuild_dynamic/4creates:dynamic([{:join_author, x}], field(x, :name) == "Tolkien")apply_joins/2adds:join(:left, [{:q, p}], a in assoc(p, :author), as: :join_author)- Final query filters on the joined author's name field
Summary
Functions
Build filter with join tracking. Returns {dynamic, context}.
Build a complete Ecto.Query with joins applied for relationship filtering. Returns a query that can be passed to Repo.all/one/etc.
Functions
@spec filter(term()) :: {Ecto.Query.dynamic_expr(), Dequel.Query.Context.t()}
Build filter with join tracking. Returns {dynamic, context}.
@spec filter(term(), Dequel.Query.Context.t()) :: {Ecto.Query.dynamic_expr(), Dequel.Query.Context.t()}
@spec query(Ecto.Query.t(), String.t(), keyword()) :: Ecto.Query.t()
Build a complete Ecto.Query with joins applied for relationship filtering. Returns a query that can be passed to Repo.all/one/etc.
Options
:schema- Schema module for semantic analysis (enables block syntax likeitems { name:foo }):preload- Preloads to apply to the query