Hnswlib.rb provides Ruby bindings for the Hnswlib that implements approximate nearest-neghbor search based on hierarchical navigable small world graphs.
Add this line to your application's Gemfile:
gem 'hnswlib'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install hnswlib
Note: hnswlib.rb gives no optimization options when building native extensions. If necessary, add the optimization options yourself during installation, as follows;
$ bundle config --local build.hnswlib "--with-cxxflags=-march=native"
$ bundle install
Or:
$ gem install hnswlib -- --with-cxxflags=-march=native
require 'hnswlib'
f = 40 # length of datum point vector that will be indexed.
t = Hnswlib::HierarchicalNSW.new(space: 'l2', dim: f)
t.init_index(max_elements: 1000)
1000.times do |i|
v = Array.new(f) { rand }
t.add_point(v, i)
end
t.save_index('test.ann')
require 'hnswlib'
u = Hnswlib::HierarchicalNSW.new(space: 'l2', dim: f)
u.load_index('test.ann')
q = Array.new(f) { rand }
p u.search_knn(q, 100) # will find the 100 nearest neighbors.
The gem is available as open source under the terms of the Apache-2.0 License.
Bug reports and pull requests are welcome on GitHub at https://github.com/yoshoku/hnswlib.rb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.