DDD Meilisearch showcase: domain/app/infra/interfaces, Taskfile, tests

This commit is contained in:
hermes committed 2026-09-09 14:21:52 +00:00
commit a9674ce14e
22 files changed
+1584

No files matched your search

+121
View File
@@ -0,0 +1,121 @@
# meilisearch-showcase
A small, production-shaped Go demo of [Meilisearch](https://www.meilisearch.com/):
full-text search with typo tolerance, prefix search, filters, facets and
highlighting — wrapped in a **Domain-Driven Design** layout and driven by
[Task](https://taskfile.dev).
## Why it is structured this way
The interesting part of a search demo is not the SDK calls, it is keeping the
search engine out of your business code. Meilisearch is an infrastructure
detail behind a port:
```
cmd/ process entrypoints (api, seed)
internal/
domain/catalog/ entities, value objects, invariants, PORTS
movie.go Movie aggregate + MovieID value object
search.go Query/Results value objects + MovieIndex port
app/ use cases (import, search, get, delete)
infra/
meili/ DRIVEN adapter: MovieIndex -> Meilisearch
config/ env configuration
interfaces/http/ DRIVING adapter: HTTP -> use cases
test/ integration tests against a live Meilisearch
```
Dependencies point inwards only. `internal/domain` and `internal/app` import
nothing from Meilisearch — `go list -deps` proves it. Swapping in Elastic or a
plain `map` means writing one new adapter, not touching the domain.
The domain speaks `catalog.Query{Genre, MinYear, MinRating}`; the adapter
translates that into Meilisearch filter syntax (`internal/infra/meili/filter.go`).
Documents indexed in Meilisearch use a separate persistence model
(`meili.document`) that is mapped to/from the aggregate, so the read model can
change without breaking invariants.
## Requirements
- Go 1.27+
- Docker (for `task meili:up`) — or any reachable Meilisearch, set `MEILI_HOST`
- [Task](https://taskfile.dev/installation/)
## Quick start
```bash
task meili:up # Meilisearch v1.24 on :7700
task seed # create index, apply settings, load data/movies.json
task run # API on :8080
# in another shell:
task query # runs scripts/demo-queries.sh
```
`task demo` chains up + seed + queries in one go. `task --list` shows everything.
## API
| Method | Path | Description |
|--------|----------------|----------------------------------------------|
| GET | `/healthz` | liveness |
| GET | `/search` | `q, genre, min_year, max_year, min_rating, limit, offset` |
| GET | `/movies/{id}` | single document |
| POST | `/movies` | JSON array of movies, validated then indexed |
| DELETE | `/movies/{id}` | remove a document |
```bash
curl 'localhost:8080/search?q=matrx' # typo tolerance
curl 'localhost:8080/search?q=spid' # prefix search
curl 'localhost:8080/search?q=dream' # searches overviews
curl 'localhost:8080/search?q=&genre=Sci-Fi&min_rating=8' # filter + facets
curl 'localhost:8080/search?q=&limit=5' # browse, ranked by rating
```
Response:
```json
{
"hits": [
{"id":"1","title":"The Matrix","genres":["Sci-Fi","Action"],"year":1999,
"rating":8.7,"match":"The <em>Matrix</em>"}
],
"estimatedTotal": 1,
"processingMs": 0,
"genres": [{"value":"Sci-Fi","count":6}]
}
```
## What the index settings do
`internal/infra/meili/index.go` `EnsureSchema` is idempotent and sets:
- `searchableAttributes: title, overview, genres` — ranked in that order
- `filterableAttributes: genres, year, rating` — enables filters and facets
- `sortableAttributes: year, rating`
- a custom ranking rule `rating:desc` as the final tie-breaker
## Tests
```bash
task test # unit tests, fake index, no Meilisearch needed
task meili:up
task test:integration # real engine: typo tolerance, filters, facets, 404s
```
Unit tests use an in-memory `MovieIndex` fake — that is the payoff of the port.
## Configuration
| Env | Default |
|--------------------|-------------------------|
| `MEILI_HOST` | `http://localhost:7700` |
| `MEILI_MASTER_KEY` | *(empty)* |
| `MEILI_INDEX` | `movies` |
| `HTTP_ADDR` | `:8080` |
| `SEED_FILE` | `data/movies.json` |
## Scope
Deliberately skipped: auth on the demo API, pagination beyond limit/offset,
tenant tokens, embedders/vector search, and a UI. Each would live in its own
adapter without touching `internal/domain`.