-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathautocomplete.py
More file actions
executable file
·67 lines (53 loc) · 1.66 KB
/
Copy pathautocomplete.py
File metadata and controls
executable file
·67 lines (53 loc) · 1.66 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
#!/usr/bin/env python
"""Autocomplete (as-you-type) search example."""
import sys
from pathlib import Path
from paradedb.functions import Score
from paradedb.search import ParadeDB, Parse
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from models import AutocompleteItem
def demo_autocomplete() -> None:
"""As-you-type autocomplete."""
print("\n" + "=" * 60)
print("Autocomplete")
print("=" * 60)
queries = [
"run",
"runn",
"running",
"wire",
"wirel",
"wireles",
"wireless",
"blue",
"blueto",
"bluetooth",
]
for query in queries:
print(f"\nUser types: '{query}' →")
# Autocomplete query
results = (
AutocompleteItem.objects.filter(
description=ParadeDB(Parse(f"description_ngram:{query}"))
)
.annotate(score=Score())
.order_by("-score")[:5]
)
if results:
for item in results:
print(f" • {item.description[:50]}... (score: {item.score:.2f})")
else:
print(" (no results)")
if __name__ == "__main__":
print("=" * 60)
print("django-paradedb Autocomplete Example")
print("Fast as-you-type search")
print("=" * 60)
# Import setup here to avoid circular import issues
sys.path.insert(0, str(Path(__file__).resolve().parent))
from setup import setup_autocomplete_table
# Ensure table and index exist before running the demo.
count = setup_autocomplete_table()
print(f"Loaded {count} products from autocomplete_items table\n")
demo_autocomplete()
print("\nDone.")