File size: 7,878 Bytes
02a96a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d222880
 
 
 
 
 
 
 
02a96a0
d222880
 
02a96a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abcf53b
 
 
 
 
02a96a0
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# Copernicus RAG — full Qdrant server setup

Stand-alone guide: from zero to a running **Qdrant server** carrying all five
collections of `dmpantiu/copernicus-rag-core`, byte-identical to the validated
indexes (dense 768-d + sparse BM25 vectors, relinked payloads, payload indexes).
No embedding model, no Google/Gemini key, no re-computation involved.

## What you get

| collection | points | content | key filterable payload fields |
|---|---|---|---|
| `copernicus_docs` | 1,418 | L1 dataset cards, all 4 stores | `product_id`, `doc_type`, `store` |
| `marine_docs` | 29,249 | CMEMS PUM/QUID/SQO chunks | `product_id`, `doc_type`, `chunk_type`, `section_path` |
| `cds_docs` | 23,341 | CDS/ADS/EWDS PUG/ATBD chunks | `dataset_ids`, `store`, `doc_type`, `doc_url` |
| `eqc_qa` | 1,274 | C3S EQC quality reports | `dataset_id`, `store`, `doc_type`, `aspect` |
| `publications` | 430,066 | 12,411 parsed papers, dataset-linked | `doi`, `paper_id`, `journal`, `year`, `domains`, `orphan`, `linked_products`, `chunk_type` |

Every collection has **named vectors**: `dense` (768-d, cosine,
`gemini-embedding-2-preview`, L2-normalized) and `sparse` (BM25, IDF modifier)
— so keyword search works with no embedding model at all, and hybrid search
works if you can embed queries (see below).

## 1 · Prerequisites

- Docker + docker compose (any recent version).
- Python 3.10+.
- Access to the private HF dataset (ask the owner to add you as a collaborator
  on `dmpantiu/copernicus-rag-core`) and a HF access token
  (huggingface.co → Settings → Access Tokens → read).
- Disk: ~2.5 GB download + ~6 GB Qdrant storage. RAM: 2–4 GB is plenty
  (payloads are stored on disk).

## 2 · Get this folder & install deps

```bash
export HF_TOKEN=hf_...           # your token
pip install -U huggingface_hub
hf download dmpantiu/copernicus-rag-core --repo-type dataset \
   --include "server/*" --local-dir .
cd server
pip install -r requirements.txt
```

## 3 · Start the Qdrant server

```bash
docker compose up -d
curl http://localhost:6333/readyz     # -> all shards are ready
```

Dashboard: <http://localhost:6333/dashboard>. Data persists in
`./qdrant_storage` across restarts. To protect the server, uncomment
`QDRANT__SERVICE__API_KEY` in `docker-compose.yml` first.

## 4 · Load all five collections

```bash
python load_all.py --url http://localhost:6333
```

What it does: downloads the four `indexes/*.tar.gz` from HF (≈2.5 GB, cached in
the temp dir), untars them, opens each prebuilt index locally and **streams the
points into your server** — vectors, payloads and payload indexes are copied
1:1 from the validated build (50/50 test queries green).

- Timing: the four smaller collections (~55k points) land in **minutes**;
  `publications` (430k points) is limited by the embedded-format reader and
  takes **a few hours** on a laptop — run it in `tmux`/`screen` or overnight.
  Practical order: grab the small ones first, then let publications grind:
  ```bash
  python load_all.py --url ... --collections copernicus_docs marine_docs cds_docs eqc_qa
  python load_all.py --url ... --collections publications   # long — tmux it
  ```
- **Resumable / idempotent**: re-running skips collections whose point count
  already matches; `--recreate` forces a clean re-copy. An interrupted
  collection is re-copied from scratch on the next run.
- Remote/managed cluster: `--url https://<cluster>.cloud.qdrant.io --api-key <key>`
- Already downloaded the tarballs? Untar each `indexes/qdrant_<name>.tar.gz`
  into `<dir>/qdrant_<name>/` and pass `--source-dir <dir>`.

The script verifies every collection (`server count == source count`) and
aborts loudly on mismatch. Expected final state:

```
copernicus_docs 1418 · marine_docs 29249 · cds_docs 23341 · eqc_qa 1274 · publications 430066
```

## 5 · Query it

### Keyword / BM25 (no embedding model needed)

```python
from qdrant_client import QdrantClient, models
from fastembed import SparseTextEmbedding

c = QdrantClient(url="http://localhost:6333")
bm25 = SparseTextEmbedding(model_name="Qdrant/bm25")

def sparse(q):
    r = list(bm25.embed([q]))[0]
    return models.SparseVector(indices=r.indices.tolist(), values=r.values.tolist())

hits = c.query_points("publications", query=sparse("marine heatwave detection SST"),
                      using="sparse", limit=5, with_payload=True)
for h in hits.points:
    print(round(h.score, 2), h.payload["title"][:70])
```

### Dense & hybrid (needs query embeddings)

The corpus vectors are `gemini-embedding-2-preview`, `RETRIEVAL_QUERY` task,
768-d, **L2-normalized** — embed queries the same way:

```python
import numpy as np
from google import genai
from google.genai import types

g = genai.Client(api_key=GEMINI_KEY)
def dense(q):
    r = g.models.embed_content(model="gemini-embedding-2-preview", contents=q,
        config=types.EmbedContentConfig(task_type="RETRIEVAL_QUERY", output_dimensionality=768))
    v = np.array(list(r.embeddings[0].values), dtype=np.float32)
    return (v / np.linalg.norm(v)).tolist()

# hybrid: dense + BM25 fused with RRF, server-side
hits = c.query_points(
    "publications",
    prefetch=[
        models.Prefetch(query=dense(q), using="dense", limit=20),
        models.Prefetch(query=sparse(q), using="sparse", limit=20),
    ],
    query=models.FusionQuery(fusion=models.Fusion.RRF),
    limit=5, with_payload=True)
```

No Gemini access? Two options: BM25-only (above, surprisingly strong on this
corpus), or re-embed the chunks with an open model — `REBUILD.md` §A is the
recipe (`chunks/*.jsonl` carry the raw text; swap is ~20 lines).

### Filters (payload indexes are in place)

```python
# everything linked to ERA5, full-text chunks only
flt = models.Filter(must=[
    models.FieldCondition(key="linked_products",
                          match=models.MatchValue(value="reanalysis-era5-single-levels")),
    models.FieldCondition(key="chunk_type", match=models.MatchValue(value="text")),
])
c.query_points("publications", query=sparse("wind energy assessment"),
               using="sparse", query_filter=flt, limit=5)

# QUID (quality) docs for one CMEMS product
flt = models.Filter(must=[
    models.FieldCondition(key="product_id",
                          match=models.MatchValue(value="GLOBAL_MULTIYEAR_PHY_001_030")),
    models.FieldCondition(key="doc_type", match=models.MatchValue(value="QUID")),
])
c.query_points("marine_docs", query=sparse("assimilated observations"),
               using="sparse", query_filter=flt, limit=5)
```

## 6 · Ops notes

- **Backups**: `curl -X POST http://localhost:6333/collections/publications/snapshots`
  (or just stop the container and copy `qdrant_storage/`).
- **Upgrades**: bump the image tag in `docker-compose.yml`; storage is
  forward-compatible across minor versions.
- **Memory**: `on_disk_payload` is enabled; vectors stay in RAM
  (~1.5 GB total). For tighter RAM add on-disk HNSW/quantization —
  see Qdrant docs.
- The MCP server (`scripts/marine_rag/rag_server.py`) is **server-first**: if a
  Qdrant server is reachable at `QDRANT_URL` (default `http://localhost:6333`)
  and carries the collection, it uses it — publications queries drop from
  minutes (embedded) to ~30 ms. It silently falls back to its embedded copies
  when the server is down; set `QDRANT_URL=""` to force embedded mode.

## 7 · Troubleshooting

| symptom | fix |
|---|---|
| `401` on download | token lacks read access, or you're not a collaborator on the dataset |
| `load_all.py` can't reach server | `docker compose ps`, `curl localhost:6333/readyz`; port 6333 busy → change mapping |
| count MISMATCH abort | re-run (it re-copies the failed collection); check server disk space |
| slow upserts | expected on spinning disks; use `--collections` to prioritize what you need first |
| Apple Silicon | works out of the box (multi-arch image) |