kmeans-test-data

command
v1.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 6, 2026 License: MIT Imports: 16 Imported by: 0

README ΒΆ

All k-means operations now have structured logging with the [K-MEANS] prefix:

Log Description
[K-MEANS] βœ… ENABLED Clustering enabled with mode/clusters/init
[K-MEANS] πŸ”„ STARTING Clustering starting with embedding count
[K-MEANS] βœ… COMPLETE Clustering complete with stats + duration
[K-MEANS] ⏭️ SKIPPED Skipped (too few embeddings or not enabled)
[K-MEANS] ❌ FAILED Failed with error
[K-MEANS] πŸ” SEARCH Search executed with mode + timing

2. Test Data Generator Tool

New tool at cmd/kmeans-test-data/main.go:

# Generate 5000 embeddings with 20 natural clusters and save to file
go run cmd/kmeans-test-data/main.go -mode clusters -count 5000 -clusters 20

# Import directly into NornicDB
go run cmd/kmeans-test-data/main.go -mode clusters -count 5000 -db ./data/kmeans-test

# Generate larger dataset for stress testing
go run cmd/kmeans-test-data/main.go -mode download -download large-text -db ./data/stress-test

Modes:

  • synthetic - Random uniformly distributed embeddings
  • clusters - Embeddings with natural cluster structure (best for k-means testing)
  • download - Pre-defined datasets (sift-small, glove-25, text-1024, large-text)

Features:

  • Idempotent (same seed = same data)
  • Ground truth cluster labels for validation
  • Statistics reporting (cluster sizes, norms, memory)
  • Direct import to NornicDB or JSON export

Full Test Flow

# 1. Generate test data with clusters
go run cmd/kmeans-test-data/main.go -mode clusters -count 5000 -clusters 50 -db ./data/kmeans-test

# 2. Enable clustering and start NornicDB
export NORNICDB_KMEANS_CLUSTERING_ENABLED=true
go run cmd/nornicdb/main.go -data ./data/kmeans-test

# 3. Watch logs for k-means activity
# [K-MEANS] βœ… Clustering ENABLED | mode=CPU clusters=100 ...
# [K-MEANS] πŸ”„ STARTING | embeddings=5000
# [K-MEANS] βœ… COMPLETE | clusters=100 embeddings=5000 iterations=12 duration=234ms

K-Means Clustering Integration

How to Enable

export NORNICDB_KMEANS_CLUSTERING_ENABLED=true

What Happens

Stage Action
Startup If flag enabled, clustering initialized (CPU mode)
GPU Available Upgrades to GPU-accelerated clustering
Index Build After indexes built β†’ triggers clustering
Embed Queue Empty After batch embedding completes β†’ auto-triggers clustering
Search Uses cluster-accelerated search when active (10-50x faster)

Smart Behavior

  • Minimum threshold: Only clusters when 1000+ embeddings (below this, brute-force is faster)
  • Fire-and-forget: Clustering runs in background, doesn't block embedding worker
  • Auto-upgrade: Starts with CPU, upgrades to GPU if available later

New Files Modified

  • pkg/search/search.go - Added clustering methods and cluster-accelerated search
  • pkg/nornicdb/embed_queue.go - Added onQueueEmpty callback
  • pkg/nornicdb/db.go - Wired everything together with feature flag

K-Means Clustering Testing Guide

Quick reference for testing k-means clustering with NornicDB.

Prerequisites

cd nornicdb

1. Generate Test Data

Option A: Movie Dataset (Best for Semantic Testing)

# Generate 2000 movies with genre-specific content (will cluster by genre)
go run cmd/kmeans-test-data/main.go -mode movies -count 2000 -db ./data/movies-test

Option B: Pre-clustered Embeddings (Best for K-Means Validation)

# Generate 5000 embeddings with 50 known clusters (ground truth)
go run cmd/kmeans-test-data/main.go -mode clusters -count 5000 -clusters 50 -db ./data/cluster-test

Option C: Large Dataset (Stress Testing)

# Generate 10000 embeddings 
go run cmd/kmeans-test-data/main.go -mode clusters -count 10000 -clusters 100 -db ./data/stress-test

2. Start NornicDB with K-Means Enabled

# Enable k-means clustering
export NORNICDB_GPU_CLUSTERING_ENABLED=true

# For movie data (needs embedder to generate embeddings)
export OLLAMA_BASE_URL=http://localhost:11434
go run cmd/nornicdb/main.go -data ./data/movies-test

# For pre-clustered data (has embeddings already)
go run cmd/nornicdb/main.go -data ./data/cluster-test

3. Watch the Logs

You should see:

πŸ”¬ K-means clustering enabled for accelerated semantic search
βœ… Search indexes built from existing data
[K-MEANS] βœ… Clustering ENABLED | mode=CPU clusters=100 max_iter=50 init=kmeans++
[K-MEANS] πŸ”„ STARTING | embeddings=5000
[K-MEANS] βœ… COMPLETE | clusters=100 embeddings=5000 iterations=12 duration=234ms

For movie data with embedder:

🧠 Embed worker started
πŸ”„ Processing node movie-00001 for embedding...
[K-MEANS] πŸ”¬ Embedding batch complete (2000 processed), triggering k-means clustering...

Via HTTP API

# Semantic search (uses cluster-accelerated path if available)
curl -X POST http://localhost:7474/nornicdb/search \
  -H "Content-Type: application/json" \
  -d '{"query": "space exploration aliens", "limit": 10}'

# Should see in logs:
# [K-MEANS] πŸ” SEARCH | mode=clustered clusters_searched=3 candidates=20 duration=1.2ms

Via Cypher

// Full-text search
CALL db.index.fulltext.queryNodes('default', 'horror scary') YIELD node, score
RETURN node.title, node.genre, score LIMIT 10

// Vector similarity search (if embeddings exist)
CALL db.index.vector.queryNodes('default', 10, 'romantic love story')
YIELD node, score RETURN node.title, score

5. Verify Clustering is Working

Check these log messages:

Log Meaning
mode=clustered βœ… Using k-means accelerated search
mode=brute_force ❌ Falling back to brute force
mode=brute_force_fallback ⚠️ Cluster search failed, using fallback
reason=not_yet_clustered ⏳ Clustering hasn't run yet
reason=too_few_embeddings Need 1000+ embeddings

6. Minimum Requirements

  • 1000+ embeddings required for k-means to trigger
  • Fewer embeddings = brute force is faster anyway

Quick Command Reference

# Generate + import movies
go run cmd/kmeans-test-data/main.go -mode movies -count 2000 -db ./data/test

# Generate + import clustered embeddings  
go run cmd/kmeans-test-data/main.go -mode clusters -count 5000 -db ./data/test

# Just save to JSON (no import)
go run cmd/kmeans-test-data/main.go -mode movies -count 2000 -output ./data/export

# Run NornicDB with k-means
NORNICDB_GPU_CLUSTERING_ENABLED=true go run cmd/nornicdb/main.go -data ./data/test

# Test search
curl -X POST localhost:7474/nornicdb/search -d '{"query":"test","limit":10}'

Cleanup

rm -rf ./data/movies-test ./data/cluster-test ./data/stress-test

Documentation ΒΆ

Overview ΒΆ

K-Means Test Data Generator for NornicDB

This tool generates synthetic embeddings or downloads real datasets for testing the k-means clustering functionality in NornicDB's search service.

Usage:

go run cmd/kmeans-test-data/main.go [options]

Options:

-mode       Generation mode: synthetic, clusters, download, movies (default: clusters)
-count      Number of embeddings to generate (default: 5000)
-dims       Embedding dimensions (default: 1024)
-clusters   Number of natural clusters for 'clusters' mode (default: 20)
-output     Output directory for generated data (default: ./data/kmeans-test)
-db         NornicDB data directory to import into (if set, imports directly)
-dataset    Dataset to download: text-1024, movies-wiki, movies-tmdb

Examples:

# Generate 5000 random embeddings
go run cmd/kmeans-test-data/main.go -mode synthetic -count 5000

# Generate 10000 embeddings with 50 natural clusters (best for k-means testing)
go run cmd/kmeans-test-data/main.go -mode clusters -count 10000 -clusters 50

# Download Wikipedia movie plots dataset
go run cmd/kmeans-test-data/main.go -mode movies -dataset movies-wiki

# Import directly into NornicDB
go run cmd/kmeans-test-data/main.go -mode clusters -count 5000 -db ./data/nornicdb

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL