#!/bin/bash
export LC_ALL=C.UTF-8
echo "===== COLLECTION PAYLOAD SCHEMA ====="
QIP=$(docker inspect qdrant --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' | awk '{print $1}')
curl -s --max-time 6 "http://$QIP:6333/collections/SariKnowledgeV2/points/scroll" \
  -H 'Content-Type: application/json' -d '{"limit":1,"with_payload":true}' 2>/dev/null | head -c 700
echo

echo
echo "===== QUERY -> EMBED -> SEARCH (top 5) ====="
python3 - "$QIP" <<'PY'
import json, sys, urllib.request

qip = sys.argv[1]
queries = ["مدیرعامل فاوا کیه", "فاوا چه سالی تاسیس شد", "مدیران فاوا چه کسایی بودند"]

def post(url, body, timeout=20):
    req = urllib.request.Request(url, data=json.dumps(body).encode(),
                                 headers={"Content-Type": "application/json"})
    with urllib.request.urlopen(req, timeout=timeout) as r:
        return json.load(r)

for q in queries:
    print(f"\n--- «{q}»")
    try:
        emb = post("http://172.18.0.1:11434/api/embeddings",
                   {"model": "bge-m3:latest", "prompt": q})["embedding"]
    except Exception as e:
        print("   embed failed:", e); continue
    try:
        res = post(f"http://{qip}:6333/collections/SariKnowledgeV2/points/search",
                   {"vector": emb, "limit": 5, "with_payload": True})
    except Exception as e:
        print("   search failed:", e); continue
    for i, hit in enumerate(res.get("result", []), 1):
        pl = hit.get("payload") or {}
        text = str(pl.get("pageContent") or pl.get("content") or pl.get("text") or pl)[:150]
        print(f"   {i}. score={hit['score']:.3f}  {text}".replace("\n", " "))
PY
