UltipaDocs
Products
Solutions
Resources
Company
Start Free Trial
UltipaDocs
Start Free Trial
  • Overview
  • Provider Configuration
  • AI Completion
  • Vectors
  • Vector Similarity Search
  • Vector Utilities
  1. Docs
  2. /
  3. AI Functions

Vector Similarity Search

Compare vectors and perform semantic search to find related content.

Similarity Functions

ai.cosine()

Computes cosine similarity between two vectors. Returns a value between -1 and 1, where 1 means identical direction.

Syntaxai.cosine(<vector1>, <vector2>)
ArgumentsNameTypeDescription
<vector1>VECTORThe first vector
<vector2>VECTORThe second vector; must have the same dimension as <vector1>
Return TypeFLOAT
GQL
LET v1 = ai.vector([1.0, 0.0, 0.0])
LET v2 = ai.vector([1.0, 1.0, 0.0])
RETURN ai.cosine(v1, v2)

Result: 0.7071067690849304

ai.euclidean()

Computes Euclidean distance between two vectors. Lower values indicate more similarity.

Syntaxai.euclidean(<vector1>, <vector2>)
ArgumentsNameTypeDescription
<vector1>VECTORThe first vector
<vector2>VECTORThe second vector; must have the same dimension as <vector1>
Return TypeFLOAT
GQL
LET v1 = ai.vector([1.0, 0.0])
LET v2 = ai.vector([0.0, 1.0])
RETURN ai.euclidean(v1, v2)

Result: 1.4142135381698608

ai.dot()

Computes the dot product of two vectors.

Syntaxai.dot(<vector1>, <vector2>)
ArgumentsNameTypeDescription
<vector1>VECTORThe first vector
<vector2>VECTORThe second vector; must have the same dimension as <vector1>
Return TypeFLOAT
GQL
LET v1 = ai.vector([1.0, 2.0, 3.0])
LET v2 = ai.vector([4.0, 5.0, 6.0])
RETURN ai.dot(v1, v2)

Result: 32

ai.distance()

Computes the cosine distance between two vectors (1 - cosine similarity). Lower values indicate more similarity.

Syntaxai.distance(<vector1>, <vector2>)
ArgumentsNameTypeDescription
<vector1>VECTORThe first vector
<vector2>VECTORThe second vector; must have the same dimension as <vector1>
Return TypeFLOAT
GQL
LET v1 = ai.vector([1.0, 0.0, 0.0])
LET v2 = ai.vector([1.0, 1.0, 0.0])
RETURN ai.distance(v1, v2)

Result: 0.2928932309150696

Vector Index Search

For large datasets, create a vector index for efficient approximate nearest neighbor (ANN) search. See Vector Index for full syntax.

Create a vector index:

GQL
CREATE VECTOR INDEX doc_search ON NODE Document (embedding) OPTIONS {
  dimensions: 1536,
  metric: "cosine"
}

k-NN Search

Find the k nearest neighbors using ORDER BY ... LIMIT. The optimizer automatically uses the vector index:

GQL
LET query = ai.embed('how to model relationships in graphs')
MATCH (d:Document)
RETURN d.title, ai.cosine(d.embedding, query) AS similarity
ORDER BY similarity DESC
LIMIT 10

Range Search

Find all vectors above a similarity threshold:

GQL
LET query = ai.embed('cloud computing tutorial')
MATCH (d:Document)
WHERE ai.cosine(d.embedding, query) > 0.7
RETURN d.title, ai.cosine(d.embedding, query) AS relevance
ORDER BY relevance DESC

Hybrid Search

Combine vector similarity with graph traversal and property filters:

GQL
LET query = ai.embed('machine learning applications')
MATCH (d:Document)
WHERE ai.cosine(d.embedding, query) > 0.8
  AND d.category = 'Technology'
MATCH (d)-[:AUTHORED_BY]->(author:Person)
RETURN d.title, ai.cosine(d.embedding, query) AS similarity, author.name
ORDER BY similarity DESC
LIMIT 5

RAG Context Retrieval

Retrieve context for Retrieval Augmented Generation:

GQL
LET query = ai.embed('How do I create a graph index?')
MATCH (d:Document)
RETURN d.content, ai.cosine(d.embedding, query) AS score
ORDER BY score DESC
LIMIT 3