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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1154f0138248270d73d1681962c7bfecc8f81e074104a24071d32d76d5405705
4
- data.tar.gz: 23c823cc022efcfebf9533d0388dd3abaec0fca61cc7e0911a2972dfb3bf429b
3
+ metadata.gz: 0d009ddc4383b98d64d9e9a5b4e5371261af7abde22470a7f3fa9b021f9040b4
4
+ data.tar.gz: b83f4e909fa613e82e64687d5d6014be9c28bd314c52780aa04a312f9dbcc3be
5
5
  SHA512:
6
- metadata.gz: 67cc38e53cb43b8775561dd234cf073d2d45115b1c517aa32f890ab9f3d059106915158ee8bc08d2af9091e2107273ba9a65da6c66a1646afc419b62a8a669e0
7
- data.tar.gz: 63cf2a9765043884726a59a6c058730d004be2b15bed0565be70579ef1717b322bb424e1311ced9a5685c8f45b7521333b0c66cca42c83cd52cb94cd13a7a5fd
6
+ metadata.gz: 8ba200a4bdf3004dbb4b033e12bd2b9c5fd0be91dd01bd35e11ceeb862483e038affa3781c62307729cd790ae7ed5832cbb28aba1edce240250fd4e2931033cb
7
+ data.tar.gz: e8924a3d4bae4d872ba6d2ce4f6afe0dca62984156210d05e51c847e2d3f3be0dc258e78160f504ee7e9c0650078628bd471174b07f08d65b3079efcca72e155
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## 1.2.0 (2026-06-04)
2
+
3
+ - Added `threshold` option
4
+
5
+ ## 1.1.1 (2026-05-14)
6
+
7
+ - Fixed `sqlite3 is not part of the bundle` error
8
+
1
9
  ## 1.1.0 (2026-05-14)
2
10
 
3
11
  - Added experimental support for SQLite with no extension
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) - experimental
10
- - SQLite - experimental
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
  ```
@@ -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)
@@ -1,3 +1,3 @@
1
1
  module Neighbor
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
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
- Neighbor::SQLite.initialize_adapter!
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)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: neighbor
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane