View Source Paradex (Paradex v0.0.1)
A collection of macros for composing ParadeDB queries.
Summary
Macros
Returns the BM25 search score for each result, aliasing it as pdb_score
in the query. Can be used with score/0
to order results
Same as score/1
but uses a variable for the alias instead. Does not support literals.
Macro for paradedb.snippet, used for highlighting.
Macro for the @@@
full text search operator.
Term-level queries
Macro for paradedb.fuzzy_term.
Macro for paradedb.exists. Prefixed with pdb_
to avoid conflicting with Ecto.Query.API.exists/1
.
Macro for paradedb.range_term
Macro for paradedb.regex. Be mindful that these regular expressions follow Tantivy's syntax, which follows Rust's regex crate with a few variations.
Macro for paradedb.term
Macro for paradedb.term_set
Range queries
Macro for paradedb.range using the date
data type.
Macro for paradedb.range using the int4
data type.
Macro for paradedb.range using the int8
data type.
Macro for paradedb.range using the timestamp
data type.
Phrase-level queries
Macro for paradedb.fuzzy_phrase.
Compound queries
Macro for paradedb.all.
Macro for paradedb.boost.
Macro for paradedb.const_score.
Macro for paradedb.empty
Macro for paradedb.parse
Macros
Returns the BM25 search score for each result, aliasing it as pdb_score
in the query. Can be used with score/0
to order results:
from(
c in Call,
select: {c, score(c.id)},
where: c.transcript ~> "mechanic",
order_by: [desc: score()]
)
Same as score/1
but uses a variable for the alias instead. Does not support literals.
score_alias = "my_score"
from(
c in Call,
select: {c, score_as(c.id, ^score_alias)},
where: c.transcript ~> "mechanic",
order_by: [desc: score_as(^score_alias)]
)
snippet(field, start_tag \\ "<b>", end_tag \\ "</b>", max_num_chars \\ 150)
View Source (macro)Macro for paradedb.snippet, used for highlighting.
from(
c in Call,
select: {c, snippet(c.transcript)},
where: c.transcript ~> "mechanic"
)
Macro for the @@@
full text search operator.
~>
is used as it's one of a few infix operators Elixir's capable of parsing, but aren't presently used.
Examples
Search queries can be run on fields directly:
import Paradex
from(
c in Call,
where: c.transcript ~> "bus"
)
Alternatively a key field and query object can be used for advanced queries:
from(
c in Call,
where: c.id ~> disjunction_max([
parse("transcript:bus"),
int4range("call_length", 10, nil, "[)")
])
)
Term-level queries
fuzzy_term(field, value, distance \\ 2, transpose_cost_one \\ true, prefix \\ false)
View Source (macro)Macro for paradedb.fuzzy_term.
from(
c in Call,
where: c.id ~> fuzzy_term("transcript", "bus", 2, true, false)
)
Macro for paradedb.exists. Prefixed with pdb_
to avoid conflicting with Ecto.Query.API.exists/1
.
from(
c in Call,
where: c.id ~> pdb_exists("call_length")
)
Macro for paradedb.range_term
Macro for paradedb.regex. Be mindful that these regular expressions follow Tantivy's syntax, which follows Rust's regex crate with a few variations.
from(
c in Call,
where: c.id ~> regex("transcript", "(stop|route)")
)
Macro for paradedb.term
from(
c in Call,
where: c.id ~> term("talkgroup_num", 7695)
)
Macro for paradedb.term_set
from(
c in Call,
where: c.id ~> term_set([
term("talkgroup_num", 7700),
term("call_length", 20)
])
)
Range queries
Macro for paradedb.range using the date
data type.
start = ~D[2024-10-09]
stop = ~D[2024-10-10]
query =
from(
c in Call,
where: c.id ~> daterange("start_time", ^start, ^stop, "[]")
)
Macro for paradedb.range using the int4
data type.
from(
c in Call,
where: c.id ~> int4range("call_length", 5, nil, "[)")
)
Macro for paradedb.range using the int8
data type.
from(
c in Call,
where: c.id ~> int8range("call_length", 5, nil, "[)")
)
Macro for paradedb.range using the timestamp
data type.
begin = ~U[2024-10-09 08:00:00.00Z]
query =
from(
c in Call,
where: c.id ~> tsrange("start_time", ^begin, nil, "[)")
)
Phrase-level queries
fuzzy_phrase(field, value, distance \\ 2, transpose_cost_one \\ true, prefix \\ false, match_all_terms \\ false)
View Source (macro)Macro for paradedb.fuzzy_phrase.
from(
c in Call,
where: c.id ~> fuzzy_phrase("transcript", "bus sotp")
)
Macro for paradedb.phrase
from(
c in Call,
where: c.id ~> phrase("transcript", ["bus", "stop"], 1)
)
Macro for paradedb.phrase_prefix
from(
c in Call,
where: c.id ~> phrase_prefix("transcript", ["en"])
)
Compound queries
Macro for paradedb.all.
from(
c in Call,
where: c.id ~> all()
)
Macro for paradedb.boolean.
Each value must be a query object, or a list of query objects.
Macro for paradedb.boost.
from(
c in Call,
select: {c. score(c.id)},
boost(2.0, "transcript:bus")
)
Macro for paradedb.const_score.
from(
c in Call,
select: {c, score(c.id)},
where: c.id ~> const_score(2.0, parse("transcript:bus"))
)
Macro for paradedb.disjunction_max
from(
c in Call,
where:
c.id ~> disjunction_max([
parse("transcript:bus"),
int4range("call_length", 10, nil, "[)")
])
)
Macro for paradedb.empty
from(
c in Call,
where: c.id ~> empty()
)
Macro for paradedb.parse
from(
c in Call,
where: c.id ~> parse("transcript:bus")
)
Macro for paradedb.parse_with_field
from(
c in Call,
where: c.id ~> parse_with_field("transcript", "traffic congestion")
)