Skip to main content

GetSimN

Schema

This request accepts an optional schema field. When it is omitted, the server uses the public schema. Set schema to target a store in another schema.

Description

The GetSimN request performs a similarity search against a specific store. It retrieves the top-N closest vectors to a given input vector. This operation is essential for applications that depend on nearest neighbor lookups such as recommendation systems, semantic search, and clustering.

Source Code Example

Click to expand source code
Go
package main

import (
"context"
"fmt"
"log"
"time"


"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"


dbsvc "github.com/deven96/ahnlich/sdk/ahnlich-client-go/grpc/services/db_service"
dbquery "github.com/deven96/ahnlich/sdk/ahnlich-client-go/grpc/db/query"
keyval "github.com/deven96/ahnlich/sdk/ahnlich-client-go/grpc/keyval"
algorithms "github.com/deven96/ahnlich/sdk/ahnlich-client-go/grpc/algorithm/algorithms"
predicates "github.com/deven96/ahnlich/sdk/ahnlich-client-go/grpc/predicates"
)


const ServerAddr = "127.0.0.1:1369"


// ExampleDBClient holds the gRPC connection, client, and context.
func stringPtr(value string) *string { return &value }


type ExampleDBClient struct {
conn *grpc.ClientConn
client dbsvc.DBServiceClient
ctx context.Context
}


// NewDBClient connects to the Ahnlich DB server.
func NewDBClient(ctx context.Context) (*ExampleDBClient, error) {
conn, err := grpc.DialContext(
ctx,
ServerAddr,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
)
if err != nil {
return nil, fmt.Errorf("failed to dial DB server %q: %w", ServerAddr, err)
}
client := dbsvc.NewDBServiceClient(conn)
return &ExampleDBClient{conn: conn, client: client, ctx: ctx}, nil
}


// Close closes the gRPC connection.
func (c *ExampleDBClient) Close() error {
return c.conn.Close()
}


// -------------------- GetSimN --------------------
func (c *ExampleDBClient) exampleGetSimN() error {
resp, err := c.client.GetSimN(c.ctx, &dbquery.GetSimN{
Store: "my_store",
Schema: stringPtr("analytics"), // Optional: defaults to public when omitted
SearchInput: &keyval.StoreKey{Key: []float32{1, 2, 3, 4}},
ClosestN: 3,
Algorithm: algorithms.Algorithm_CosineSimilarity,
Condition: nil, // Optional: filter results using predicates
})
if err != nil {
return err
}
fmt.Println("GetSimN Results:")
for _, entry := range resp.Entries {
fmt.Println(" - Key:", entry.Key.Key, "Value:", entry.Value.Value)
}
return nil
}


// -------------------- Main --------------------
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()


client, err := NewDBClient(ctx)
if err != nil {
log.Fatalf("Failed to create DB client: %v", err)
}
defer client.Close()


if err := client.exampleGetSimN(); err != nil {
log.Fatalf("GetSimN failed: %v", err)
}
}

  • Store – The request targets my_store, which must already exist.

  • SearchInput – A vector [1, 2, 3, 4] is used as the query input. This must match the dimensionality of the store.

  • ClosestN – The request asks for the 3 most similar vectors.

  • Algorithm – Uses CosineSimilarity to compute vector similarity. Other options: EuclideanDistance, DotProductSimilarity.

  • Condition – Optional predicate filter to restrict which vectors are considered in the search. Set to nil to search all vectors. See Predicates documentation for filtering examples.

  • Response – The server returns the top matches as resp.Entries, including both the stored vectors and any metadata associated with them.

This makes GetSimN a fundamental query for retrieving entries most similar to a given embedding while leveraging the similarity algorithms supported by Ahnlich DB.