Perform a semantic search in a script

If a table has been vectorization enabled, you can query it using semantic similarity directly in a script.

Prerequisites

Procedure

  • Use the findSimilar method on the table entity, providing a natural-language search string and any standard TypeORM query options:

const data = await entities.testtable.findSimilar({
    search: "My query",
    select: ["id"], // select any fields you need
    where: { id: Not("test") }, // standard TypeORM where clause
    // additional TypeORM arguments
});
  • To also return the similarity score of each row, set withSimilarity to true:

const data = await entities.testtable.findSimilar({
    search: "My query",
    select: ["id"],
    withSimilarity: true,
});

Result

  • The method returns the rows whose vector representation is most similar to the search string, ranked by semantic similarity.

  • If withSimilarity is set, each returned row also contains its cosine similarity score.

Was this page helpful?

What made the biggest difference for you?
What went wrong? (Your feedback helps us fix it.)

Please don't include personal or sensitive data.