File size: 3,222 Bytes
0ec8fd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
verify_copernicus_docs.py — sanity-check the unified copernicus_docs index
WITHOUT hitting the Gemini quota:

  1. point counts per store
  2. BM25 (sparse) keyword queries per domain — local FastEmbed, no API
  3. dense neighbour check — reuse a stored card vector as the query vector

Prints top hits so we can eyeball that the right datasets surface per store.
"""
import json
from pathlib import Path

from qdrant_client import QdrantClient, models
from fastembed import SparseTextEmbedding

OUT = Path(__file__).resolve().parent / "out"
COLLECTION = "copernicus_docs"
client = QdrantClient(path=str(OUT / "qdrant_db"))
_bm25 = SparseTextEmbedding(model_name="Qdrant/bm25")


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


def kw_search(q, k=5, store=None):
    flt = models.Filter(must=[models.FieldCondition(key="store", match=models.MatchValue(value=store))]) if store else None
    res = client.query_points(collection_name=COLLECTION, query=sparse(q),
                              using="sparse", limit=k, with_payload=True, query_filter=flt).points
    return res


def main():
    info = client.get_collection(COLLECTION)
    print(f"=== {COLLECTION}: {info.points_count} points ===\n")

    # per-store counts
    print("per-store counts:")
    for s in ("CMEMS", "CDS", "ADS", "EWDS"):
        cnt = client.count(collection_name=COLLECTION,
                           count_filter=models.Filter(must=[models.FieldCondition(
                               key="store", match=models.MatchValue(value=s))])).count
        print(f"  {s:6s} {cnt}")

    # BM25 keyword probes across domains
    probes = [
        ("sea surface temperature satellite", None),
        ("greenhouse gas carbon dioxide forecast", "ADS"),
        ("river discharge flood forecast europe", "EWDS"),
        ("ERA5 reanalysis climate", "CDS"),
        ("ocean salinity mediterranean", "CMEMS"),
        ("wildfire fire danger", None),
    ]
    print("\n=== BM25 keyword probes ===")
    for q, store in probes:
        print(f"\nQ: {q!r}" + (f"  [store={store}]" if store else ""))
        for p in kw_search(q, k=4, store=store):
            pl = p.payload
            print(f"  {p.score:5.2f} [{pl.get('store'):5s}] {pl.get('dataset_id','')[:45]:45s} {pl.get('product_title','')[:40]}")

    # dense neighbour check — take one CDS card's stored vector, find nearest
    print("\n=== dense neighbour check (stored vector as query) ===")
    sample = client.scroll(collection_name=COLLECTION, limit=1, with_vectors=True,
                           scroll_filter=models.Filter(must=[models.FieldCondition(
                               key="store", match=models.MatchValue(value="CDS"))]))[0][0]
    print(f"seed: [{sample.payload['store']}] {sample.payload.get('dataset_id')}")
    nn = client.query_points(collection_name=COLLECTION, query=sample.vector["dense"],
                             using="dense", limit=5, with_payload=True).points
    for p in nn:
        print(f"  {p.score:5.3f} [{p.payload.get('store'):5s}] {p.payload.get('dataset_id','')[:45]}")


if __name__ == "__main__":
    main()