Class: Hnswlib::HierarchicalNSW

Inherits:
Object
  • Object
show all
Defined in:
ext/hnswlib/dummy.rb

Overview

HierarchicalNSW is a class that provides functions for approximate k-NN search. This class is used internally.

Examples:

require 'hnswlib'

n_features = 32
max_elements = 100

index = HierarchicalNSW.new(space: space, dim: n_features)
index.init_index(max_elements: max_elements)

max_elements.times do |i|
  vec = Array.new(n_features) { rand }
  index.add_point(vec, i)
end

query = Array.new(n_features) { rand }
index.search_knn(query, 10)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(space:, dim:) ⇒ HierarchicalNSW

Create a new HierarchicalNSW.

Parameters:

  • space (String)

    The metric space name of search index (‘l2’, ‘ip’, or ‘cosine’).

  • dim (Integer)

    The number of dimensions (features).



99
# File 'ext/hnswlib/dummy.rb', line 99

def initialize(space:, dim:); end

Instance Attribute Details

#spaceL2Space | InnerProductSpace (readonly)

Returns the metric space of search index.

Returns:



93
94
95
# File 'ext/hnswlib/dummy.rb', line 93

def space
  @space
end

Instance Method Details

#add_point(arr, idx, replace_deleted: false) ⇒ Boolean

Add item to be indexed.

Parameters:

  • arr (Array)

    The vector of item.

  • idx (Integer)

    The ID of item.

  • replace_deleted (Boolean) (defaults to: false)

    The flag to replace a deleted item.

Returns:

  • (Boolean)


117
# File 'ext/hnswlib/dummy.rb', line 117

def add_point(arr, idx, replace_deleted: false); end

#current_countInteger

Return the number of items in the search index.

Returns:

  • (Integer)


172
# File 'ext/hnswlib/dummy.rb', line 172

def current_count; end

#get_idsArray

Return the indices of stored items.

Returns:

  • (Array)


147
# File 'ext/hnswlib/dummy.rb', line 147

def get_ids; end

#get_point(idx) ⇒ Array

Return the item vector.

Parameters:

  • idx (Integer)

    The ID of item.

Returns:

  • (Array)


142
# File 'ext/hnswlib/dummy.rb', line 142

def get_point(idx); end

#init_index(max_elements:, m: 16, ef_construction: 200, random_seed: 100, allow_replace_deleted: false) ⇒ Nil

Intialize search index.

Parameters:

  • max_elements (Integer)

    The maximum number of items.

  • m (Integer) (defaults to: 16)

    The maximum number of outgoing connections in the graph

  • ef_construction (Integer) (defaults to: 200)

    The size of the dynamic list for the nearest neighbors.

  • random_seed (Integer) (defaults to: 100)

    The seed value using to initialize the random generator.

  • allow_replace_deleted (Boolean) (defaults to: false)

    The flag to replace deleted item when adding new item.

Returns:

  • (Nil)


109
# File 'ext/hnswlib/dummy.rb', line 109

def init_index(max_elements:, m: 16, ef_construction: 200, random_seed: 100, allow_replace_deleted: false); end

#load_index(filename, allow_replace_deleted: false) ⇒ Object

Load a search index from disk.

Parameters:

  • filename (String)

    The filename of search index.

  • allow_replace_deleted (Boolean) (defaults to: false)

    The flag to replace deleted item when adding new item.



136
# File 'ext/hnswlib/dummy.rb', line 136

def load_index(filename, allow_replace_deleted: false); end

#mark_deleted(idx) ⇒ Object

Mark the item as deleted.

Parameters:

  • idx (Integer)

    The ID of item.



152
# File 'ext/hnswlib/dummy.rb', line 152

def mark_deleted(idx); end

#max_elementsInteger

Return the maximum number of items.

Returns:

  • (Integer)


167
# File 'ext/hnswlib/dummy.rb', line 167

def max_elements; end

#resize_index(new_max_item) ⇒ Object

Reize the search index.

Parameters:

  • new_max_item (Integer)

    The maximum number of items.



157
# File 'ext/hnswlib/dummy.rb', line 157

def resize_index(new_max_item); end

#save_index(filename) ⇒ Object

Save the search index to disk.

Parameters:

  • filename (String)

    The filename of search index.



130
# File 'ext/hnswlib/dummy.rb', line 130

def save_index(filename); end

#search_knn(arr, k, filter: nil) ⇒ Array<Array<Integer>, Array<Float>>

Search the k closest items.

Parameters:

  • arr (Array)

    The vector of query item.

  • k (Integer)

    The number of nearest neighbors.

  • filter (Proc) (defaults to: nil)

    The function that filters elements by its labels.

Returns:

  • (Array<Array<Integer>, Array<Float>>)


125
# File 'ext/hnswlib/dummy.rb', line 125

def search_knn(arr, k, filter: nil); end

#set_ef(new_ef) ⇒ Object

Set the size of the dynamic list for the nearest neighbors.

Parameters:

  • new_ef (Integer)

    The size of the dynamic list.



162
# File 'ext/hnswlib/dummy.rb', line 162

def set_ef(new_ef); end