pgvector-elixir
pgvector examples for Elixir
Getting Started
Follow the instructions for your database library:
Ecto
Create a migration
mix ecto.gen.migration create_vector_extensionwith:
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
endRun the migration
mix ecto.migrate
You can now use the vector type in future migrations
create table(:items) do
add :factors, :vector, size: 3
endInsert 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:
- Report bugs
- Fix bugs and submit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features
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