# `Skeg.Vector`

Vector index commands.

    :ok = Skeg.Vector.create(conn, "docs", 768)
    :ok = Skeg.Vector.set(conn, "docs", 1, embedding, payload: "lang=it")
    {:ok, hits} = Skeg.Vector.search(conn, "docs", question, 10, filter: "lang = it")

Vectors are lists of numbers, or binaries of little-endian f32, and every
component must be finite in f32. IDs are integers from 0 to 2^64-1.
Arguments outside those ranges raise `ArgumentError` before anything is
sent. Every function takes `:timeout` in ms as an option.

# `error`

```elixir
@type error() :: {:error, Skeg.Error.t()}
```

# `id`

```elixir
@type id() :: non_neg_integer()
```

# `item`

```elixir
@type item() :: {id(), vector()} | {id(), vector(), binary()}
```

A row for `set_many/4`: `{id, vector}` stores no payload,
`{id, vector, payload}` stores a non-empty payload.

# `kind`

```elixir
@type kind() :: :f32 | :int8 | :binary | :tq1 | :tq2 | :tq4
```

How an index stores its vectors.

# `vector`

```elixir
@type vector() :: [number()] | binary()
```

# `create`

```elixir
@spec create(Skeg.conn(), String.t(), pos_integer(), keyword()) :: :ok | error()
```

Creates an index of `dim` dimensions: by default a durable disk index of
kind `:tq2`. Every setting is sent explicitly, so the result does not
depend on server defaults.

Options:

  * `:kind` - one of [:f32, :int8, :binary, :tq1, :tq2, :tq4], default `:tq2`
  * `:in_memory` - a flat in-memory index, removed when the server
    restarts. Default `false`.
  * `:search_list` - default search list length of a disk index
  * `:if_not_exists` - succeed without changes when an index with the
    same settings exists; one with other settings is still an error

# `delete`

```elixir
@spec delete(Skeg.conn(), String.t(), id(), keyword()) :: {:ok, boolean()} | error()
```

Deletes the row and reports whether a live row was removed.

# `drop`

```elixir
@spec drop(Skeg.conn(), String.t(), keyword()) :: :ok | error()
```

Removes the index and every vector in it.

# `get`

```elixir
@spec get(Skeg.conn(), String.t(), id(), keyword()) ::
  {:ok, [float()] | nil} | error()
```

The stored vector as a list of floats, or `nil` when the row does not exist.

# `info`

```elixir
@spec info(Skeg.conn(), String.t(), keyword()) ::
  {:ok, Skeg.Vector.Info.t()} | error()
```

# `list`

```elixir
@spec list(Skeg.conn(), keyword()) :: {:ok, [Skeg.Vector.Summary.t()]} | error()
```

The indexes visible to this connection's tenant.

# `max_batch_items`

Most items `set_many/4` sends in one request.

# `max_payload_bytes`

Largest payload stored with one vector.

# `search`

```elixir
@spec search(Skeg.conn(), String.t(), vector(), pos_integer(), keyword()) ::
  {:ok, [Skeg.Vector.Hit.t()]} | error()
```

The `k` rows nearest to `vector`, best first. The server reduces `k`
above its `max_k` limit.

Options:

  * `:search_list` - larger is slower and more accurate; `0`, the
    default, uses the index default. The server reduces values above
    its `max_l` limit.
  * `:with_payload` - return each hit's payload. Default `false`.
  * `:filter` - keep only rows whose payload matches, such as
    `"lang = it AND year >= 2020"`

# `set`

```elixir
@spec set(Skeg.conn(), String.t(), id(), vector(), keyword()) :: :ok | error()
```

Stores `vector` under `id`, replacing the row. Without `:payload` the row
keeps no payload; a payload of `key=value` fields can be used in search
filters.

# `set_many`

```elixir
@spec set_many(Skeg.conn(), String.t(), [item()], keyword()) ::
  {:ok, [:ok | {:error, Skeg.Error.t()}]} | error()
```

Stores many rows in one request. The result has one entry per item, in
order: `:ok` for a stored row, `{:error, %Skeg.Error{kind: :server}}` for
a row the server refused. A refusal of the whole request is the outer
`{:error, _}`; like any server error, it does not prove that no row was
stored.

At most `max_batch_items/0` items, each payload at most
`max_payload_bytes/0`; the server also refuses a batch whose vectors
exceed 64 MiB in total. A batch cannot carry an empty payload, because
the server stores it as no payload: use `{id, vector}`, or `set/5` with
`payload: ""`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
