1
1
# Semantic Book Search with Go, pgvector, and Gemini API
2
2
3
3
[ ![ Release with GoReleaser] ( https://github.com/nmdra/Semantic-Search/actions/workflows/release.yaml/badge.svg )] ( https://github.com/nmdra/Semantic-Search/actions/workflows/release.yaml )
4
+ [ ![ golangci-lint] ( https://github.com/nmdra/Semantic-Search/actions/workflows/golangci-lint.yaml/badge.svg )] ( https://github.com/nmdra/Semantic-Search/actions/workflows/golangci-lint.yaml )
4
5
[ ![ Go Version] ( https://img.shields.io/badge/go-1.24-blue.svg )] ( https://golang.org/dl/ )
5
6
[ ![ License: MIT] ( https://img.shields.io/badge/license-MIT-green.svg )] ( LICENSE )
6
7
[ ![ Docker Image] ( https://img.shields.io/badge/docker-ghcr.io%2Fnmdra%2Fsemantic--search-blue?logo=docker )] ( https://ghcr.io/nmdra/semantic-search )
@@ -28,9 +29,9 @@ C4Context
28
29
UpdateRelStyle(gemini, api, $offsetY="0", $offsetX="80")
29
30
UpdateRelStyle(db, api, $offsetY="40", $offsetX="50")
30
31
UpdateLayoutConfig($c4ShapeInRow="3", $c4BoundaryInRow="1")
31
- ```
32
+ ````
32
33
33
- > [ !CAUTION]
34
+ > \ [!CAUTION]
34
35
> This project is intended for learning and demonstration purposes only.
35
36
> While it tries to follow best and security practices, it may contain errors or incomplete implementations.
36
37
@@ -39,39 +40,28 @@ C4Context
39
40
* **Semantic Search** — Search books by semantic similarity using vector embeddings
40
41
* **Gemini API Integration** — Generates high-quality embeddings via Google's Gemini API
41
42
* **PostgreSQL + pgvector** — Efficient storage and approximate nearest neighbor search
43
+ * **Redis-powered Cache** — Speeds up repeated search queries with vector caching
44
+ * **Run Migrations via CLI** — Run `-migrate` to apply database schema changes at startup
42
45
* **Multi-Platform Support** — Build and release for Linux, macOS, Windows, amd64, and arm64
43
46
* **Docker & GitHub Container Registry** — Easy deployment with multi-arch Docker images
44
- * ** Clean Architecture** — Modular codebase with separate API, service, repository layers
45
47
* **Automated Releases** — GitHub Actions + GoReleaser for continuous delivery
46
48
47
- ## Architecture & Directory Structure
48
-
49
- ```
50
- .
51
- ├── api # HTTP handlers (Echo framework)
52
- ├── cmd # Main application entrypoint
53
- ├── internal # Core business logic, embedder, repository implementations
54
- │ ├── embed # Gemini embedding client
55
- │ └── repository # Database access layer (sqlc generated)
56
- ├── db # SQL migration files & schema
57
- ├── Dockerfile # Multi-stage Docker build for scratch image
58
- ├── .goreleaser.yml # Release automation configuration
59
- ├── go.mod # Go modules dependencies
60
- ├── Makefile # Helper commands (build, migrate, test)
61
- └── README.md # Project documentation (this file)
62
- ```
63
-
64
- For detailed project layout, see [ Go Project Directory Structure] ( https://gist.github.com/ayoubzulfiqar/9f1a34049332711fddd4d4b2bfd46096 ) .
65
-
66
49
## Getting Started
67
50
68
51
### Prerequisites
69
52
70
53
* Go 1.24+
71
54
* PostgreSQL with `pgvector` extension installed
72
55
* Gemini API Key ([Get API Key Here](https://aistudio.google.com/app/apikey))
56
+ * Redis (for vector caching)
73
57
* Docker (optional, for containerized deployment)
74
58
59
+ ### API Endpoints
60
+
61
+ * `POST /books` — Add a book with title and description; stores embedding in DB
62
+ * `GET /search?q=your+query` — Search books semantically by query text
63
+ * `GET /ping` — Health check endpoint
64
+
75
65
### Setup PostgreSQL
76
66
77
67
1. Create your database:
@@ -82,13 +72,17 @@ Run migrations:
82
72
make migrate-up
83
73
```
84
74
85
- ### Environment Variables
75
+ Or via the binary:
86
76
87
- Create a ` .env ` file with the following:
77
+ ``` bash
78
+ semantic-search-api -migrate=true
79
+ ```
80
+ ### Environment Variables
88
81
89
82
```
90
83
DATABASE_URL=postgres://user:password@localhost:5432/semantic_search?sslmode=disable
91
84
GEMINI_API_KEY=your_gemini_api_key_here
85
+ REDIS_URL=localhost:6379
92
86
```
93
87
94
88
### Running Locally
@@ -99,22 +93,27 @@ go run ./cmd/main.go
99
93
100
94
API will be available at ` http://localhost:8080 ` .
101
95
102
- ### API Endpoints
103
-
104
- * ` POST /books ` — Add a book with title and description; stores embedding in DB
105
- * ` GET /search?q=your+query ` — Search books semantically by query text
106
- * ` GET /ping ` — Health check endpoint
107
-
108
96
### Docker
109
97
110
- Build multi-arch images with GoReleaser or manually :
98
+ Run Database migrations :
111
99
112
100
``` bash
113
- docker buildx build --platform linux/amd64,linux/arm64 -t ghcr.io/nmdra/semantic-search:latest .
101
+ docker run --rm \
102
+ --network=host \
103
+ ghcr.io/nmdra/semantic-search:latest \
104
+ -apikey=" $GEMINI_API_KEY " \
105
+ -db-dsn=" $DATABASE_URL " \
106
+ -migrate
114
107
```
115
108
116
109
Run container:
117
110
118
111
``` bash
119
- docker run -p 8080:8080 ghcr.io/nmdra/semantic-search:latest
112
+ docker run --rm \
113
+ --network=host \
114
+ ghcr.io/nmdra/semantic-search:latest \
115
+ -apikey=" $GEMINI_API_KEY " \
116
+ -db-dsn=" $DATABASE_URL " \
117
+ -redis=" localhost:6379" \
118
+ -loglevel=" warn"
120
119
```
0 commit comments