-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathquickstart.py
More file actions
executable file
·91 lines (73 loc) · 2.58 KB
/
Copy pathquickstart.py
File metadata and controls
executable file
·91 lines (73 loc) · 2.58 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
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
#!/usr/bin/env python
"""Quickstart example for django-paradedb."""
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.functions import Score, Snippet
from paradedb.search import MatchAll, ParadeDB, Phrase
def demo_basic_search() -> None:
"""Basic keyword search."""
print("\n--- Basic Search: 'shoes' ---")
for item in MockItem.objects.filter(description=ParadeDB(MatchAll("shoes")))[:5]:
print(f" • {item.description[:60]}...")
def demo_scored_search() -> None:
"""Search with BM25 scores."""
print("\n--- Scored Search: 'running' ---")
results = (
MockItem.objects.filter(description=ParadeDB(MatchAll("running")))
.annotate(score=Score())
.order_by("-score")[:5]
)
for item in results:
print(f" • {item.description[:50]}... (score: {item.score:.2f})")
def demo_phrase_search() -> None:
"""Phrase search."""
print("\n--- Phrase Search: 'running shoes' ---")
results = (
MockItem.objects.filter(description=ParadeDB(Phrase("running shoes")))
.annotate(score=Score())
.order_by("-score")[:5]
)
for item in results:
print(f" • {item.description[:50]}... (score: {item.score:.2f})")
def demo_snippet_highlighting() -> None:
"""Snippet highlighting."""
print("\n--- Snippet Highlighting: 'shoes' ---")
results = (
MockItem.objects.filter(description=ParadeDB(MatchAll("shoes")))
.annotate(
score=Score(),
snippet=Snippet("description", start_sel="<b>", stop_sel="</b>"),
)
.order_by("-score")[:3]
)
for item in results:
print(f" • {item.snippet}")
def demo_filtered_search() -> None:
"""Search with Django ORM filters."""
print("\n--- Filtered Search: 'shoes' + in_stock + rating >= 4 ---")
results = (
MockItem.objects.filter(
description=ParadeDB(MatchAll("shoes")),
in_stock=True,
rating__gte=4,
)
.annotate(score=Score())
.order_by("-score")[:5]
)
for item in results:
print(f" • {item.description[:40]}... (rating: {item.rating})")
if __name__ == "__main__":
print("=" * 60)
print("django-paradedb Quickstart Example")
print("=" * 60)
count = setup_mock_items()
print(f"Loaded {count} mock items")
demo_basic_search()
demo_scored_search()
demo_phrase_search()
demo_snippet_highlighting()
demo_filtered_search()
print("\n" + "=" * 60)
print("Done!")