View Source Dequel.Match (Dequel v0.7.0)

In-memory matching of Dequel queries against structs.

Tests if a preloaded struct matches a Dequel query. Supports relation traversal via dot notation and block syntax.

Usage

book = Repo.get(Book, 1) |> Repo.preload([:author, :tags])

# Simple field matching
Dequel.matches?(book, "title:*Rings")
# => true

# Dot notation for relations
Dequel.matches?(book, "author.country:UK")
# => true

# Block syntax (any semantics)
Dequel.matches?(book, "tags{name:fantasy}")
# => true if any tag has name "fantasy"

Preload Requirements

All associations referenced in the query must be preloaded. If a query references an unloaded association, Dequel.PreloadRequiredError is raised.

Use Dequel.preloads/1 to get the required preloads:

preloads = Dequel.preloads(query)
book = Repo.preload(book, preloads)
Dequel.matches?(book, query)

Summary

Functions

Tests if a struct matches a Dequel query.

Functions

@spec matches?(struct() | map(), binary() | tuple()) :: boolean()

Tests if a struct matches a Dequel query.

Accepts a struct and a query (string or parsed AST). Returns true if the struct matches all conditions in the query.

Raises Dequel.PreloadRequiredError if the query references an association that isn't loaded.

Examples

iex> book = %{title: "The Lord of the Rings", genre: "fantasy", page_count: 1178}
iex> Dequel.Match.matches?(book, "genre:fantasy")
true

iex> book = %{title: "The Lord of the Rings", genre: "fantasy", page_count: 1178}
iex> Dequel.Match.matches?(book, "title:*Rings")
true

iex> book = %{title: "The Lord of the Rings", genre: "fantasy", page_count: 1178}
iex> Dequel.Match.matches?(book, "page_count:>1000")
true

iex> book = %{title: "The Lord of the Rings", author: %{name: "Tolkien", country: "UK"}}
iex> Dequel.Match.matches?(book, "author.country:UK")
true

iex> book = %{title: "The Lord of the Rings", tags: [%{name: "fantasy"}, %{name: "classic"}]}
iex> Dequel.Match.matches?(book, "tags{name:fantasy}")
true

iex> Dequel.Match.matches?(%{name: "test"}, "")
true