Class: Hnswlib::BruteforceSearch

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

Overview

BruteforceSearch is a class that provides functions for exct k-NN search. This class is useful for evaluating the search accuracy and investigating the optimal hyperparameters of HierarchicalNSW.

Examples:

require 'hnswlib'

n_features = 32
max_elements = 100

index = BruteforceSearch.new(space: 'l2', 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:) ⇒ BruteforceSearch

Create a new BruteforceSearch.

Parameters:

  • space (String)

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



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

def initialize(space:); end

Instance Attribute Details

#spaceL2Space | InnerProductSpace (readonly)

Returns the metric space of search index.

Returns:



197
198
199
# File 'ext/hnswlib/dummy.rb', line 197

def space
  @space
end

Instance Method Details

#add_point(arr, idx) ⇒ Boolean

Add item to be indexed.

Parameters:

  • arr (Array)

    The vector of item.

  • idx (Integer)

    The ID of item.

Returns:

  • (Boolean)


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

def add_point(arr, idx); end

#current_countInteger

Return the number of items in the search index.

Returns:

  • (Integer)


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

def current_count; end

#init_index(max_elements:) ⇒ Nil

Initialize search index.

Parameters:

  • max_elements (Integer)

    The maximum number of items.

Returns:

  • (Nil)


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

def init_index(max_elements:); end

#load_index(filename) ⇒ Object

Load a search index from disk.

Parameters:

  • filename (String)

    The filename of search index.



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

def load_index(filename); end

#max_elementsInteger

Return the maximum number of items.

Returns:

  • (Integer)


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

def max_elements; end

#remove_point(idx) ⇒ Object

Remove the item from index.

Parameters:

  • idx (Integer)

    The ID of item.



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

def remove_point(idx); end

#save_index(filename) ⇒ Object

Save the search index to disk.

Parameters:

  • filename (String)

    The filename of search index.



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

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>>)


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

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