Sifter - a flexible string-based query filtering library for Ecto

Sifter is a a query filtering library for Ecto.

It lets frontend apps send human-readable query strings like:

"elixir phoenix status:published OR (draft author:me) createdAt:>=2024-01-01"

and turns them into safe, validated Ecto queries with full-text search support.

Key Features

  • Rich query syntax: field predicates, boolean logic, IN/NOT IN, wildcards, lists.
  • Full-text search: supports :ilike or PostgreSQL tsquery.
  • Field allow-lists: keep filtering secure with schema-aware whitelisting.
  • Friendly errors: lexer/parser messages with positions.
  • SQL inspection: Sifter.to_sql/4 for debugging and testing.

Example

{query, meta} = Sifter.filter!(Post, params["q"],
  schema: Post,
  allowed_fields: ["status", "priority", "author.name"],
  search_fields: ["title", "content"],
  search_strategy: {:tsquery, "english"}
)

posts = Repo.all(query)
IO.inspect(meta)
# %{uses_full_text?: true, recommended_order: [search_rank: :desc]}

GitHub Repo:

https://github.com/typhoonworks/sifter

6 Likes

Hi, nice library. Do you have any doc or formal reference for this query format? Is this query format used anywhere else?

Hi, thanks!

The grammar for the query is captured in the Lexer docs:

As for wether it is used elsewhere, I don’t think so. I implemented something similar in a previous job and at that time I drew heavily from Shopify’s search query sintax: Shopify API search syntax

So that’s likely the closest to this format that’s in use out there :slight_smile:

1 Like