Muninn.Schema (Muninn v0.5.5)

View Source

Schema definition for Muninn search indices.

A schema defines the structure of documents in an index, including field names, types, and indexing options.

Example

schema = Muninn.Schema.new()
  |> Muninn.Schema.add_text_field("title", stored: true)
  |> Muninn.Schema.add_text_field("body", stored: true)

Summary

Functions

Adds a boolean field to the schema.

Adds a bytes (binary) field to the schema.

Adds a 64-bit floating point field to the schema.

Adds a signed 64-bit integer field to the schema.

Adds a text field to the schema.

Adds an unsigned 64-bit integer field to the schema.

Creates a new empty schema.

Converts the schema to a map representation for NIF consumption.

Validates the schema.

Types

t()

@type t() :: %Muninn.Schema{fields: [Muninn.Schema.Field.t()]}

Functions

add_bool_field(schema, name, opts \\ [])

@spec add_bool_field(t(), String.t(), keyword()) :: t()

Adds a boolean field to the schema.

Options

  • :stored - Whether to store the field value (default: false)
  • :indexed - Whether to index the field (default: true)

Examples

iex> schema = Muninn.Schema.new()
iex> schema = Muninn.Schema.add_bool_field(schema, "published", stored: true)
iex> hd(schema.fields).type
:bool

add_bytes_field(schema, name, opts \\ [])

@spec add_bytes_field(t(), String.t(), keyword()) :: t()

Adds a bytes (binary) field to the schema.

Options

  • :stored - Whether to store the field value (default: false)
  • :indexed - Whether to index the field (default: true)
  • :fast - Whether to enable fast field storage (default: false)

Examples

iex> schema = Muninn.Schema.new()
iex> schema = Muninn.Schema.add_bytes_field(schema, "payload", stored: true)
iex> hd(schema.fields).type
:bytes

add_f64_field(schema, name, opts \\ [])

@spec add_f64_field(t(), String.t(), keyword()) :: t()

Adds a 64-bit floating point field to the schema.

Options

  • :stored - Whether to store the field value (default: false)
  • :indexed - Whether to index the field (default: true)

Examples

iex> schema = Muninn.Schema.new()
iex> schema = Muninn.Schema.add_f64_field(schema, "price", stored: true)
iex> hd(schema.fields).type
:f64

add_i64_field(schema, name, opts \\ [])

@spec add_i64_field(t(), String.t(), keyword()) :: t()

Adds a signed 64-bit integer field to the schema.

Options

  • :stored - Whether to store the field value (default: false)
  • :indexed - Whether to index the field (default: true)

Examples

iex> schema = Muninn.Schema.new()
iex> schema = Muninn.Schema.add_i64_field(schema, "temperature", stored: true)
iex> hd(schema.fields).type
:i64

add_text_field(schema, name, opts \\ [])

@spec add_text_field(t(), String.t(), keyword()) :: t()

Adds a text field to the schema.

Options

  • :stored - Whether to store the field value (default: false)
  • :indexed - Whether to index the field for searching (default: true)

Examples

iex> schema = Muninn.Schema.new()
iex> schema = Muninn.Schema.add_text_field(schema, "title", stored: true)
iex> length(schema.fields)
1

add_u64_field(schema, name, opts \\ [])

@spec add_u64_field(t(), String.t(), keyword()) :: t()

Adds an unsigned 64-bit integer field to the schema.

Options

  • :stored - Whether to store the field value (default: false)
  • :indexed - Whether to index the field (default: true)

Examples

iex> schema = Muninn.Schema.new()
iex> schema = Muninn.Schema.add_u64_field(schema, "count", stored: true)
iex> hd(schema.fields).type
:u64

new()

@spec new() :: t()

Creates a new empty schema.

Examples

iex> schema = Muninn.Schema.new()
iex> schema.fields
[]

to_map(schema)

@spec to_map(t()) :: map()

Converts the schema to a map representation for NIF consumption.

Examples

iex> schema = Muninn.Schema.new() |> Muninn.Schema.add_text_field("title", stored: true)
iex> map = Muninn.Schema.to_map(schema)
iex> is_map(map)
true

validate(schema)

@spec validate(t()) :: :ok | {:error, atom()}

Validates the schema.

Returns :ok if valid, or {:error, reason} if invalid.

Examples

iex> schema = Muninn.Schema.new() |> Muninn.Schema.add_text_field("title")
iex> Muninn.Schema.validate(schema)
:ok

iex> schema = Muninn.Schema.new()
iex> Muninn.Schema.validate(schema)
{:error, :no_fields}