🎉 Seed RoundWe've raised 2.5M from Swift, YC, & Chapter One. Read more

Learning - 2023-08-03

A Guide To Efficient Similarity Search In Metal

by Pablo Rios

Two index type vehicles racing each other.

Two index type vehicles racing each other.

Choosing the right index is crucial in vector similarity search as it directly impacts both search efficiency and accuracy. The index acts as a guide, enabling quick identification of similar data points while reducing computational costs and improving search speed. Understanding the characteristics of different indexing techniques empowers users to optimize their similarity search applications according to their specific needs.

In this blog post, we'll delve into the two popular index types available in Metal: the Flat index and the Hierarchical Navigable Small World (HNSW) index. These indexing methods have gained significant popularity in the machine learning and AI community for their exceptional performance in similarity search tasks, especially with high-dimensional embeddings and large datasets. Exploring their characteristics, advantages, and disadvantages will help you make an informed decision when selecting the most suitable index type for efficient embedding retrieval.

Flat Index

The Flat index is the simplest and most intuitive method for embedding retrieval. It involves storing all embeddings directly in a single data structure. When a query is made, the index computes the similarity between the query embedding and all embeddings in the index, returning the nearest neighbors based on the similarity metric.

Pros of a Flat Index:

  • Accurate Search Results: The Flat index produces the most accurate search results compared to other indexing methods. Since it directly compares the query vector with all vectors in the index, it aims for perfect search quality, ensuring that the returned nearest neighbors are the best matches.
  • Flexibility with Distance Metrics: Flat indexes can accommodate various distance metrics, providing the flexibility to choose different similarity measures based on the nature of the data and specific application requirements.

Cons of a Flat Index:

  • Computationally Expensive: As the dataset grows larger and higher-dimensional, the number of distance computations increases significantly, leading to slower search times since it has to scan the entire index for every query.
  • Memory Usage: The Flat index requires a moderate amount of memory to store all embeddings, which can be a concern for large-scale applications.

HNSW Index

The HNSW index is an advanced data structure used for approximate nearest neighbor search in large-scale similarity search applications. It constructs a hierarchical graph-like structure that organizes data points into multiple levels, each representing different resolution levels of the dataset. This hierarchical organization allows for efficient navigation through the graph, enabling the algorithm to quickly locate candidate nearest neighbors and significantly reduce the number of distance computations required during search.

While sacrificing some level of accuracy, the HNSW index achieves impressive retrieval speed and scalability, making it well-suited for high-dimensional datasets with millions or billions of embeddings.

Pros of HNSW Index:

  • Efficient Nearest Neighbor Search: HNSW is specifically optimized for fast approximate nearest neighbor searches, significantly reducing the number of distance computations required during search. Its hierarchical graph-like structure efficiently guides search towards candidate neighbors, striking an excellent balance between retrieval speed and accuracy.
  • Scalability: As the dataset grows, HNSW scales efficiently, making it suitable for large-scale applications. It can handle the increasing volume of data without significant degradation in search performance, ensuring its usability in modern embedding retrieval platforms and applications.

Cons of HNSW Index:

  • Approximate Search Results: The HNSW index provides fast approximate nearest neighbor search results but at the cost of sacrificing some level of accuracy. As an approximate method, it may not guarantee exact matches, which might be a limitation for applications that require precise similarity search results.
  • High Memory Usage: One drawback of the HNSW index is its significant memory consumption, especially when having a large number of connections per vertex. As this increases, the index requires more memory. In memory-constrained environments or with large datasets, the HNSW index's memory footprint can become a concern, requiring careful consideration during deployment.

How to Choose Between Flat and HNSW?

When deciding between Flat and HNSW index types, consider various factors:

  1. Dataset Size: For small datasets (up to tens of thousands of vectors) or exact results, choose Flat.
  2. Dimensionality: HNSW is better for high-dimensional embeddings and large datasets (up to billions of vectors).
  3. Accuracy vs. Speed: If precise results are critical, go with Flat; for faster but approximate results, opt for HNSW.
  4. Memory Constraint: If memory is limited and the dataset fits comfortably, choose Flat; for larger datasets with sufficient memory, opt for HNSW for better scalability and performance.
  5. Update Frequency: If the dataset changes frequently, consider the trade-offs of rebuilding the index.

Choose a Flat Index when:

  • Dataset is small and fits in memory.
  • Precise and deterministic search results are vital.
  • Simplicity and ease of implementation are preferred.

Choose a HNSW Index when:

  • Dealing with large-scale datasets and high-dimensional embeddings.
  • Fast approximate results meet application needs.
  • Efficient scalability is crucial.

Remember, these guidelines are not rigid, and trade-offs may exist. Experiment with both indexes to assess performance for your specific use case.


Flat and HNSW indexes are two distinct approaches to store and retrieve embeddings efficiently for similarity search tasks. The Flat index is simple but computationally expensive for large datasets, while the HNSW index provides fast and scalable approximate search results at the cost of some accuracy. Consider the specific requirements of your application, the size and dimensionality of your dataset, and the trade-off between search efficiency and accuracy to select the most suitable index type for your similarity search needs.