Redis.Commands.Search (Redis v0.7.1)

Copy Markdown View Source

Command builders for RediSearch (FT.*) full-text search and indexing.

Provides pure functions for creating and managing search indexes, running full-text and numeric queries, performing aggregations, and managing auto-complete suggestion dictionaries. Includes a schema builder DSL that translates Elixir tuples into the FT.CREATE SCHEMA syntax.

All functions return command lists for use with Redis.command/2 or Redis.pipeline/2.

Examples

# Create a JSON index with text, numeric, and tag fields
Redis.command(conn, Search.create("idx:users", :json,
  prefix: "user:",
  schema: [
    {"$.name", :text, as: "name"},
    {"$.age", :numeric, as: "age", sortable: true},
    {"$.email", :tag, as: "email"}
  ]
))

# Full-text search with sorting and pagination
Redis.command(conn, Search.search("idx:users", "@name:Alice",
  sortby: {"age", :asc},
  limit: {0, 20},
  return: ["name", "age"]
))

# Aggregation with grouping and reduction
Redis.command(conn, Search.aggregate("idx:users", "*",
  groupby: ["@age"],
  reduce: [{"COUNT", 0, as: "count"}],
  sortby: [{"@count", :desc}],
  limit: {0, 10}
))

Summary

Functions

FT.AGGREGATE — run an aggregation query.

FT.ALTER — add a field to an existing index.

FT.CREATE — create a search index.

FT.DROPINDEX — drop an index.

FT.INFO — get index information.

FT._LIST — list all indexes.

FT.SEARCH — search an index.

FT.SUGADD — add a suggestion string.

FT.SUGDEL — delete a suggestion.

FT.SUGGET — get suggestion strings.

FT.SUGLEN — get number of suggestions.

FT.TAGVALS — get all distinct tag values for a field.

Functions

aggregate(index, query, opts \\ [])

@spec aggregate(String.t(), String.t(), keyword()) :: [String.t()]

FT.AGGREGATE — run an aggregation query.

Options

  • :groupby - list of fields to group by
  • :reduce - list of reduce functions {func, nargs} or {func, nargs, as: alias}
  • :sortby - [{field, :asc | :desc}]

  • :limit - {offset, count}
  • :apply - {expression, as: alias}
  • :filter - filter expression
  • :params - [{name, value}]
  • :dialect - query dialect version

alter(index, field_def)

@spec alter(String.t(), {String.t(), atom()} | {String.t(), atom(), keyword()}) :: [
  String.t()
]

FT.ALTER — add a field to an existing index.

create(index, type \\ :hash, opts)

@spec create(String.t(), :hash | :json, keyword()) :: [String.t()]

FT.CREATE — create a search index.

Options

  • :prefix - key prefix(es) to index (string or list)
  • :filter - filter expression
  • :language - default language
  • :score - default score
  • :stopwords - list of stopwords (or 0 for none)
  • :schema - list of field definitions (required)

Schema Fields

Each field is a tuple: {name_or_path, type} or {name_or_path, type, opts}

Types: :text, :tag, :numeric, :geo, :vector

Field options:

  • :as - alias name
  • :sortable - enable sorting
  • :noindex - store but don't index
  • :nostem - disable stemming (text)
  • :weight - field weight (text)
  • :separator - tag separator (default ",")

dropindex(index, opts \\ [])

@spec dropindex(
  String.t(),
  keyword()
) :: [String.t()]

FT.DROPINDEX — drop an index.

info(index)

@spec info(String.t()) :: [String.t()]

FT.INFO — get index information.

list()

@spec list() :: [String.t()]

FT._LIST — list all indexes.

search(index, query, opts \\ [])

@spec search(String.t(), String.t(), keyword()) :: [String.t()]

FT.SEARCH — search an index.

Options

  • :return - list of fields to return
  • :limit - {offset, count} (default: {0, 10})
  • :sortby - {field, :asc | :desc}

  • :nocontent - return only IDs
  • :verbatim - don't expand query terms
  • :params - [{name, value}] for parameterized queries
  • :dialect - query dialect version

sugadd(key, string, score, opts \\ [])

@spec sugadd(String.t(), String.t(), float(), keyword()) :: [String.t()]

FT.SUGADD — add a suggestion string.

sugdel(key, string)

@spec sugdel(String.t(), String.t()) :: [String.t()]

FT.SUGDEL — delete a suggestion.

sugget(key, prefix, opts \\ [])

@spec sugget(String.t(), String.t(), keyword()) :: [String.t()]

FT.SUGGET — get suggestion strings.

suglen(key)

@spec suglen(String.t()) :: [String.t()]

FT.SUGLEN — get number of suggestions.

tagvals(index, field)

@spec tagvals(String.t(), String.t()) :: [String.t()]

FT.TAGVALS — get all distinct tag values for a field.