| 1 |
.. _basic-usage-label: |
| 2 |
|
| 3 |
=========== |
| 4 |
Basic Usage |
| 5 |
=========== |
| 6 |
|
| 7 |
Usage is best demonstrated with some simple examples. |
| 8 |
|
| 9 |
.. warning:: |
| 10 |
|
| 11 |
The code here is for demonstration purposes only! It might work (or not, I haven't |
| 12 |
tested), but as always, don't blindly copy code from the internet. |
| 13 |
|
| 14 |
Examples |
| 15 |
======== |
| 16 |
|
| 17 |
models.py |
| 18 |
--------- |
| 19 |
|
| 20 |
Let's say we have an app which contains a model `Location`. It could look something like this. |
| 21 |
|
| 22 |
.. code-block:: python |
| 23 |
|
| 24 |
# |
| 25 |
# models.py |
| 26 |
# |
| 27 |
|
| 28 |
from django.db import models |
| 29 |
from haystack.utils.geo import Point |
| 30 |
|
| 31 |
|
| 32 |
class Location(models.Model): |
| 33 |
|
| 34 |
latitude = models.FloatField() |
| 35 |
longitude = models.FloatField() |
| 36 |
address = models.CharField(max_length=100) |
| 37 |
city = models.CharField(max_length=30) |
| 38 |
zip_code = models.CharField(max_length=10) |
| 39 |
|
| 40 |
created = models.DateTimeField(auto_now_add=True) |
| 41 |
updated = models.DateTimeField(auto_now=True) |
| 42 |
|
| 43 |
def __str__(self): |
| 44 |
return self.address |
| 45 |
|
| 46 |
@property |
| 47 |
def coordinates(self): |
| 48 |
return Point(self.longitude, self.latitude) |
| 49 |
|
| 50 |
|
| 51 |
.. _search-index-example-label: |
| 52 |
|
| 53 |
search_indexes.py |
| 54 |
----------------- |
| 55 |
|
| 56 |
We would have to make a ``search_indexes.py`` file for haystack to pick it up. |
| 57 |
|
| 58 |
.. code-block:: python |
| 59 |
|
| 60 |
# |
| 61 |
# search_indexes.py |
| 62 |
# |
| 63 |
|
| 64 |
from django.utils import timezone |
| 65 |
from haystack import indexes |
| 66 |
from .models import Location |
| 67 |
|
| 68 |
|
| 69 |
class LocationIndex(indexes.SearchIndex, indexes.Indexable): |
| 70 |
|
| 71 |
text = indexes.CharField(document=True, use_template=True) |
| 72 |
address = indexes.CharField(model_attr="address") |
| 73 |
city = indexes.CharField(model_attr="city") |
| 74 |
zip_code = indexes.CharField(model_attr="zip_code") |
| 75 |
|
| 76 |
autocomplete = indexes.EdgeNgramField() |
| 77 |
coordinates = indexes.LocationField(model_attr="coordinates") |
| 78 |
|
| 79 |
@staticmethod |
| 80 |
def prepare_autocomplete(obj): |
| 81 |
return " ".join(( |
| 82 |
obj.address, obj.city, obj.zip_code |
| 83 |
)) |
| 84 |
|
| 85 |
def get_model(self): |
| 86 |
return Location |
| 87 |
|
| 88 |
def index_queryset(self, using=None): |
| 89 |
return self.get_model().objects.filter( |
| 90 |
created__lte=timezone.now() |
| 91 |
) |
| 92 |
|
| 93 |
|
| 94 |
views.py |
| 95 |
-------- |
| 96 |
|
| 97 |
For a generic Django REST Framework view, you could do something like this. |
| 98 |
|
| 99 |
.. code-block:: python |
| 100 |
|
| 101 |
# |
| 102 |
# views.py |
| 103 |
# |
| 104 |
|
| 105 |
from drf_haystack.serializers import HaystackSerializer |
| 106 |
from drf_haystack.viewsets import HaystackViewSet |
| 107 |
|
| 108 |
from .models import Location |
| 109 |
from .search_indexes import LocationIndex |
| 110 |
|
| 111 |
|
| 112 |
class LocationSerializer(HaystackSerializer): |
| 113 |
|
| 114 |
class Meta: |
| 115 |
# The `index_classes` attribute is a list of which search indexes |
| 116 |
# we want to include in the search. |
| 117 |
index_classes = [LocationIndex] |
| 118 |
|
| 119 |
# The `fields` contains all the fields we want to include. |
| 120 |
# NOTE: Make sure you don't confuse these with model attributes. These |
| 121 |
# fields belong to the search index! |
| 122 |
fields = [ |
| 123 |
"text", "address", "city", "zip_code", "autocomplete" |
| 124 |
] |
| 125 |
|
| 126 |
|
| 127 |
class LocationSearchView(HaystackViewSet): |
| 128 |
|
| 129 |
# `index_models` is an optional list of which models you would like to include |
| 130 |
# in the search result. You might have several models indexed, and this provides |
| 131 |
# a way to filter out those of no interest for this particular view. |
| 132 |
# (Translates to `SearchQuerySet().models(*index_models)` behind the scenes. |
| 133 |
index_models = [Location] |
| 134 |
|
| 135 |
serializer_class = LocationSerializer |
| 136 |
|
| 137 |
|
| 138 |
urls.py |
| 139 |
------- |
| 140 |
|
| 141 |
Finally, hook up the views in your `urls.py` file. |
| 142 |
|
| 143 |
.. note:: |
| 144 |
|
| 145 |
Make sure you specify the `basename` attribute when wiring up the view in the router. |
| 146 |
Since we don't have any single `model` for the view, it is impossible for the router to |
| 147 |
automatically figure out the base name for the view. |
| 148 |
|
| 149 |
.. code-block:: python |
| 150 |
|
| 151 |
# |
| 152 |
# urls.py |
| 153 |
# |
| 154 |
|
| 155 |
from django.conf.urls import patterns, url, include |
| 156 |
from rest_framework import routers |
| 157 |
|
| 158 |
from .views import LocationSearchView |
| 159 |
|
| 160 |
router = routers.DefaultRouter() |
| 161 |
router.register("location/search", LocationSearchView, basename="location-search") |
| 162 |
|
| 163 |
|
| 164 |
urlpatterns = patterns( |
| 165 |
"", |
| 166 |
url(r"/api/v1/", include(router.urls)), |
| 167 |
) |
| 168 |
|
| 169 |
|
| 170 |
Query time! |
| 171 |
----------- |
| 172 |
|
| 173 |
Now that we have a view wired up, we can start using it. |
| 174 |
By default, the `HaystackViewSet` (which, more importantly inherits the `HaystackGenericAPIView` |
| 175 |
class) is set up to use the `HaystackFilter`. This is the most basic filter included and can do |
| 176 |
basic search by querying any of the field included in the `fields` attribute on the |
| 177 |
`Serializer`. |
| 178 |
|
| 179 |
.. code-block:: none |
| 180 |
|
| 181 |
http://example.com/api/v1/location/search/?city=Oslo |
| 182 |
|
| 183 |
Would perform a query looking up all documents where the `city field` equals "Oslo". |
| 184 |
|
| 185 |
|
| 186 |
Field Lookups |
| 187 |
............. |
| 188 |
|
| 189 |
You can also use field lookups in your field queries. See the |
| 190 |
Haystack `field lookups <https://django-haystack.readthedocs.io/en/latest/searchqueryset_api.html?highlight=lookups#id1>`_ |
| 191 |
documentation for info on what lookups are available. A query using a lookup might look like the |
| 192 |
following: |
| 193 |
|
| 194 |
.. code-block:: none |
| 195 |
|
| 196 |
http://example.com/api/v1/location/search/?city__startswith=Os |
| 197 |
|
| 198 |
This would perform a query looking up all documents where the `city field` started with "Os". |
| 199 |
You might get "Oslo", "Osaka", and "Ostrava". |
| 200 |
|
| 201 |
Term Negation |
| 202 |
............. |
| 203 |
|
| 204 |
You can also specify terms to exclude from the search results using the negation keyword. |
| 205 |
The default keyword is ``not``, but is configurable via settings using ``DRF_HAYSTACK_NEGATION_KEYWORD``. |
| 206 |
|
| 207 |
.. code-block:: none |
| 208 |
|
| 209 |
http://example.com/api/v1/location/search/?city__not=Oslo |
| 210 |
http://example.com/api/v1/location/search/?city__not__contains=Los |
| 211 |
http://example.com/api/v1/location/search/?city__contains=Los&city__not__contains=Angeles |
| 212 |
|