Member Junction
    Preparing search index...

    Class SimpleVectorService<TMetadata>

    SimpleVectorService provides in-memory vector similarity search capabilities. This service is storage-agnostic and allows developers to manage their own vector persistence.

    // Create service instance
    const vectorService = new SimpleVectorService();

    // Load vectors from your data source
    const vectors = new Map<string, number[]>();
    vectors.set('item1', [0.1, 0.2, 0.3]);
    vectors.set('item2', [0.4, 0.5, 0.6]);
    vectorService.LoadVectors(vectors);

    // Find similar items
    const queryVector = [0.15, 0.25, 0.35];
    const results = vectorService.FindNearest(queryVector, 5);

    Type Parameters

    • TMetadata = Record<string, unknown>

    Hierarchy (View Summary)

    Index

    Constructors

    Accessors

    Methods

    • Adds a new vector or updates an existing one based on whether the key already exists. If the key does not exist, a new entry is created. If the key already exists, the vector and/or metadata are updated in place.

      Parameters

      • key: string

        The unique identifier for the vector

      • vector: number[]

        The vector/embedding array

      • Optionalmetadata: TMetadata

        Optional metadata to associate with the vector

      Returns boolean

      True if an existing vector was updated, false if a new vector was added

      If key is null/undefined, or if vector is invalid

      // First call creates the entry
      const wasUpdate = service.AddOrUpdateVector('doc1', [0.1, 0.2, 0.3], { title: 'Doc 1' });
      console.log(wasUpdate); // false (new entry)

      // Second call updates the existing entry
      const wasUpdate2 = service.AddOrUpdateVector('doc1', [0.4, 0.5, 0.6], { title: 'Doc 1 v2' });
      console.log(wasUpdate2); // true (updated)
    • Adds or updates a single vector in the service

      Parameters

      • key: string

        The unique identifier for the vector

      • vector: number[]

        The vector/embedding array

      • Optionalmetadata: TMetadata

        Optional metadata to associate with the vector

      Returns void

      If key is null/undefined, or if vector is invalid

      service.AddVector('product123', [0.1, 0.2, 0.3], {
      name: 'Product Name',
      category: 'Electronics'
      });
    • Calculates the average between-cluster distance for a clustering result. Higher values indicate better cluster separation.

      Measures how far apart different clusters are from each other. Also known as cluster separation.

      • High value: Well-separated groups (good)
      • Low value: Overlapping groups (may have too many clusters)

      Parameters

      Returns number

      Average between-cluster distance (0-1, higher is better)

      const result = service.KMeansCluster(3);
      const separation = service.BetweenClusterDistance(result);
      console.log(`Cluster separation: ${separation.toFixed(3)}`);
    • Calculates distance/similarity between two vectors using the specified metric. All metrics are normalized to 0-1 range where 1 indicates highest similarity.

      Parameters

      • a: number[]

        First vector

      • b: number[]

        Second vector

      • Optionalmetric: DistanceMetric = 'cosine'

        The metric to use

      Returns number

      Normalized similarity score (0-1)

      If vectors have different dimensions or invalid metric

      const similarity = service.CalculateDistance(vec1, vec2, 'euclidean');
      
    • Protected

      Calculates cosine similarity between two vectors using the formula: similarity = (A · B) / (||A|| × ||B||)

      Cosine similarity measures the cosine of the angle between two vectors in multi-dimensional space. It tells us how similar two vectors are regardless of their magnitude (length).

      • 1.0: Vectors point in exactly the same direction (identical)
      • 0.0: Vectors are perpendicular (orthogonal/unrelated)
      • -1.0: Vectors point in opposite directions (completely different)
      • 0.7-1.0: High similarity (vectors are closely related)
      • 0.3-0.7: Moderate similarity
      • < 0.3: Low similarity

      Text embeddings encode semantic meaning as vectors. Cosine similarity is ideal because:

      • It focuses on direction (meaning) rather than magnitude (importance)
      • It's normalized between -1 and 1, making scores comparable
      • It works well in high-dimensional spaces (384-1536 dimensions)
      cosine_similarity = dot_product(A, B) / (magnitude(A) * magnitude(B))

      Where:
      - dot_product(A, B) = Σ(a[i] * b[i]) for all i
      - magnitude(A) = √(Σ(a[i]²)) for all i

      Parameters

      • a: number[]

        First vector (e.g., embedding of document A)

      • b: number[]

        Second vector (e.g., embedding of document B)

      Returns number

      Cosine similarity score between -1 and 1

      If vectors have different dimensions (must be same length)

      // Two identical vectors have similarity of 1
      const similarity1 = CosineSimilarity([1, 2, 3], [1, 2, 3]); // ≈ 1.0

      // Perpendicular vectors have similarity of 0
      const similarity2 = CosineSimilarity([1, 0], [0, 1]); // = 0.0

      // Opposite vectors have similarity of -1
      const similarity3 = CosineSimilarity([1, 2], [-1, -2]); // = -1.0
    • Performs DBSCAN (Density-Based Spatial Clustering of Applications with Noise) clustering.

      DBSCAN groups together points that are closely packed together (high density), marking points in low-density regions as outliers. Unlike K-Means, it doesn't require specifying the number of clusters beforehand.

      • Fraud Detection: Identify unusual transaction patterns
      • Anomaly Detection: Find outliers in any dataset
      • Customer Behavior: Find natural groupings without preconceptions
      • Geographic Clustering: Find areas of high activity
      • Quality Control: Identify defective products

      ✅ Unknown number of clusters ✅ Non-spherical clusters ✅ Varying cluster densities ✅ Need to identify outliers ✅ Noise in the data ❌ High-dimensional sparse data ❌ Clusters of vastly different densities

      Parameters

      • epsilon: number

        Maximum distance between two vectors to be considered neighbors

      • minPoints: number

        Minimum number of points to form a dense region

      • Optionalmetric: DistanceMetric = 'euclidean'

        Distance metric to use

      • Optionalfilter: (metadata: TMetadata) => boolean

        Optional filter to pre-filter vectors by metadata before clustering

      Returns ClusterResult<TMetadata>

      Clustering results with outliers identified

      // epsilon=0.3 means similarity must be > 0.7
      const result = service.DBSCANCluster(0.3, 3, 'cosine');
      console.log(`Found ${result.clusters.size} clusters`);
      console.log(`Outliers: ${result.outliers?.length || 0}`);

      // Cluster only active items
      const activeResult = service.DBSCANCluster(
      0.3,
      3,
      'cosine',
      (metadata) => metadata.status === 'Active'
      );
    • Protected

      Calculates dot product similarity between two vectors, normalized to 0-1 range.

      Dot product (inner product) measures both direction AND magnitude alignment. Unlike cosine similarity, it rewards vectors that point the same way AND have similar magnitudes.

      dot_product = Σ(a[i] × b[i])
      normalized = (tanh(dot_product / scale) + 1) / 2
      • 1.0: Perfect alignment with similar magnitude
      • 0.5: Orthogonal or neutral relationship
      • 0.0: Opposite direction or very different magnitudes
      • Recommendation systems: When popularity (magnitude) matters
      • Revenue analysis: Quantity × Price calculations
      • Weighted scoring: Features × Importance weights
      • Portfolio analysis: Holdings × Performance
      • Marketing effectiveness: Reach × Engagement metrics

      ✅ Magnitude is meaningful (popularity, importance, quantity) ✅ Pre-normalized vectors with semantic magnitude ✅ Weighted feature comparisons ✅ Collaborative filtering with implicit feedback ❌ Vectors with different scales ❌ When only direction matters (use cosine)

      Parameters

      • a: number[]

        First vector

      • b: number[]

        Second vector

      Returns number

      Normalized similarity score (0-1, where 1 = high similarity)

      If vectors have different dimensions

    • Finds the optimal number of clusters using the elbow method. Tests different values of k and returns their inertias.

      A technique to find the optimal number of clusters by plotting inertia vs k. The "elbow" point where inertia stops decreasing rapidly suggests the optimal k.

      1. Run this method with a range of k values
      2. Plot inertia (y-axis) vs k (x-axis)
      3. Look for the "elbow" where the curve flattens
      4. Choose k at the elbow point

      Parameters

      • minK: number

        Minimum number of clusters to test

      • maxK: number

        Maximum number of clusters to test

      • Optionalmetric: DistanceMetric = 'euclidean'

        Distance metric to use

      Returns Map<number, number>

      Map of k values to inertias

      const elbowData = service.ElbowMethod(2, 10);
      elbowData.forEach((inertia, k) => {
      console.log(`k=${k}: inertia=${inertia.toFixed(2)}`);
      });
      // Look for the "elbow" in the results
    • Protected

      Calculates Euclidean distance between two vectors, normalized to 0-1 range.

      Euclidean distance is the "straight-line" distance between two points in space. It's what you'd measure with a ruler in the physical world.

      distance = √(Σ(a[i] - b[i])²)
      normalized_similarity = 1 / (1 + distance)
      • 1.0: Identical vectors (distance = 0)
      • 0.5: Moderate distance (distance = 1)
      • →0: Very different vectors (large distance)
      • Product specifications: Compare products by numeric features (size, weight, price)
      • Quality control: Measure deviation from target specifications
      • Geographic analysis: Distance between store locations (lat/long)
      • Customer segmentation: Group customers by purchase behavior metrics
      • Inventory management: Find similar SKUs by dimensions

      ✅ Continuous numeric features where magnitude matters ✅ Physical measurements and specifications ✅ When you need true geometric distance ❌ High-dimensional sparse data (use cosine instead) ❌ Text embeddings (use cosine for better results)

      Parameters

      • a: number[]

        First vector

      • b: number[]

        Second vector

      Returns number

      Normalized similarity score (0-1, where 1 = identical)

      If vectors have different dimensions

    • Finds all vectors with similarity above a threshold

      Parameters

      • queryVector: number[]

        The vector to search for similar items

      • threshold: number

        Minimum similarity threshold (0-1)

      • Optionalmetric: DistanceMetric = 'cosine'

        The distance metric to use

      • Optionalfilter: (metadata: TMetadata) => boolean

        Optional filter to pre-filter vectors by metadata before similarity calculation

      Returns VectorSearchResult<TMetadata>[]

      Array of search results sorted by similarity

      // Find all highly similar items (similarity > 0.8)
      const similar = service.FindAboveThreshold(queryVector, 0.8);

      // Find all items with Jaccard similarity > 0.5
      const matches = service.FindAboveThreshold(features, 0.5, 'jaccard');

      // Find all similar items in a specific category
      const categoryMatches = service.FindAboveThreshold(
      queryVector,
      0.7,
      'cosine',
      (metadata) => metadata.status === 'Active'
      );
    • Finds the centroid (mean) of a set of vectors.

      A centroid is the mean position of all vectors in a group. It represents the "center" of the cluster in vector space.

      • Representative Selection: Find the average customer profile
      • Summarization: Create a summary vector for a group
      • Cluster Analysis: Understand cluster characteristics
      • Trend Analysis: Find the average trend across time periods

      Parameters

      • vectors: number[][]

        Array of vectors to find centroid of

      Returns number[]

      The centroid vector

      const vectors = [[1, 2], [3, 4], [5, 6]];
      const centroid = service.FindCentroid(vectors);
      // Returns [3, 4] (mean of each dimension)
    • Finds the K nearest neighbors to a query vector using the specified similarity metric

      Parameters

      • queryVector: number[]

        The vector to search for similar items

      • OptionaltopK: number = 10

        Number of nearest neighbors to return

      • Optionalthreshold: number

        Optional minimum similarity threshold (0-1)

      • Optionalmetric: DistanceMetric = 'cosine'

        The distance metric to use

      • Optionalfilter: (metadata: TMetadata) => boolean

        Optional filter to pre-filter vectors by metadata before similarity calculation

      Returns VectorSearchResult<TMetadata>[]

      Array of search results sorted by similarity (highest first)

      If queryVector is null/undefined or empty

      // Default cosine similarity
      const nearestItems = service.FindNearest(queryEmbedding, 5);

      // Using Euclidean distance for numeric features
      const similarProducts = service.FindNearest(productFeatures, 10, undefined, 'euclidean');

      // With threshold - only return items with similarity > 0.7
      const highSimilarity = service.FindNearest(queryVector, 10, 0.7, 'cosine');

      // Using Jaccard for categorical data
      const similarUsers = service.FindNearest(userPreferences, 5, undefined, 'jaccard');

      // Filter by metadata before searching (efficient pre-filtering)
      const agentNotes = service.FindNearest(
      queryVector,
      5,
      0.5,
      'cosine',
      (metadata) => metadata.agentId === 'agent-123'
      );
    • Finds vectors similar to an existing vector identified by its key. The source vector itself is excluded from the results.

      Parameters

      • key: string

        The key of the source vector

      • OptionaltopK: number = 10

        Number of similar vectors to return

      • Optionalthreshold: number

        Optional minimum similarity threshold (0-1)

      • Optionalmetric: DistanceMetric = 'cosine'

        The distance metric to use

      • Optionalfilter: (metadata: TMetadata) => boolean

        Optional filter to pre-filter vectors by metadata before similarity calculation

      Returns VectorSearchResult<TMetadata>[]

      Array of similar vectors sorted by similarity

      If the key doesn't exist in the service

      // Find items similar to 'product123'
      const similarProducts = service.FindSimilar('product123', 5);

      // Find highly similar items (similarity > 0.8)
      const verySimilar = service.FindSimilar('product123', 10, 0.8);

      // Find similar items using Euclidean distance
      const similar = service.FindSimilar('product123', 5, undefined, 'euclidean');

      // Find similar items filtered by category
      const similarInCategory = service.FindSimilar(
      'product123',
      5,
      0.7,
      'cosine',
      (metadata) => metadata.category === 'Electronics'
      );
    • Gets all keys currently stored in the service

      Returns string[]

      Array of all vector keys

      const allKeys = service.GetAllKeys();
      console.log(`Total vectors: ${allKeys.length}`);
    • Retrieves the metadata associated with a specific vector

      Parameters

      • key: string

        The key of the vector

      Returns TMetadata

      The metadata, or undefined if not found

      const metadata = service.GetMetadata('product123');
      if (metadata) {
      console.log(`Product name: ${metadata.name}`);
      }
    • Retrieves a specific vector by its key

      Parameters

      • key: string

        The key of the vector to retrieve

      Returns number[]

      The vector array, or undefined if not found

      const vector = service.GetVector('product123');
      if (vector) {
      console.log(`Vector dimensions: ${vector.length}`);
      }
    • Protected

      Calculates Hamming distance between two vectors, normalized to similarity score.

      Hamming distance counts the number of positions where two vectors differ. Originally designed for error detection in telecommunications, it's useful for comparing categorical data or binary strings.

      hamming_distance = count(a[i] ≠ b[i])
      similarity = 1 - (hamming_distance / vector_length)
      • 1.0: Identical vectors (no differences)
      • 0.5: Half of positions differ
      • 0.0: All positions differ
      • Data quality: Detecting errors in data entry
      • A/B testing: Comparing feature flags or configurations
      • Fraud detection: Unusual patterns in categorical attributes
      • Product variants: Comparing product configurations
      • System monitoring: Configuration drift detection

      ✅ Categorical data comparison ✅ Binary feature vectors ✅ Error detection and correction ✅ Fixed-length codes or identifiers ❌ Continuous numeric features ❌ When magnitude of difference matters

      For continuous values, this uses exact equality. Consider binning continuous values first if approximate matching is needed.

      Parameters

      • a: number[]

        First vector

      • b: number[]

        Second vector

      Returns number

      Normalized similarity (0-1, where 1 = identical)

      If vectors have different dimensions

    • Checks if a vector with the given key exists

      Parameters

      • key: string

        The key to check

      Returns boolean

      True if the key exists, false otherwise

      if (service.Has('product123')) {
      console.log('Product vector exists');
      }
    • Protected

      Calculates Jaccard similarity between two vectors treated as sets.

      Jaccard similarity (Jaccard index) measures the similarity between two sets as the size of their intersection divided by the size of their union. For vectors, non-zero elements are treated as "present" in the set.

      jaccard = |AB| / |AB|
      where A and B are sets of non-zero indices
      • 1.0: Identical sets (same non-zero positions)
      • 0.5: Half of combined elements are shared
      • 0.0: No overlap (completely different sets)
      • Customer behavior: Products purchased, features used
      • Document similarity: Presence/absence of keywords
      • Market basket: Product co-occurrence analysis
      • User permissions: Comparing access control sets
      • Tag similarity: Comparing item categorizations

      ✅ Binary or categorical data (presence/absence) ✅ Sparse vectors where zeros mean "not present" ✅ Set membership comparisons ✅ When magnitude doesn't matter, only presence ❌ Continuous numeric features ❌ Dense embeddings

      This implementation treats 0 as absence and any non-zero as presence. This is suitable for sparse representations where 0 explicitly means "not in set".

      Parameters

      • a: number[]

        First vector (treated as set)

      • b: number[]

        Second vector (treated as set)

      Returns number

      Jaccard similarity (0-1, where 1 = identical sets)

      If vectors have different dimensions

    • Performs K-Means clustering on the loaded vectors. Uses K-Means++ initialization for better starting centroids.

      K-Means divides vectors into K clusters by minimizing within-cluster variance. Each vector is assigned to the nearest centroid, and centroids are iteratively updated.

      • Customer Segmentation: Group customers by behavior patterns
      • Product Categorization: Organize products into natural groups
      • Market Segmentation: Identify distinct market segments
      • Anomaly Detection: Identify unusual patterns (far from all centroids)
      • Document Organization: Group similar documents together

      ✅ Known or estimated number of clusters ✅ Spherical, well-separated clusters ✅ Similar cluster sizes ✅ Need interpretable centroids ❌ Non-spherical clusters ❌ Varying cluster densities ❌ Unknown number of clusters

      Parameters

      • k: number

        Number of clusters

      • OptionalmaxIterations: number = 100

        Maximum iterations before stopping

      • Optionalmetric: DistanceMetric = 'euclidean'

        Distance metric to use

      • Optionaltolerance: number = 0.0001

        Convergence tolerance

      Returns ClusterResult<TMetadata>

      Clustering results with assignments and centroids

      const result = service.KMeansCluster(3, 100, 'euclidean');
      result.clusters.forEach((members, clusterId) => {
      console.log(`Cluster ${clusterId}: ${members.length} members`);
      });
    • Loads vectors into memory. Can accept either an array of VectorEntry objects or a Map where keys are identifiers and values are vector arrays.

      Parameters

      Returns void

      If entries is null or undefined

      // Load from array
      service.LoadVectors([
      { key: 'doc1', vector: [0.1, 0.2], metadata: { title: 'Document 1' } },
      { key: 'doc2', vector: [0.3, 0.4], metadata: { title: 'Document 2' } }
      ]);

      // Load from Map
      const vectorMap = new Map();
      vectorMap.set('doc1', [0.1, 0.2]);
      vectorMap.set('doc2', [0.3, 0.4]);
      service.LoadVectors(vectorMap);
    • Protected

      Calculates Manhattan distance between two vectors, normalized to 0-1 range.

      Manhattan distance (also called L1 distance, city block distance, or taxicab distance) measures the distance between two points by summing the absolute differences of their coordinates. Like navigating a city grid where you can only move along streets.

      distance = Σ|a[i] - b[i]|
      normalized_similarity = 1 / (1 + distance)
      • 1.0: Identical vectors (distance = 0)
      • 0.5: Moderate distance (sum of differences = 1)
      • →0: Very different vectors (large total difference)
      • Supply chain: Warehouse grid navigation, pick-path optimization
      • Time series: Comparing trends (robust to outliers)
      • Resource allocation: Movement costs in grid systems
      • Urban planning: Actual travel distance in city blocks
      • Inventory differences: Total units different across SKUs

      ✅ Grid-based movement systems ✅ When outliers should have linear (not squared) impact ✅ Discrete movements or changes ✅ Each dimension represents independent cost/distance ❌ Smooth gradients needed ❌ True geometric distance required

      Parameters

      • a: number[]

        First vector

      • b: number[]

        Second vector

      Returns number

      Normalized similarity score (0-1, where 1 = identical)

      If vectors have different dimensions

    • Removes a vector from the service

      Parameters

      • key: string

        The key of the vector to remove

      Returns boolean

      True if the vector was removed, false if it didn't exist

      if (service.RemoveVector('product123')) {
      console.log('Vector removed successfully');
      }
    • Calculates the Silhouette Score for a clustering result. Measures how similar a point is to its own cluster compared to other clusters.

      A measure of how appropriate the clustering is, combining both cohesion and separation. Ranges from -1 to 1, where:

      • 1: Perfect clustering (tight, well-separated clusters)
      • 0: Overlapping clusters
      • -1: Wrong clustering (points assigned to wrong clusters)
      • 0.7-1.0: Strong clustering structure
      • 0.5-0.7: Reasonable structure
      • 0.25-0.5: Weak structure
      • < 0.25: No meaningful structure

      Parameters

      Returns number

      Silhouette score (-1 to 1, higher is better)

      const result = service.KMeansCluster(3);
      const score = service.SilhouetteScore(result);
      if (score > 0.7) {
      console.log('Excellent clustering!');
      }
    • Calculates the cosine similarity between two vectors identified by their keys

      Parameters

      • key1: string

        Key of the first vector

      • key2: string

        Key of the second vector

      Returns number

      Cosine similarity score between 0 and 1

      If either key doesn't exist in the service

      const similarity = service.Similarity('item1', 'item2');
      console.log(`Similarity: ${similarity}`);
    • Updates an existing vector entry in-place, allowing partial updates to the vector data, metadata, or both without removing and re-adding the entry. Unlike AddVector, this method requires the key to already exist and will throw if it does not.

      This is useful when you need to:

      • Update the embedding for an existing key after re-encoding (e.g., content changed)
      • Patch metadata without touching the vector data
      • Atomically update both vector and metadata in a single call

      Parameters

      • key: string

        The unique identifier of the vector to update. Must already exist.

      • updates: { metadata?: TMetadata; vector?: number[] }

        The fields to update. At least one of vector or metadata must be provided.

        • Optionalmetadata?: TMetadata

          New metadata to replace the existing metadata.

        • Optionalvector?: number[]

          New vector/embedding to replace the existing one. Must match the expected dimensions of the service.

      Returns boolean

      True if the update was applied successfully

      If the key does not exist in the service

      If neither vector nor metadata is provided

      If the new vector has mismatched dimensions

      // Update only the vector (e.g., after re-embedding content)
      service.UpdateVector('doc123', { vector: newEmbedding });

      // Update only metadata (e.g., status changed)
      service.UpdateVector('doc123', { metadata: { title: 'Updated Title', status: 'reviewed' } });

      // Update both vector and metadata atomically
      service.UpdateVector('doc123', {
      vector: newEmbedding,
      metadata: { title: 'Updated Title', version: 2 }
      });
    • Calculates the average within-cluster distance for a clustering result. Lower values indicate tighter, more cohesive clusters.

      Measures how close points are to other points in the same cluster. Also known as cluster cohesion or compactness.

      • Low value: Tight, well-defined groups (good)
      • High value: Loose, scattered groups (may need more clusters)

      Parameters

      Returns number

      Average within-cluster distance (0-1, lower is better)

      const result = service.KMeansCluster(3);
      const cohesion = service.WithinClusterDistance(result);
      console.log(`Cluster cohesion: ${cohesion.toFixed(3)}`);