GridGain Developers Hub

Vector Search

GridGain can index vectors stored in a field and then search the cache based on the provided vector.

Requirements

  • GridGain must be running on Java 11 or later.

  • GridGain license must provide access to vector search feature.

  • Vector search can only be implemented for REPLICATED caches.

  • Vectors for the field must be acquired by using a separate model, as no model is provided with GridGain.

Installation

To start using vector store, enable the optional gridgain-vector-query module.

Vector Fields

When creating the field for vector, mark the field that will hold the vector with the QueryVectorField annotation. This field must have the float[] type. GridGain will create a vector index based on the provided embedding.

The example below shows a class that uses a text field and a vector field:

public class Article {
    /**
     * Content (indexed).
     */
    @QueryTextField
    private String content;

    @QueryVectorField
    private float[] contentVector;

    /**
     * Required for binary deserialization.
     */
    public Article() {
        // No-op.
    }

    public Article(String contentVector, float[] contentVec) {
        this.contentVector = contentVector;
        this.vec = contentVec;
    }
}

Objects with vector fields can be stored as normal. GridGain will build an additional index for the vector column that can be queried.

Performing a Vector Query

To perform a vector query, you would need a search vector provided by the same model as the one used to create the original vectors for the database objects. In this example, we will assume that you have procured the required vector already. Once the vector is available, you can use the VectorQuery object to create a query and send it to the cluster with the query method:

float[] searchVector = // get from model
cache.query(new VectorQuery(Article.class, "vec", searchVector, 5)).getAll());

This example returns 5 nearest neighbours for the search vector provided.