Class: Hnswlib::HnswIndex Deprecated
- Inherits:
-
Object
- Object
- Hnswlib::HnswIndex
- Defined in:
- lib/hnswlib.rb
Overview
This class was prepared as a class with an interface similar to Annoy, but it is not very useful and will be deleted in the next version.
HnswIndex is a class that provides functions for k-nearest eighbors search.
Instance Attribute Summary collapse
-
#metric ⇒ String
readonly
Returns the metric of index.
Instance Method Summary collapse
-
#add_item(i, v, replace_removed: false) ⇒ Boolean
Add item to be indexed.
-
#get_distance(i, j) ⇒ Float or Integer
Calculate the distances between items.
-
#get_item(i) ⇒ Array
Return the item vector.
-
#get_nns_by_item(i, n, include_distances: false, filter: nil) ⇒ Array<Integer> or Array<Array<Integer>, Array<Float>>
Search the n closest items.
-
#get_nns_by_vector(v, n, include_distances: false, filter: nil) ⇒ Array<Integer> or Array<Array<Integer>, Array<Float>>
Search the n closest items.
-
#initialize(n_features:, max_item:, metric: 'l2', m: 16, ef_construction: 200, random_seed: 100, allow_replace_removed: false) ⇒ HnswIndex
constructor
Create a new search index.
-
#load(filename, allow_replace_removed: false) ⇒ Object
Load a search index from disk.
-
#max_item ⇒ Integer
Return the maximum number of items.
-
#n_features ⇒ Integer
Returns the number of features of indexed item.
-
#n_items ⇒ Integer
Return the number of items in the search index.
-
#remove_item(i) ⇒ Object
Remove the item vector.
-
#resize_index(new_max_item) ⇒ Object
Reize the search index.
-
#save(filename) ⇒ Object
Save the search index to disk.
-
#set_ef(ef) ⇒ Object
Set the size of the dynamic list for the nearest neighbors.
Constructor Details
#initialize(n_features:, max_item:, metric: 'l2', m: 16, ef_construction: 200, random_seed: 100, allow_replace_removed: false) ⇒ HnswIndex
Create a new search index.
36 37 38 39 40 41 42 43 |
# File 'lib/hnswlib.rb', line 36 def initialize(n_features:, max_item:, metric: 'l2', m: 16, ef_construction: 200, random_seed: 100, allow_replace_removed: false) @metric = metric space = @metric == 'dot' ? 'ip' : 'l2' @index = Hnswlib::HierarchicalNSW.new(space: space, dim: n_features) @index.init_index(max_elements: max_item, m: m, ef_construction: ef_construction, random_seed: random_seed, allow_replace_deleted: allow_replace_removed) end |
Instance Attribute Details
#metric ⇒ String (readonly)
Returns the metric of index.
25 26 27 |
# File 'lib/hnswlib.rb', line 25 def metric @metric end |
Instance Method Details
#add_item(i, v, replace_removed: false) ⇒ Boolean
Add item to be indexed.
51 52 53 |
# File 'lib/hnswlib.rb', line 51 def add_item(i, v, replace_removed: false) @index.add_point(v, i, replace_deleted: replace_removed) end |
#get_distance(i, j) ⇒ Float or Integer
Calculate the distances between items.
129 130 131 132 133 |
# File 'lib/hnswlib.rb', line 129 def get_distance(i, j) vi = @index.get_point(i) vj = @index.get_point(j) @index.space.distance(vi, vj) end |
#get_item(i) ⇒ Array
Return the item vector.
59 60 61 |
# File 'lib/hnswlib.rb', line 59 def get_item(i) @index.get_point(i) end |
#get_nns_by_item(i, n, include_distances: false, filter: nil) ⇒ Array<Integer> or Array<Array<Integer>, Array<Float>>
Search the n closest items.
77 78 79 80 81 |
# File 'lib/hnswlib.rb', line 77 def get_nns_by_item(i, n, include_distances: false, filter: nil) v = @index.get_point(i) ids, dists = @index.search_knn(v, n, filter: filter) include_distances ? [ids, dists] : ids end |
#get_nns_by_vector(v, n, include_distances: false, filter: nil) ⇒ Array<Integer> or Array<Array<Integer>, Array<Float>>
Search the n closest items.
90 91 92 93 |
# File 'lib/hnswlib.rb', line 90 def get_nns_by_vector(v, n, include_distances: false, filter: nil) ids, dists = @index.search_knn(v, n, filter: filter) include_distances ? [ids, dists] : ids end |
#load(filename, allow_replace_removed: false) ⇒ Object
Load a search index from disk.
120 121 122 |
# File 'lib/hnswlib.rb', line 120 def load(filename, allow_replace_removed: false) @index.load_index(filename, allow_replace_deleted: allow_replace_removed) end |
#max_item ⇒ Integer
Return the maximum number of items.
152 153 154 |
# File 'lib/hnswlib.rb', line 152 def max_item @index.max_elements end |
#n_features ⇒ Integer
Returns the number of features of indexed item.
145 146 147 |
# File 'lib/hnswlib.rb', line 145 def n_features @index.space.dim end |
#n_items ⇒ Integer
Return the number of items in the search index.
138 139 140 |
# File 'lib/hnswlib.rb', line 138 def n_items @index.current_count end |
#remove_item(i) ⇒ Object
Remove the item vector.
66 67 68 |
# File 'lib/hnswlib.rb', line 66 def remove_item(i) @index.mark_deleted(i) end |
#resize_index(new_max_item) ⇒ Object
Reize the search index.
98 99 100 |
# File 'lib/hnswlib.rb', line 98 def resize_index(new_max_item) @index.reisze_index(new_max_item) end |
#save(filename) ⇒ Object
Save the search index to disk.
112 113 114 |
# File 'lib/hnswlib.rb', line 112 def save(filename) @index.save_index(filename) end |
#set_ef(ef) ⇒ Object
Set the size of the dynamic list for the nearest neighbors.
105 106 107 |
# File 'lib/hnswlib.rb', line 105 def set_ef(ef) @index.set_ef(ef) end |