Skip to content

Commit fc9f221

Browse files
authored
Add querying typesense example (#95) (#100)
1 parent 6f7efe1 commit fc9f221

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Diff for: README.md

+28
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,34 @@ def genre_name(self, obj):
164164
return obj.genre_name
165165
```
166166

167+
### Search Collections
168+
169+
Using Typesense for search
170+
171+
```py
172+
from django_typesense.utils import typesense_search
173+
174+
from .models import Song
175+
176+
177+
def search_songs(request):
178+
search_term = request.GET.get("q", None)
179+
songs = Song.objects.all()
180+
181+
if search_term:
182+
data = {
183+
"q": search_term,
184+
"query_by": Song.collection_class.query_by_fields,
185+
# Include other search parameters here
186+
# https://typesense.org/docs/27.1/api/search.html#search-parameters
187+
}
188+
res = typesense_search(Song.collection_class.schema_name, **data)
189+
ids = [result["document"]["id"] for result in res["hits"]]
190+
songs = songs.filter(id__in=ids)
191+
192+
...
193+
```
194+
167195
### Update Collection Schema
168196
To add or remove fields to a collection's schema in place, update your collection then run:
169197
`python manage.py updatecollections`. Consider adding this to your CI/CD pipeline.

0 commit comments

Comments
 (0)