Skip to content
NGT - high-speed approximate nearest neighbors - for Ruby
Ruby Python Shell
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib Added optimizer Nov 6, 2019
test Added optimizer Nov 6, 2019
.gitignore First commit Oct 22, 2019
.travis.yml Added optimizer Nov 6, 2019
CHANGELOG.md Added dates to changelog [skip ci] Nov 14, 2019
Gemfile First commit Oct 22, 2019
LICENSE.txt First commit Oct 22, 2019
README.md Added instructions for getting started with development [skip ci] Nov 14, 2019
Rakefile First commit Oct 22, 2019
ngt.gemspec First commit Oct 22, 2019

README.md

NGT

NGT - high-speed approximate nearest neighbors - for Ruby

Build Status

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

Optimize the index [master]

optimizer = Ngt::Optimizer.new(outgoing: 10, incoming: 120)
optimizer.execute(path, new_path)

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:

To get started with development and testing:

git clone https://github.com/ankane/ngt.git
cd ngt
bundle install
bundle exec rake test
You can’t perform that action at this time.