View Source AnnoyEx (AnnoyEx v1.0.0)

The Spotity Annoy library as a NIF.

Link to this section Summary

Functions

Adds item i (any nonnegative integer) with list v

builds a forest of n_trees trees. More trees gives higher precision when querying.

returns the distance between items i and j.

Returns the vector for item i that was previously added.

Returns the number of items in the index.

Returns the number of trees in the index.

Returns the n closest items to the item at position i. During the query it will inspect up to search_k nodes, which defaults to n_trees * n if not provided. search_k gives you a run-time tradeoff between better accuracy and speed. If you set include_distances to true.

Loads (mmaps) an index from disk. Full path must be given.

Initializes a new index that's read-write and stores vector of f dimensions.

prepares annoy to build the index in the specified file instead of RAM (execute before adding items, no need to save after build)

Saves the index to disk. Full path must be given.

will initialize the random number generator with the given seed.

Unbuilds.

Unloads.

Set verbosity.

Link to this section Types

@type ok_or_err_tuple() :: :ok | {:err, String.t()}

Link to this section Functions

@spec add_item(idx :: reference(), i :: pos_integer(), v :: list()) ::
  ok_or_err_tuple()

Adds item i (any nonnegative integer) with list v

Link to this function

build(idx, n_trees, n_jobs \\ -1)

View Source
@spec build(idx :: reference(), n_trees :: pos_integer(), n_jobs :: integer()) ::
  ok_or_err_tuple()

builds a forest of n_trees trees. More trees gives higher precision when querying.

After calling build, no more items can be added.

n_jobs specifies the number of threads used to build the trees. n_jobs=-1 uses all available CPU cores.

@spec get_distance(idx :: reference(), i :: pos_integer(), j :: pos_integer()) ::
  float()

returns the distance between items i and j.

@spec get_item_vector(idx :: reference(), i :: pos_integer()) :: list()

Returns the vector for item i that was previously added.

@spec get_n_items(idx :: reference()) :: integer()

Returns the number of items in the index.

@spec get_n_trees(idx :: reference()) :: integer()

Returns the number of trees in the index.

Link to this function

get_nns_by_item(idx, i, n, search_k \\ -1, include_distances \\ true)

View Source
@spec get_nns_by_item(
  idx :: reference(),
  i :: pos_integer(),
  n :: pos_integer(),
  search_k :: integer(),
  include_distances :: boolean()
) :: {list(), list()}

Returns the n closest items to the item at position i. During the query it will inspect up to search_k nodes, which defaults to n_trees * n if not provided. search_k gives you a run-time tradeoff between better accuracy and speed. If you set include_distances to true.

Returns a 2 element tuple with two lists in it: results and distances.

Link to this function

get_nns_by_vector(idx, v, n, search_k \\ -1, include_distances \\ true)

View Source
@spec get_nns_by_vector(
  idx :: reference(),
  v :: list(),
  n :: pos_integer(),
  search_k :: integer(),
  include_distances :: boolean()
) :: {list(), list()}

Same as get_nns_by_item but query by list v

Returns a 2 element tuple with two lists in it: results and distances.

Link to this function

load(idx, filename, preload \\ false)

View Source
@spec load(idx :: reference(), filename :: binary(), preload :: boolean()) ::
  ok_or_err_tuple()

Loads (mmaps) an index from disk. Full path must be given.

Link to this function

new(f, metric \\ :angular)

View Source
@spec new(f :: pos_integer(), metric :: atom()) :: {:ok, reference()}

Initializes a new index that's read-write and stores vector of f dimensions.

Available metrics:

  • :angular - The default.
  • :euclidean
  • :manhattan
  • :dot
Link to this function

on_disk_build(idx, filename)

View Source
@spec on_disk_build(idx :: reference(), filename :: binary()) :: ok_or_err_tuple()

prepares annoy to build the index in the specified file instead of RAM (execute before adding items, no need to save after build)

Link to this function

save(idx, filename, prefault \\ false)

View Source
@spec save(idx :: reference(), filename :: binary(), prefault :: boolean()) ::
  ok_or_err_tuple()

Saves the index to disk. Full path must be given.

@spec set_seed(idx :: reference(), seed :: integer()) :: :ok

will initialize the random number generator with the given seed.

Only used for building up the tree, i.e. only necessary to pass this before adding the items. Will have no effect after calling build or load

@spec unbuild(idx :: reference()) :: ok_or_err_tuple()

Unbuilds.

@spec unload(idx :: reference()) :: :ok

Unloads.

@spec verbose(idx :: reference(), verbose :: boolean()) :: :ok

Set verbosity.