pgvector-elixir

pgvector examples for Elixir

Build Status

Getting Started

Follow the instructions for your database library:

Ecto

Create a migration

mix ecto.gen.migration create_vector_extension

with:

defmodule App.Repo.Migrations.CreateVectorExtension do
  use Ecto.Migration

  def up do
    execute "CREATE EXTENSION IF NOT EXISTS vector"
  end

  def down do
    execute "DROP EXTENSION vector"
  end
end

Run the migration

mix ecto.migrate

You can now use the vector type in future migrations

create table(:items) do
  add :factors, :vector, size: 3
end

Insert a vector

Ecto.Adapters.SQL.query!(App.Repo, "INSERT INTO items (factors) VALUES ('[1,2,3]')")

Get the nearest neighbors

import Ecto.Query

App.Repo.all(from i in "items", order_by: fragment("factors <-> ?", "[1,2,3]"), limit: 5, select: i.id)

Add an approximate index in a migration

create index("items", ["factors vector_l2_ops"], using: :ivfflat)

Use vector_ip_ops for inner product and vector_cosine_ops for cosine distance

History

View the changelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

To get started with development:

git clone https://github.com/pgvector/pgvector-elixir.git
cd pgvector-elixir
mix deps.get
createdb pgvector_elixir_test
mix run postgrex/example.exs