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:

  1. Wraps base query with :q alias via ensure_base_alias/1
  2. Applies LEFT JOINs in dependency order via apply_joins/2
  3. Adds the WHERE clause with the dynamic expression
  4. Optionally adds preloads

Example Flow

For query "author.name:Tolkien":

  1. Parser produces: {:==, [], [[:author, :name], "Tolkien"]}
  2. Context.ensure_joins/2 registers join for [:author] → returns :join_author
  3. build_dynamic/4 creates: dynamic([{:join_author, x}], field(x, :name) == "Tolkien")
  4. apply_joins/2 adds: join(:left, [{:q, p}], a in assoc(p, :author), as: :join_author)
  5. 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

Build filter with join tracking. Returns {dynamic, context}.

Link to this function

query(base_query, input, opts \\ [])

View Source
@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 like items { name:foo })
  • :preload - Preloads to apply to the query