QuackDB.FTS (quackdb v0.4.0)

Copy Markdown View Source

SQL helpers for DuckDB's full-text search extension.

These helpers return iodata for DuckDB FTS statements and expressions. DuckDB autoloads the fts extension on first use in many configurations, but you can install/load it explicitly:

alias QuackDB.FTS

QuackDB.query!(conn, FTS.install())
QuackDB.query!(conn, FTS.load())
QuackDB.query!(conn, FTS.create_index("documents", :id, [:title, :body]))

score = FTS.match_bm25(~s|"id"|, "duckdb analytics", schema: FTS.schema_name("main.documents"))
QuackDB.query!(conn, ["SELECT id, ", score, " AS score FROM documents ORDER BY score DESC"])

bm25/3 and search_score/3 are aliases for match_bm25/3. Use stem/2 for DuckDB's stemming helper.

Summary

Functions

Builds PRAGMA create_fts_index(...).

Builds PRAGMA drop_fts_index(...).

Builds an INSTALL fts; statement.

Builds a LOAD fts; statement.

Builds match_bm25(id, query, ...).

Returns DuckDB's generated FTS schema name for a table.

Alias for match_bm25/3 when using the expression as a score/rank.

Builds stem(text, stemmer).

Types

create_option()

@type create_option() ::
  {:stemmer, atom() | String.t()}
  | {:stopwords, atom() | String.t()}
  | {:ignore, String.t()}
  | {:strip_accents, boolean()}
  | {:lower, boolean()}
  | {:overwrite, boolean()}

match_option()

@type match_option() ::
  {:fields, [atom() | String.t()] | atom() | String.t()}
  | {:k, number()}
  | {:b, number()}
  | {:conjunctive, boolean()}

Functions

bm25(id_expression, query, options \\ [])

@spec bm25(iodata(), String.t(), [match_option()]) :: iodata()

Alias for match_bm25/3.

create_index(table, id_column, columns, options \\ [])

@spec create_index(
  atom() | String.t(),
  atom() | String.t(),
  [atom() | String.t()] | :all,
  [
    create_option()
  ]
) :: iodata()

Builds PRAGMA create_fts_index(...).

drop_index(table)

@spec drop_index(atom() | String.t()) :: iodata()

Builds PRAGMA drop_fts_index(...).

install()

@spec install() :: iodata()

Builds an INSTALL fts; statement.

load()

@spec load() :: iodata()

Builds a LOAD fts; statement.

match_bm25(id_expression, query, options \\ [])

@spec match_bm25(iodata(), String.t(), [match_option()]) :: iodata()

Builds match_bm25(id, query, ...).

schema_name(table)

@spec schema_name(atom() | String.t()) :: String.t()

Returns DuckDB's generated FTS schema name for a table.

search_score(id_expression, query, options \\ [])

@spec search_score(iodata(), String.t(), [match_option()]) :: iodata()

Alias for match_bm25/3 when using the expression as a score/rank.

stem(text_expression, stemmer \\ :porter)

@spec stem(iodata(), atom() | String.t()) :: iodata()

Builds stem(text, stemmer).