View Source Paradex (Paradex v0.2.0)

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 the @@@ full text search operator.

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.

Macros

Link to this macro

score(key_field)

View Source (macro)

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()]
)
Link to this macro

score_as(key_field, as)

View Source (macro)

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)]
)
Link to this macro

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"
)
Link to this macro

field ~> query

View Source (macro)

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

Link to this macro

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)
)
Link to this macro

pdb_exists(field)

View Source (macro)

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")
)
Link to this macro

range_term(field, value)

View Source (macro)

Macro for paradedb.range_term.

Link to this macro

regex(field, pattern)

View Source (macro)

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)")
)
Link to this macro

term(field, value)

View Source (macro)

Macro for paradedb.term.

from(
  c in Call,
  where: c.id ~> term("talkgroup_num", 7695)
)
Link to this macro

term_set(terms)

View Source (macro)

Macro for paradedb.term_set.

from(
  c in Call,
  where: c.id ~> term_set([
    term("talkgroup_num", 7700),
    term("call_length", 20)
  ])
)

Range queries

Link to this macro

daterange(field, min, max, bounds)

View Source (macro)

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, "[]")
  )
Link to this macro

int4range(field, min, max, bounds)

View Source (macro)

Macro for paradedb.range using the int4 data type.

from(
  c in Call,
  where: c.id ~> int4range("call_length", 5, nil, "[)")
)
Link to this macro

int8range(field, min, max, bounds)

View Source (macro)

Macro for paradedb.range using the int8 data type.

from(
  c in Call,
  where: c.id ~> int8range("call_length", 5, nil, "[)")
)
Link to this macro

tsrange(field, min, max, bounds)

View Source (macro)

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

Link to this macro

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")
)
Link to this macro

phrase(field, phrases, slop \\ 0)

View Source (macro)

Macro for paradedb.phrase.

from(
  c in Call,
  where: c.id ~> phrase("transcript", ["bus", "stop"], 1)
)
Link to this macro

phrase_prefix(field, phrases, max_expansion \\ 0)

View Source (macro)

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()
)
Link to this macro

boolean(queries)

View Source (macro)

Macro for paradedb.boolean.

Each value must be a literal Keyword list at the top level, where each value is a list:

x = "transcript:transfer"

from(
  c in Call,
  where: c.id ~> boolean(
    must: [parse(^x)],
    must_not: [parse("transcript:station")]
  )
)

Keys other than must, should, and must_not are ignored, so be wary of misspelling.

Link to this macro

boost(boost, query)

View Source (macro)

Macro for paradedb.boost.

from(
  c in Call,
  select: {c. score(c.id)},
  boost(2.0, "transcript:bus")
)
Link to this macro

const_score(score, query)

View Source (macro)

Macro for paradedb.const_score.

from(
  c in Call,
  select: {c, score(c.id)},
  where: c.id ~> const_score(2.0, parse("transcript:bus"))
)
Link to this macro

disjunction_max(disjuncts, tie_breaker \\ 0.0)

View Source (macro)

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()
)
Link to this macro

parse(query, lenient \\ false, conjunction_mode \\ true)

View Source (macro)

Macro for paradedb.parse.

from(
  c in Call,
  where: c.id ~> parse("transcript:bus")
)
Link to this macro

parse_with_field(field, query, lenient \\ false, conjunction_mode \\ true)

View Source (macro)

Macro for paradedb.parse_with_field.

from(
  c in Call,
  where: c.id ~> parse_with_field("transcript", "traffic congestion")
)