-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfaceted_search.py
More file actions
executable file
·58 lines (44 loc) · 1.79 KB
/
Copy pathfaceted_search.py
File metadata and controls
executable file
·58 lines (44 loc) · 1.79 KB
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
#!/usr/bin/env python
"""Faceted search example using ParadeDB's facets() helper.
This example demonstrates how to fetch faceted counts alongside a search
query using the ParadeDBQuerySet.facets() helper (Top K rows + buckets).
"""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from common import MockItem, setup_mock_items
from paradedb.search import MatchAll, ParadeDB
def demo_facets_with_rows(query: str) -> None:
"""Fetch Top K rows with facet buckets using a window aggregation."""
print("\n--- Facets + Rows (Top K) ---")
queryset = MockItem.objects.filter(description=ParadeDB(MatchAll(query))).order_by(
"-rating"
)[:5]
rows, facets = queryset.facets( # type: ignore[attr-defined]
"category", "rating", "metadata.color", include_rows=True
)
print("Top results:")
for item in rows:
color = item.metadata.get("color") if item.metadata else "N/A"
stock = "In Stock" if item.in_stock else "Out of Stock"
print(
f" • {item.description[:50]}... "
f"[{item.category}] (rating: {item.rating}, {stock}, color: {color})"
)
print("\nFacet buckets:")
for key, data in facets.items():
buckets = data.get("buckets", []) if isinstance(data, dict) else []
print(f"{key} ({len(buckets)} buckets)")
for bucket in buckets:
print(f" • {bucket.get('key')}: {bucket.get('doc_count')}")
if __name__ == "__main__":
print("=" * 60)
print("django-paradedb Faceted Search Example")
print("=" * 60)
count = setup_mock_items()
print(f"Loaded {count} mock items")
search_query = "shoes"
print(f"\nQuery: '{search_query}'")
demo_facets_with_rows(search_query)
print("\n" + "=" * 60)
print("Done!")