Annoy.rb is a Ruby binding for the Annoy (Approximate Nearest Neighbors Oh Yeah).


Add this line to your application's Gemfile:

gem 'annoy-rb'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install annoy-rb

Note: Annoy.rb does not require the installation of another external library.



require 'annoy'

f = 40 # length of item vector that will be indexed.
t = f, metric: 'angular')

1000.times do |i|
  v = { rand }
  t.add_item(i, v)
end # 10 trees.'test.ann')

u = f, metric: 'angular')
p u.get_nns_by_item(0, 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 This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

Code of Conduct

Everyone interacting in the Annoy.rb project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.