-
-
Notifications
You must be signed in to change notification settings - Fork 0
Sdk Usage Java Client
Type-safe, thread-safe Java access to Spector — as a remote client or embedded engine. Whether you're connecting to a server or embedding search directly in your application, this guide covers everything you need.
Remote client (connects to a running server):
<dependency>
<groupId>com.spectrayan</groupId>
<artifactId>spector-client</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>Embedded engine (in-process, zero network overhead):
<dependency>
<groupId>com.spectrayan</groupId>
<artifactId>spector-engine</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>Tip
Choose embedded for maximum performance (zero latency overhead). Choose client when you want a shared server across multiple services.
import com.spectrayan.spector.client.SpectorClient;
SpectorClient client = SpectorClient.builder()
.host("localhost")
.port(7070)
.apiKey("my-secret-key") // optional
.connectTimeout(Duration.ofSeconds(10))
.requestTimeout(Duration.ofSeconds(30))
.maxConnections(10)
.build();Configuration Options:
| Option | Default | Description |
|---|---|---|
host |
localhost | Server hostname |
port |
7070 | Server port |
apiKey |
— | API key for authentication |
connectTimeout |
10s | Connection timeout |
requestTimeout |
30s | Per-request timeout |
maxConnections |
10 | HTTP connection pool size |
Note
SpectorClient is fully thread-safe. It uses Java's HttpClient with internal connection pooling. Share a single instance across all threads.
// Single document
IngestResponse response = client.ingest(IngestRequest.builder()
.id("doc-1")
.title("Java Vector API")
.content("SIMD-accelerated search engine built on modern JVM")
.vector(new float[]{0.1f, 0.2f, 0.3f, 0.4f, 0.5f})
.build());
System.out.println("Indexed: " + response.id());// Bulk ingest
List<IngestRequest> documents = List.of(
IngestRequest.builder().id("d1").content("first doc").vector(vec1).build(),
IngestRequest.builder().id("d2").content("second doc").vector(vec2).build(),
IngestRequest.builder().id("d3").content("third doc").vector(vec3).build()
);
IngestResponse bulkResponse = client.bulkIngest(documents);// Keyword search
SearchResponse results = client.search(SearchRequest.builder()
.text("vector search engine")
.topK(10)
.build());
// Vector search
SearchResponse results = client.search(SearchRequest.builder()
.vector(queryEmbedding)
.topK(10)
.build());
// Hybrid search (both text and vector)
SearchResponse results = client.search(SearchRequest.builder()
.text("search engine")
.vector(queryEmbedding)
.topK(10)
.build());
// Process results
for (SearchResponse.Result result : results.results()) {
System.out.printf("%s (%.4f): %s%n",
result.id(), result.score(), result.content());
}client.delete("doc-1");StatusResponse status = client.status();
System.out.println("Documents: " + status.documentCount());
System.out.println("SIMD: " + status.simd());
MetricsResponse metrics = client.metrics();
System.out.println("QPS: " + metrics.queriesPerSecond());try {
client.search(request);
} catch (SpectorApiException e) {
// HTTP 4xx/5xx from server
System.err.println("HTTP " + e.statusCode() + ": " + e.message());
} catch (SpectorConnectionException e) {
// Server unreachable
System.err.println("Cannot connect to " + e.endpoint());
} catch (SpectorTimeoutException e) {
// Request timed out
System.err.println("Timeout after " + e.timeout());
}The client implements AutoCloseable:
try (SpectorClient client = SpectorClient.builder().build()) {
// Use client...
} // Connections released automaticallyFor applications that want in-process search without network overhead:
import com.spectrayan.spector.engine.SpectorEngine;
import com.spectrayan.spector.engine.SpectorConfig;
var config = SpectorConfig.DEFAULT
.withDimensions(384)
.withCapacity(100_000)
.withSimilarityFunction(SimilarityFunction.COSINE)
.withGpu(true) // optional GPU
.withReranker("http://localhost:11434", "llama3.2", 20); // optional LLM
try (var engine = new SpectorEngine(config)) {
// Engine is ready — sub-millisecond search, zero network overhead
}// With pre-computed vector
engine.ingest("doc-1", "Document content here", embedding);
// The engine handles BM25 indexing, HNSW insertion, and storage automatically// Hybrid search (keyword + vector)
SearchResponse response = engine.hybridSearch("search query", queryVector, 10);
// Keyword-only
SearchResponse response = engine.keywordSearch("exact phrase", 10);
// Vector-only
SearchResponse response = engine.vectorSearch(queryVector, 10);
// Process results
for (ScoredResult result : response.results()) {
System.out.printf("%s → %.4f%n", result.id(), result.score());
}engine.delete("doc-1");import com.spectrayan.spector.client.SpectorClient;
import com.spectrayan.spector.client.model.*;
public class SpectorExample {
public static void main(String[] args) throws Exception {
try (SpectorClient client = SpectorClient.builder()
.host("localhost")
.port(7070)
.build()) {
// Ingest documents
client.ingest(IngestRequest.builder()
.id("java-1")
.title("Virtual Threads")
.content("Java virtual threads enable millions of concurrent tasks")
.vector(new float[]{0.9f, 0.1f, 0.3f, 0.7f, 0.5f})
.build());
client.ingest(IngestRequest.builder()
.id("java-2")
.title("Vector API")
.content("The Vector API provides SIMD acceleration for math operations")
.vector(new float[]{0.2f, 0.8f, 0.4f, 0.1f, 0.6f})
.build());
// Search
SearchResponse results = client.search(SearchRequest.builder()
.text("SIMD acceleration")
.topK(5)
.build());
System.out.println("Results:");
for (var r : results.results()) {
System.out.printf(" %s (%.4f): %s%n", r.id(), r.score(), r.title());
}
// Cleanup
client.delete("java-1");
client.delete("java-2");
}
}
}-
REST API Reference — Underlying API endpoints
-
Spring AI Integration — Spring AI VectorStore adapter
-
Configuration Guide — All engine parameters
-
Getting Started — Quick start guide
- Home
- About
- Getting Started
-
Architecture
- System Overview
- Core Concepts
- MCP Integration
- Ingestion Pipeline
- RAG Pipeline
- Distributed Mode
- GPU Acceleration
- Test Framework & LLM Judge
-
Modules
- Overview
- spector-core
- spector-commons
- spector-config
- spector-storage
- spector-embed-api
- spector-embed-ollama
- spector-index
- spector-query
- spector-gpu
- spector-rag
- spector-engine
- spector-ingestion
- spector-memory
- spector-runtime
- spector-node
- spector-mcp
- spector-cli
- spector-client
- spector-spring
- spector-test-support
- spector-metrics
- spector-events
- spector-bench
- spector-dist
- spector-cortex
- Deep Dives
-
🧠 Cognitive Memory
- Overview
- Getting Started
- Architecture
-
Biological Systems
- Overview
- Cortex — Tier Stores
- Hippocampus — Sleep Consolidation
- Synapse — Tags & Scoring
- Dopamine — Surprise Detection
- Amygdala — Emotional Valence
- 3-Layer Cognitive Graph
- Habituation — Anti-Filter Bubble
- Inhibition — Suppression
- Interference — Deduplication
- Prospective — Future Intents
- Metamemory — Self-Reflection
- Sync — Persistence & Replication
- Advanced Profiles
- Deep Dives
- API Reference
- 🧬 Cortex Dashboard
- Reference
- Operations
- FAQ
- Roadmap
- 🔬 Labs