Files
stack/docs/scratchpads/70-semantic-search-api.md
Jason Woltje 5d348526de feat(#71): implement graph data API
Implemented three new API endpoints for knowledge graph visualization:

1. GET /api/knowledge/graph - Full knowledge graph
   - Returns all entries and links with optional filtering
   - Supports filtering by tags, status, and node count limit
   - Includes orphan detection (entries with no links)

2. GET /api/knowledge/graph/stats - Graph statistics
   - Total entries and links counts
   - Orphan entries detection
   - Average links per entry
   - Top 10 most connected entries
   - Tag distribution across entries

3. GET /api/knowledge/graph/:slug - Entry-centered subgraph
   - Returns graph centered on specific entry
   - Supports depth parameter (1-5) for traversal distance
   - Includes all connected nodes up to specified depth

New Files:
- apps/api/src/knowledge/graph.controller.ts
- apps/api/src/knowledge/graph.controller.spec.ts

Modified Files:
- apps/api/src/knowledge/dto/graph-query.dto.ts (added GraphFilterDto)
- apps/api/src/knowledge/entities/graph.entity.ts (extended with new types)
- apps/api/src/knowledge/services/graph.service.ts (added new methods)
- apps/api/src/knowledge/services/graph.service.spec.ts (added tests)
- apps/api/src/knowledge/knowledge.module.ts (registered controller)
- apps/api/src/knowledge/dto/index.ts (exported new DTOs)
- docs/scratchpads/71-graph-data-api.md (implementation notes)

Test Coverage: 21 tests (all passing)
- 14 service tests including orphan detection, filtering, statistics
- 7 controller tests for all three endpoints

Follows TDD principles with tests written before implementation.
All code quality gates passed (lint, typecheck, tests).

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-02 15:27:00 -06:00

58 lines
2.1 KiB
Markdown

# Issue #70: [KNOW-018] Semantic Search API
## Objective
Implement semantic (vector) search endpoint that uses embeddings generated by issue #69 to enable natural language search over knowledge entries.
## Approach
1. Review existing embedding schema and pgvector setup
2. Review OllamaEmbeddingService from issue #69
3. Create DTOs for semantic search request/response
4. Write tests first (TDD)
5. Implement semantic search in SearchService using pgvector cosine similarity
6. Create controller endpoint POST /api/knowledge/search/semantic
7. Add configurable similarity threshold
8. Test with real queries
9. Run quality checks and code review
## Progress
- [x] Create scratchpad
- [x] Review existing code (embedding schema, OllamaEmbeddingService)
- [x] Add similarity threshold environment variable
- [x] Write tests (TDD - RED)
- [x] Update SearchService to use OllamaEmbeddingService instead of OpenAI (TDD - GREEN)
- [x] Update hybridSearch to use OllamaEmbeddingService
- [x] Update test files to include OllamaEmbeddingService mocks
- [x] All tests passing
- [x] Type check and build successful
- [x] Run code review (quality gates passed)
- [x] Run QA checks (prettier, lint, typecheck all passed)
- [x] Commit changes
- [ ] Close issue
## Testing
- Unit tests for SearchService.semanticSearch()
- Controller tests for POST /api/knowledge/search/semantic
- Integration tests with real embeddings
- Target: 85%+ coverage
## Notes
- Use pgvector cosine similarity operator (<=>)
- Lower distance = higher similarity
- Results should include similarity scores
- Similarity threshold should be configurable via environment variable
- Reuse OllamaEmbeddingService from issue #69
## Findings
- The semantic search endpoint already exists in search.controller.ts (line 111)
- The SearchService already has semanticSearch() method (line 449)
- BUT: It currently uses OpenAI-based EmbeddingService instead of OllamaEmbeddingService
- Need to update SearchService to inject and use OllamaEmbeddingService
- Need to add configurable similarity threshold
- Controller endpoint already properly configured with guards and permissions