NGT
NGT - high-speed approximate nearest neighbors - for Ruby
Installation
First, install NGT. For Homebrew, use:
brew install ngt
Add this line to your application’s Gemfile:
gem 'ngt'
Getting Started
Prep your data
objects = [
[1, 1, 2, 1],
[5, 4, 6, 5],
[1, 2, 1, 2]
]
Create an index
index = Ngt::Index.create(path, dimensions)
Insert objects
index.batch_insert(objects)
Search the index
index.search(query, size: 3)
Save the index
index.save
Load an index
index = Ngt::Index.new(path)
Get an object by id
index.object(id)
Insert a single object
index.insert(object)
Remove an object by id
index.remove(id)
Build the index
index.build_index
Full Example
dim = 10
objects = []
100.times do |i|
objects << dim.times.map { rand(100) }
end
index = Ngt::Index.create("tmp", dim)
index.batch_insert(objects)
index.save
query = objects[0]
result = index.search(query, size: 3)
result.each do |res|
puts "#{res[:id]}, #{res[:distance]}"
p index.object(res[:id])
end
Data
Data can be an array of arrays
[[1, 2, 3], [4, 5, 6]]
Or a Numo NArray
Numo::DFloat.new(3, 2).seq
Resources
Credits
This library is modeled after NGT’s Python API.
History
View the changelog
Contributing
Everyone is encouraged to help improve this project. Here are a few ways you can help:
- Report bugs
- Fix bugs and submit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features