AnnoyIndex
public class AnnoyIndex<T> where T : AnnoyOperable
The primary generic class used to create an index.
-
Supported distance metrics / similarity measures
See moreDeclaration
Swift
public enum DistanceMetric : String
-
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
itemLengthThe vector length (Array.count) of each item stored in the index .
metricThe 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]) throwsParameters
indexThe index (integer) to assign to the item.
vectorArray representing the feature vector for the item.
-
Adds multiple new items to the index.
Throws
AnnoyIndexError.addIemFailedDeclaration
Swift
public func addItems(items: inout [[T]]) throwsParameters
itemsAn 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) throwsParameters
numTreesThe 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.UnbuildFailedDeclaration
Swift
public func unbuild() throws -
Saves the current index to a file then immediately loads (memory maps) the index from disk.
Throws
AnnoyIndexError.saveFailedDeclaration
Swift
public func save(url: URL, prefault: Bool = false) throwsParameters
urlThe file destination URL for the index.
prefaultIf 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.loadFailedDeclaration
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
item1The index of first item of inerest.
item2The 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
itemThe index of the item of interest.
neighborsThe number of neighbors to return.
search_kThe 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
vectorThe vector of interest.
neighborsThe number of neighbors to return.
search_kThe 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
itemThe 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
urlA file destination URL for the index.
AnnoyIndex Class Reference