ankane/neighbor
Nearest neighbor search for Rails
Neighbor – Nearest‑neighbor search for Ruby on Rails
What it is
- A Ruby gem that adds vector‑based similarity search to ActiveRecord models. It lets you store embeddings (or any kind of numeric vector) in your database and query for the k nearest records using common distance metrics.
Key features
- Works with several databases out of the box: PostgreSQL (via the
pgvectororcubeextensions), MariaDB, MySQL (HeatWave), SQLite, and also has adapters for Redis and S3 vectors. - Supports many distance functions: Euclidean, cosine, inner‑product, taxicab, Hamming, Jaccard, and more depending on the backend.
- Handles a wide range of vector types: full‑precision (
vector), half‑precision (halfvec), binary (bit/bigint), sparse (sparsevec), and int8 vectors for SQLite. - Provides both exact and approximate indexing (HNSW, IVFFlat) to speed up large‑scale searches.
- Simple model API – add
has_neighbors :embeddingto an ActiveRecord model and callnearest_neighborson a record or directly on the class. - Built‑in helpers for hybrid search (keyword + semantic), sparse search, and online recommendations using the Disco library.
How you use it
- Add the gem to your
Gemfileand runbundle install. - Create a vector column in a migration (
:vector,:cube,:binary, etc.) matching the database you use. - Declare the neighbor relationship in the model with
has_neighbors :embedding(optionally specify dimensions, type, or normalization). - Populate embeddings – either via an external API (OpenAI, Cohere) or a local model (Informers, Transformers.rb).
- Query:
Results include aitem.nearest_neighbors(:embedding, distance: "euclidean").first(5) # or Item.nearest_neighbors(:embedding, [0.9, 1.3, 1.1], distance: "cosine").first(5)neighbor_distanceattribute.
Why it matters
- Enables semantic search, recommendation, and similarity‑based features directly inside a Rails app without needing a separate vector database.
- Leverages the native indexing capabilities of the underlying SQL engine, keeping data and search in the same system.
- Flexible enough for research (sparse vectors, half‑precision) and production (approximate indexes, HeatWave for MySQL).
Typical use‑cases
- Document or product search powered by embeddings.
- Real‑time item‑based recommendations.
- Hybrid keyword‑plus‑semantic retrieval.
- Any Rails‑based service that needs fast similarity look‑ups.
Resources
- Full documentation is in the README, with sections for each supported backend.
- Example code for OpenAI, Cohere, Informers, hybrid search, sparse search, and Disco recommendations.
- Separate adapters:
neighbor-redisandneighbor-s3for non‑SQL stores.
All details above are taken directly from the project's README.
Related
- Project
- Project
- Project
- Project