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.
38 39 40 41 42 43 44 45 |
# File 'lib/hnswlib.rb', line 38 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.
27 28 29 |
# File 'lib/hnswlib.rb', line 27 def metric @metric end |
Instance Method Details
#add_item(i, v, replace_removed: false) ⇒ Boolean
Add item to be indexed.
53 54 55 |
# File 'lib/hnswlib.rb', line 53 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.
131 132 133 134 135 |
# File 'lib/hnswlib.rb', line 131 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.
61 62 63 |
# File 'lib/hnswlib.rb', line 61 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.
79 80 81 82 83 |
# File 'lib/hnswlib.rb', line 79 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.
92 93 94 95 |
# File 'lib/hnswlib.rb', line 92 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.
122 123 124 |
# File 'lib/hnswlib.rb', line 122 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.
154 155 156 |
# File 'lib/hnswlib.rb', line 154 def max_item @index.max_elements end |
#n_features ⇒ Integer
Returns the number of features of indexed item.
147 148 149 |
# File 'lib/hnswlib.rb', line 147 def n_features @index.space.dim end |
#n_items ⇒ Integer
Return the number of items in the search index.
140 141 142 |
# File 'lib/hnswlib.rb', line 140 def n_items @index.current_count end |
#remove_item(i) ⇒ Object
Remove the item vector.
68 69 70 |
# File 'lib/hnswlib.rb', line 68 def remove_item(i) @index.mark_deleted(i) end |
#resize_index(new_max_item) ⇒ Object
Reize the search index.
100 101 102 |
# File 'lib/hnswlib.rb', line 100 def resize_index(new_max_item) @index.reisze_index(new_max_item) end |
#save(filename) ⇒ Object
Save the search index to disk.
114 115 116 |
# File 'lib/hnswlib.rb', line 114 def save(filename) @index.save_index(filename) end |
#set_ef(ef) ⇒ Object
Set the size of the dynamic list for the nearest neighbors.
107 108 109 |
# File 'lib/hnswlib.rb', line 107 def set_ef(ef) @index.set_ef(ef) end |