Class ANNIndex


  • public class ANNIndex
    extends java.lang.Object
    *************************************************************************************\ Approximate Nearest Neighbors * \***************************************************************************************
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected ANNIndex​(long addr)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static ANNIndex __fromPtr__​(long addr)  
      void addItems​(Mat features)
      Add feature vectors to index.
      void build()
      Build the index.
      void build​(int trees)
      Build the index.
      static ANNIndex create​(int dim)
      Creates an instance of annoy index class with given parameters
      static ANNIndex create​(int dim, int distType)
      Creates an instance of annoy index class with given parameters
      protected void finalize()  
      int getItemNumber()
      Return the number of feature vectors in the index.
      long getNativeObjAddr()  
      int getTreeNumber()
      Return the number of trees in the index.
      void knnSearch​(Mat query, Mat indices, Mat dists, int knn)
      Performs a K-nearest neighbor search for given query vector(s) using the index.
      void knnSearch​(Mat query, Mat indices, Mat dists, int knn, int search_k)
      Performs a K-nearest neighbor search for given query vector(s) using the index.
      void load​(java.lang.String filename)
      Loads (mmaps) an index from disk.
      void load​(java.lang.String filename, boolean prefault)
      Loads (mmaps) an index from disk.
      void save​(java.lang.String filename)
      Save the index to disk and loads it.
      void save​(java.lang.String filename, boolean prefault)
      Save the index to disk and loads it.
      boolean setOnDiskBuild​(java.lang.String filename)
      Prepare to build the index in the specified file instead of RAM (execute before adding items, no need to save after build)
      void setSeed​(int seed)
      Initialize the random number generator with the given seed.
      • Methods inherited from class java.lang.Object

        clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ANNIndex

        protected ANNIndex​(long addr)
    • Method Detail

      • getNativeObjAddr

        public long getNativeObjAddr()
      • __fromPtr__

        public static ANNIndex __fromPtr__​(long addr)
      • addItems

        public void addItems​(Mat features)
        Add feature vectors to index.
        Parameters:
        features - Matrix containing the feature vectors to index. The size of the matrix is num_features x feature_dimension.
      • build

        public void build​(int trees)
        Build the index.
        Parameters:
        trees - Number of trees in the index. If not provided, the number is determined automatically in a way that at most 2x as much memory as the features vectors take is used.
      • build

        public void build()
        Build the index. in a way that at most 2x as much memory as the features vectors take is used.
      • knnSearch

        public void knnSearch​(Mat query,
                              Mat indices,
                              Mat dists,
                              int knn,
                              int search_k)
        Performs a K-nearest neighbor search for given query vector(s) using the index.
        Parameters:
        query - The query vector(s).
        indices - Matrix that will contain the indices of the K-nearest neighbors found, optional.
        dists - Matrix that will contain the distances to the K-nearest neighbors found, optional.
        knn - Number of nearest neighbors to search for.
        search_k - The maximum number of nodes to inspect, which defaults to trees x knn if not provided.
      • knnSearch

        public void knnSearch​(Mat query,
                              Mat indices,
                              Mat dists,
                              int knn)
        Performs a K-nearest neighbor search for given query vector(s) using the index.
        Parameters:
        query - The query vector(s).
        indices - Matrix that will contain the indices of the K-nearest neighbors found, optional.
        dists - Matrix that will contain the distances to the K-nearest neighbors found, optional.
        knn - Number of nearest neighbors to search for.
      • save

        public void save​(java.lang.String filename,
                         boolean prefault)
        Save the index to disk and loads it. After saving, no more vectors can be added.
        Parameters:
        filename - Filename of the index to be saved.
        prefault - If prefault is set to true, it will pre-read the entire file into memory (using mmap with MAP_POPULATE). Default is false.
      • save

        public void save​(java.lang.String filename)
        Save the index to disk and loads it. After saving, no more vectors can be added.
        Parameters:
        filename - Filename of the index to be saved. with MAP_POPULATE). Default is false.
      • load

        public void load​(java.lang.String filename,
                         boolean prefault)
        Loads (mmaps) an index from disk.
        Parameters:
        filename - Filename of the index to be loaded.
        prefault - If prefault is set to true, it will pre-read the entire file into memory (using mmap with MAP_POPULATE). Default is false.
      • load

        public void load​(java.lang.String filename)
        Loads (mmaps) an index from disk.
        Parameters:
        filename - Filename of the index to be loaded. with MAP_POPULATE). Default is false.
      • getTreeNumber

        public int getTreeNumber()
        Return the number of trees in the index.
        Returns:
        automatically generated
      • getItemNumber

        public int getItemNumber()
        Return the number of feature vectors in the index.
        Returns:
        automatically generated
      • setOnDiskBuild

        public boolean setOnDiskBuild​(java.lang.String filename)
        Prepare to build the index in the specified file instead of RAM (execute before adding items, no need to save after build)
        Parameters:
        filename - Filename of the index to be built.
        Returns:
        automatically generated
      • setSeed

        public void setSeed​(int seed)
        Initialize the random number generator with the given seed. Only necessary to pass this before adding the items. Will have no effect after calling build() or load().
        Parameters:
        seed - The given seed of the random number generator. Its value should be within the range of uint32_t.
      • create

        public static ANNIndex create​(int dim,
                                      int distType)
        Creates an instance of annoy index class with given parameters
        Parameters:
        dim - The dimension of the feature vector.
        distType - Metric to calculate the distance between two feature vectors, can be DIST_EUCLIDEAN, DIST_MANHATTAN, DIST_ANGULAR, DIST_HAMMING, or DIST_DOTPRODUCT.
        Returns:
        automatically generated
      • create

        public static ANNIndex create​(int dim)
        Creates an instance of annoy index class with given parameters
        Parameters:
        dim - The dimension of the feature vector. DIST_MANHATTAN, DIST_ANGULAR, DIST_HAMMING, or DIST_DOTPRODUCT.
        Returns:
        automatically generated
      • finalize

        protected void finalize()
                         throws java.lang.Throwable
        Overrides:
        finalize in class java.lang.Object
        Throws:
        java.lang.Throwable