Skip to content

Commit

Permalink
Improved docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Feb 28, 2024
1 parent 76d0dd5 commit bde72d9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ Pgvector::PG.register_vector(registry)
conn.type_map_for_results = PG::BasicTypeMapForResults.new(conn, registry: registry)
```

Create a table

```ruby
conn.exec("CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))")
```

Insert a vector

```ruby
Expand All @@ -56,6 +62,16 @@ Get the nearest neighbors to a vector
conn.exec_params("SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5", [embedding]).to_a
```

Add an approximate index

```ruby
conn.exec("CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)")
# or
conn.exec("CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)")
```

Use `vector_ip_ops` for inner product and `vector_cosine_ops` for cosine distance

## Sequel

Enable the extension
Expand Down Expand Up @@ -101,6 +117,14 @@ Get the nearest neighbors to a vector
Item.nearest_neighbors(:embedding, [1, 1, 1], distance: "euclidean").limit(5)
```

Add an approximate index

```ruby
DB.add_index :items, :embedding, type: "hnsw", opclass: "vector_l2_ops"
```

Use `vector_ip_ops` for inner product and `vector_cosine_ops` for cosine distance

## History

View the [changelog](https://github.com/pgvector/pgvector-ruby/blob/master/CHANGELOG.md)
Expand Down
1 change: 1 addition & 0 deletions test/sequel_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
primary_key :id
column :embedding, "vector(3)"
end
DB.add_index :sequel_items, :embedding, type: "hnsw", opclass: "vector_l2_ops"

class Item < Sequel::Model(DB[:sequel_items])
plugin :pgvector, :embedding
Expand Down

0 comments on commit bde72d9

Please sign in to comment.