neighbor 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/README.md +12 -4
- data/lib/neighbor/model.rb +9 -2
- data/lib/neighbor/version.rb +1 -1
- data/lib/neighbor.rb +6 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0d009ddc4383b98d64d9e9a5b4e5371261af7abde22470a7f3fa9b021f9040b4
|
|
4
|
+
data.tar.gz: b83f4e909fa613e82e64687d5d6014be9c28bd314c52780aa04a312f9dbcc3be
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8ba200a4bdf3004dbb4b033e12bd2b9c5fd0be91dd01bd35e11ceeb862483e038affa3781c62307729cd790ae7ed5832cbb28aba1edce240250fd4e2931033cb
|
|
7
|
+
data.tar.gz: e8924a3d4bae4d872ba6d2ce4f6afe0dca62984156210d05e51c847e2d3f3be0dc258e78160f504ee7e9c0650078628bd471174b07f08d65b3079efcca72e155
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
|
@@ -5,9 +5,9 @@ Nearest neighbor search for Rails
|
|
|
5
5
|
Supports:
|
|
6
6
|
|
|
7
7
|
- Postgres (pgvector and cube)
|
|
8
|
-
- MariaDB 11.8
|
|
9
|
-
- MySQL 9 (searching requires HeatWave)
|
|
10
|
-
- SQLite
|
|
8
|
+
- MariaDB 11.8+
|
|
9
|
+
- MySQL 9.7+ (searching requires HeatWave)
|
|
10
|
+
- SQLite
|
|
11
11
|
|
|
12
12
|
Also available for [Redis](https://github.com/ankane/neighbor-redis) and [S3 Vectors](https://github.com/ankane/neighbor-s3)
|
|
13
13
|
|
|
@@ -91,6 +91,12 @@ nearest_item = item.nearest_neighbors(:embedding, distance: "euclidean").first
|
|
|
91
91
|
nearest_item.neighbor_distance
|
|
92
92
|
```
|
|
93
93
|
|
|
94
|
+
Get the nearest neighbors within a distance threshold
|
|
95
|
+
|
|
96
|
+
```ruby
|
|
97
|
+
item.nearest_neighbors(:embedding, distance: "euclidean", threshold: 0.5).first(5)
|
|
98
|
+
```
|
|
99
|
+
|
|
94
100
|
See the additional docs for:
|
|
95
101
|
|
|
96
102
|
- [pgvector](#pgvector)
|
|
@@ -923,12 +929,14 @@ bundle exec rake test:postgresql
|
|
|
923
929
|
|
|
924
930
|
# SQLite
|
|
925
931
|
bundle exec rake test:sqlite
|
|
932
|
+
bundle exec rake test:sqlitevec
|
|
933
|
+
bundle exec rake test:vec1
|
|
926
934
|
|
|
927
935
|
# MariaDB
|
|
928
936
|
docker run -e MARIADB_ALLOW_EMPTY_ROOT_PASSWORD=1 -e MARIADB_DATABASE=neighbor_test -p 3307:3306 mariadb:11.8
|
|
929
937
|
bundle exec rake test:mariadb
|
|
930
938
|
|
|
931
939
|
# MySQL
|
|
932
|
-
docker run -e MYSQL_ALLOW_EMPTY_PASSWORD=1 -e MYSQL_DATABASE=neighbor_test -p 3306:3306 mysql:9
|
|
940
|
+
docker run -e MYSQL_ALLOW_EMPTY_PASSWORD=1 -e MYSQL_DATABASE=neighbor_test -p 3306:3306 mysql:9.7
|
|
933
941
|
bundle exec rake test:mysql
|
|
934
942
|
```
|
data/lib/neighbor/model.rb
CHANGED
|
@@ -60,7 +60,7 @@ module Neighbor
|
|
|
60
60
|
end
|
|
61
61
|
end
|
|
62
62
|
|
|
63
|
-
scope :nearest_neighbors, ->(attribute_name, vector, distance:, precision: nil) {
|
|
63
|
+
scope :nearest_neighbors, ->(attribute_name, vector, distance:, threshold: nil, precision: nil) {
|
|
64
64
|
attribute_name = attribute_name.to_sym
|
|
65
65
|
options = neighbor_attributes[attribute_name]
|
|
66
66
|
raise ArgumentError, "Invalid attribute" unless options
|
|
@@ -137,9 +137,16 @@ module Neighbor
|
|
|
137
137
|
|
|
138
138
|
# for select, use column_names instead of * to account for ignored columns
|
|
139
139
|
select_columns = select_values.any? ? [] : column_names
|
|
140
|
-
select(*select_columns, "#{neighbor_distance} AS neighbor_distance")
|
|
140
|
+
result = select(*select_columns, "#{neighbor_distance} AS neighbor_distance")
|
|
141
141
|
.where.not(attribute_name => nil)
|
|
142
142
|
.reorder(Arel.sql(order))
|
|
143
|
+
|
|
144
|
+
if threshold
|
|
145
|
+
op = distance == "inner_product" ? ">=" : "<="
|
|
146
|
+
result = result.where("#{neighbor_distance} #{op} ?", threshold)
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
result
|
|
143
150
|
}
|
|
144
151
|
|
|
145
152
|
def nearest_neighbors(attribute_name, **options)
|
data/lib/neighbor/version.rb
CHANGED
data/lib/neighbor.rb
CHANGED
|
@@ -30,7 +30,12 @@ ActiveSupport.on_load(:active_record) do
|
|
|
30
30
|
end
|
|
31
31
|
|
|
32
32
|
Neighbor::MySQL.initialize!
|
|
33
|
-
|
|
33
|
+
|
|
34
|
+
begin
|
|
35
|
+
Neighbor::SQLite.initialize_adapter!
|
|
36
|
+
rescue Gem::LoadError
|
|
37
|
+
# tries to load sqlite3 gem, which may not be available
|
|
38
|
+
end
|
|
34
39
|
end
|
|
35
40
|
|
|
36
41
|
require_relative "neighbor/railtie" if defined?(Rails::Railtie)
|