API Reference

Information on the search methods (and their parameters) and spaces, can be found on this page.

nmslib.init

This function acts act the main entry point into NMS lib. This function should be called first before calling any other method.

nmslib.init(space: str='cosinesimil', space_params: object=None, method: str='hnsw', data_type: nmslib.DataType=DataType.DENSE_VECTOR, dtype: nmslib.DistType=DistType.FLOAT) → object

This function initializes a new NMSLIB index

Parameters:
  • space (str optional) – The metric space to create for this index
  • space_params (dict optional) – Parameters for configuring the space
  • method (str optional) – The index method to use
  • data_type (nmslib.DataType optional) – The type of data to index (dense/sparse/string vectors)
Returns:

Return type:

A new NMSLIB Index.

class nmslib.DistType
FLOAT
INT
class nmslib.DataType
DENSE_VECTOR
OBJECT_AS_STRING
SPARSE_VECTOR

nmslib.FloatIndex

nmslib.dist.FloatIndex

class nmslib.dist.FloatIndex
addDataPoint(self: nmslib.dist.FloatIndex, id: int, data: object) → int

Adds a single datapoint to the index

Parameters:
  • id (int) – The id of the object to add
  • data (object) – The object to add to the index.
Returns:

The position the item was added at

Return type:

int

addDataPointBatch(self: nmslib.dist.FloatIndex, data: object, ids: object=None) → int

Adds multiple datapoints to the index

Parameters:
  • data (object) – The objects to add to the index.
  • ids (array_like optional) – The ids of the object being inserted. If not set will default to the row id of each object in the dataset
Returns:

The number of items added

Return type:

int

createIndex(self: nmslib.dist.FloatIndex, index_params: object=None, print_progress: bool=False) → None

Creates the index, and makes it available for querying

Parameters:
  • index_params (dict optional) – Dictionary of optional parameters to use in indexing
  • print_progress (bool optional) – Whether or not to display progress bar when creating index
getDistance(self: nmslib.dist.FloatIndex, arg0: int, arg1: int) → float
knnQuery(self: nmslib.dist.FloatIndex, vector: object, k: int=10) → object

Finds the approximate K nearest neighbours of a vector in the index

Parameters:
  • vector (array_like) – A 1D vector to query for.
  • k (int optional) – The number of neighbours to return
Returns:

  • ids (array_like.) – A 1D vector of the ids of each nearest neighbour.
  • distances (array_like.) – A 1D vector of the distance to each nearest neigbhour.

knnQueryBatch(self: nmslib.dist.FloatIndex, queries: object, k: int=10, num_threads: int=0) → object

Performs multiple queries on the index, distributing the work over a thread pool

Parameters:
  • input (list) – A list of queries to query for
  • k (int optional) – The number of neighbours to return
  • num_threads (int optional) – The number of threads to use
Returns:

A list of tuples of (ids, distances)

Return type:

list

loadIndex(self: nmslib.dist.FloatIndex, filename: str, load_data: bool=False) → None

Loads the index from disk

Parameters:
  • filename (str) – The filename to read from
  • load_data (bool optional) – Whether or not to load previously saved data.
saveIndex(self: nmslib.dist.FloatIndex, filename: str, save_data: bool=False) → None

Saves the index and/or data to disk

Parameters:
  • filename (str) – The filename to save to
  • save_data (bool optional) – Whether or not to save data
setQueryTimeParams(self: nmslib.dist.FloatIndex, params: object=None) → None

Sets parameters used in knnQuery.

Parameters:params (dict) – A dictionary of params to use in querying. Setting params to None will reset

nmslib.IntIndex

nmslib.dist.IntIndex

class nmslib.dist.IntIndex
addDataPoint(self: nmslib.dist.IntIndex, id: int, data: object) → int

Adds a single datapoint to the index

Parameters:
  • id (int) – The id of the object to add
  • data (object) – The object to add to the index.
Returns:

The position the item was added at

Return type:

int

addDataPointBatch(self: nmslib.dist.IntIndex, data: object, ids: object=None) → int

Adds multiple datapoints to the index

Parameters:
  • data (object) – The objects to add to the index.
  • ids (array_like optional) – The ids of the object being inserted. If not set will default to the row id of each object in the dataset
Returns:

The number of items added

Return type:

int

createIndex(self: nmslib.dist.IntIndex, index_params: object=None, print_progress: bool=False) → None

Creates the index, and makes it available for querying

Parameters:
  • index_params (dict optional) – Dictionary of optional parameters to use in indexing
  • print_progress (bool optional) – Whether or not to display progress bar when creating index
getDistance(self: nmslib.dist.IntIndex, arg0: int, arg1: int) → int
knnQuery(self: nmslib.dist.IntIndex, vector: object, k: int=10) → object

Finds the approximate K nearest neighbours of a vector in the index

Parameters:
  • vector (array_like) – A 1D vector to query for.
  • k (int optional) – The number of neighbours to return
Returns:

  • ids (array_like.) – A 1D vector of the ids of each nearest neighbour.
  • distances (array_like.) – A 1D vector of the distance to each nearest neigbhour.

knnQueryBatch(self: nmslib.dist.IntIndex, queries: object, k: int=10, num_threads: int=0) → object

Performs multiple queries on the index, distributing the work over a thread pool

Parameters:
  • input (list) – A list of queries to query for
  • k (int optional) – The number of neighbours to return
  • num_threads (int optional) – The number of threads to use
Returns:

A list of tuples of (ids, distances)

Return type:

list

loadIndex(self: nmslib.dist.IntIndex, filename: str, load_data: bool=False) → None

Loads the index from disk

Parameters:
  • filename (str) – The filename to read from
  • load_data (bool optional) – Whether or not to load previously saved data.
saveIndex(self: nmslib.dist.IntIndex, filename: str, save_data: bool=False) → None

Saves the index and/or data to disk

Parameters:
  • filename (str) – The filename to save to
  • save_data (bool optional) – Whether or not to save data
setQueryTimeParams(self: nmslib.dist.IntIndex, params: object=None) → None

Sets parameters used in knnQuery.

Parameters:params (dict) – A dictionary of params to use in querying. Setting params to None will reset