AnnoyIndex

public class AnnoyIndex<T> where T : AnnoyOperable

The primary generic class used to create an index.

  • Supported distance metrics / similarity measures

    See more

    Declaration

    Swift

    public enum DistanceMetric : String

Properties

  • The length of each vector (Array count if you wish) for each item in the index.

    Declaration

    Swift

    public private(set) var itemLength: Int { get }
  • The distance metric used to measure the similarity between vectors.

    Declaration

    Swift

    public private(set) var distanceMetric: [CChar] { get }
  • The type of data being used in each vector.

    Declaration

    Swift

    public private(set) var dataType: [CChar] { get }
  • The number of items in the index.

    Declaration

    Swift

    public var numberOfItems: Int { get }
  • The number of trees in the index. (if built)

    Declaration

    Swift

    public var numberOfTrees: Int { get }
  • Declaration

    Swift

    public init(itemLength: Int, metric: DistanceMetric = .euclidean)

    Parameters

    itemLength

    The vector length (Array.count) of each item stored in the index .

    metric

    The metric to be used to measure the distance between items. One of .angular, .dotProduct, .euclidean, or .manhattan

  • Adds an a new item to the index.

    Declaration

    Swift

    public func addItem(index: Int, vector: inout [T]) throws

    Parameters

    index

    The index (integer) to assign to the item.

    vector

    Array representing the feature vector for the item.

  • Adds multiple new items to the index.

    Throws

    AnnoyIndexError.addIemFailed

    Declaration

    Swift

    public func addItems(items: inout [[T]]) throws

    Parameters

    items

    An Array of vectors to add to the index.

  • Builds the index to allow for fast approximate nearest neighbors lookup.

    Using a larger number of trees to build the index will take longer, but will also lead to better accuracy during querying. You may need to experiment to find the right balance.

    Declaration

    Swift

    public func build(numTrees: Int) throws

    Parameters

    numTrees

    The number of trees to use to build the index.

  • Unbuilds the current AnnoyIndex which allows additional items to be added or for the index to be rebuilt with a different number of trees.

    Throws

    AnnoyIndexError.UnbuildFailed

    Declaration

    Swift

    public func unbuild() throws
  • Saves the current index to a file then immediately loads (memory maps) the index from disk.

    Throws

    AnnoyIndexError.saveFailed

    Declaration

    Swift

    public func save(url: URL, prefault: Bool = false) throws

    Parameters

    url

    The file destination URL for the index.

    prefault

    If set to true the entire file will be preread into memory.

  • Unloads the underlying index and all associated items.

    Declaration

    Swift

    public func unload()
  • Loads a previously saved index.

    Throws

    AnnoyIndexError.loadFailed

    Declaration

    Swift

    public func load(url: URL) throws
  • Calculates the distance between two items in the index.

    Declaration

    Swift

    public func getDistance(item1: Int, item2: Int) -> T?

    Parameters

    item1

    The index of first item of inerest.

    item2

    The index of the second item of interest.

    Return Value

    The distance between the two items or nil if one of the items is not in the index.

  • Gathers the approximate nearest neighbors for an item.

    Declaration

    Swift

    public func getNNsForItem(item: Int, neighbors: Int, search_k: Int = -1) -> (indices: [Int], distances: [T])?

    Parameters

    item

    The index of the item of interest.

    neighbors

    The number of neighbors to return.

    search_k

    The number of nodes to inspect during search. search_k defaults to n * n_trees * D where n is the number of approximate nearest neighbors and D is a constant determined by Annoy based on the distance metric. In general, increasing search_k increases search time, but will give more accurate results.

    Return Value

    A tuple of arrays containing the item indicies and distances.

  • Gathers the approximate nearest neighbors for a vector.

    Declaration

    Swift

    public func getNNsForVector(vector: inout [T], neighbors: Int, search_k: Int = -1) -> (indices: [Int], distances: [T])?

    Parameters

    vector

    The vector of interest.

    neighbors

    The number of neighbors to return.

    search_k

    The number of nodes to inspect during search. search_k defaults to n * n_trees * D where n is the number of approximate nearest neighbors and D is a constant determined by Annoy based on the distance metric. In general, increasing search_k increases search time, but will give more accurate results.

    Return Value

    A tuple of arrays containing the item indicies and distances.

  • When set to true provides additional information about operations carried out by Annoy via stdout.

    Declaration

    Swift

    public func setVerbos(boolVal: Bool)
  • Retrieves the vector for an item in the index.

    Declaration

    Swift

    public func getItem(index: Int) -> [T]?

    Parameters

    item

    The index of the item to retrieve

    Return Value

    The vector associated with the item or nil if not found.

  • Sets the random seed used for generating the index. Setting this value is useful when running performance testing to ensure consistent results.

    Declaration

    Swift

    public func setSeed(seedVal: Int)
  • Prepares the index to be built on disk rather than in RAM. Should be set before adding items to the index.

    Declaration

    Swift

    public func onDiskBuild(url: URL)

    Parameters

    url

    A file destination URL for the index.