You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+54-25Lines changed: 54 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,8 @@
1
+
1
2
# Algolia Search API Client for Django
2
3
3
-
[Algolia Search](https://www.algolia.com) is a hosted full-text, numerical, and faceted search engine capable of delivering realtime results from the first keystroke.
4
+
[Algolia Search](https://www.algolia.com) is a hosted full-text, numerical,
5
+
and faceted search engine capable of delivering realtime results from the first keystroke.
*[Field Preprocessing and Related objects](#field-preprocessing-and-related-objects)
54
48
*[Index settings](#index-settings)
55
49
*[Restrict indexing to a subset of your data](#restrict-indexing-to-a-subset-of-your-data)
56
50
*[Multiple indices per model](#multiple-indices-per-model)
57
51
58
52
1.**[Tests](#tests)**
59
-
60
53
*[Run Tests](#run-tests)
61
54
62
55
63
56
64
57
58
+
65
59
# Setup
66
60
67
61
68
62
63
+
## Introduction
64
+
65
+
This package lets you easily integrate the Algolia Search API to your [Django](https://www.djangoproject.com/) project. It's based on the [algoliasearch-client-python](https://github.com/algolia/algoliasearch-client-python) package.
66
+
67
+
You might be interested in this sample Django application providing a typeahead.js based auto-completion and Google-like instant search: [algoliasearch-django-example](https://github.com/algolia/algoliasearch-django-example)
68
+
69
+
Compatible with **Python 2.7**, **Python 3.3+** and **Django 1.7+**
70
+
69
71
## Install
70
72
71
73
```sh
@@ -86,7 +88,7 @@ ALGOLIA = {
86
88
There are several optional settings:
87
89
88
90
*`INDEX_PREFIX`: prefix all indexes. Use it to separate different applications, like `site1_Products` and `site2_Products`.
89
-
*`INDEX_SUFFIX`: suffix all indexes. Use it to differentiate development and production environment, like `Location_dev` and `Location_prod`.
91
+
*`INDEX_SUFFIX`: suffix all indexes. Use it to differentiate development and production environments, like `Location_dev` and `Location_prod`.
90
92
*`AUTO_INDEXING`: automatically synchronize the models with Algolia (default to **True**).
91
93
*`RAISE_EXCEPTIONS`: raise exceptions on network errors instead of logging them (default to **settings.DEBUG**).
92
94
@@ -127,6 +129,8 @@ class YourModelIndex(AlgoliaIndex):
127
129
And then replace `algoliasearch.register(YourModel)` with `algoliasearch.register(YourModel, YourModelIndex)`.
128
130
129
131
132
+
133
+
130
134
# Commands
131
135
132
136
@@ -139,31 +143,38 @@ And then replace `algoliasearch.register(YourModel)` with `algoliasearch.registe
139
143
*`python manage.py algolia_clearindex`: clear the index
140
144
141
145
146
+
147
+
142
148
# Search
143
149
144
150
145
151
146
152
## Search
147
153
148
-
We recommend the usage of our [JavaScript API Client](https://github.com/algolia/algoliasearch-client-javascript) to perform queries directly from the end-user browser without going through your server.
154
+
We recommend using our [InstantSearch.js library](https://community.algolia.com/instantsearch.js) to build your search
155
+
interface and perform search queries directly from the end-user browser without going through your server.
149
156
150
-
However, if you want to search from your backend you can use the `raw_search(YourModel, 'yourQuery', params)` method. It retrieves the raw JSON answer from the API.
157
+
However, if you want to search from your backend you can use the `raw_search(YourModel, 'yourQuery', params)` method.
158
+
It retrieves the raw JSON answer from the API, and accepts in `param` any
Use the `geo_field` attribute to localize your record. `geo_field` should be a callable that returns a tuple `(latitude, longitude)` or a list of such tuples.
177
+
Use the `geo_field` attribute to localize your record. `geo_field` should be a callable that returns a tuple (latitude, longitude).
@@ -198,13 +211,16 @@ class ArticleIndex(AlgoliaIndex):
198
211
At query time, specify `{ tagFilters: 'tagvalue' }` or `{ tagFilters: ['tagvalue1', 'tagvalue2'] }` as search parameters to restrict the result set to specific tags.
199
212
200
213
214
+
215
+
201
216
# Options
202
217
203
218
204
219
205
220
## Custom `objectID`
206
221
207
-
You can choose which field will be used as the `objectID `. The field should be unique and can be a string or integer. By default, we use the `pk` field of the model.
222
+
You can choose which field will be used as the `objectID `. The field should be unique and can
223
+
be a string or integer. By default, we use the `pk` field of the model.
208
224
209
225
```python
210
226
classArticleIndex(AlgoliaIndex):
@@ -222,10 +238,12 @@ class ContactIndex(algoliaindex):
222
238
223
239
## Field Preprocessing and Related objects
224
240
225
-
If you want to process a field before indexing it (e.g. capitalizing a `Contact`'s `name`), or if you want to index a [related object](https://docs.djangoproject.com/en/1.11/ref/models/relations/)'s attribute,
226
-
you need to define **proxy methods** for these fields.
241
+
If you want to process a field before indexing it (e.g. capitalizing a `Contact`'s `name`),
242
+
or if you want to index a [related object](https://docs.djangoproject.com/en/1.11/ref/models/relations/)'s
243
+
attribute, you need to define **proxy methods** for these fields.
227
244
228
245
### Models
246
+
229
247
```python
230
248
classAccount(models.Model):
231
249
username = models.CharField(max_length=40)
@@ -245,10 +263,10 @@ class Contact(models.Model):
- With this configuration, you can search for a `Contact` using its `Account` names
261
-
- You can use the associated `account_ids` at search-time to fetch more data from your model (you should **only proxy the fields relevant for search** to keep your records' size as small as possible)
280
+
- You can use the associated `account_ids` at search-time to fetch more data from your
281
+
model (you should **only proxy the fields relevant for search** to keep your records' size
282
+
as small as possible)
262
283
263
284
## Index settings
264
285
265
286
We provide many ways to configure your index allowing you to tune your overall index relevancy.
266
-
All the configuration is explained on [our doc](https://www.algolia.com/doc/api-client/python/parameters/).
287
+
All the configuration is explained on [our doc](https://www.algolia.com/doc/api-reference/api-parameters/).
267
288
268
289
```python
269
290
class ArticleIndex(AlgoliaIndex):
@@ -275,7 +296,8 @@ class ArticleIndex(AlgoliaIndex):
275
296
276
297
## Restrict indexing to a subset of your data
277
298
278
-
You can add constraints controlling if a record must be indexed ornot. `should_index` should be a callable that returns a boolean.
299
+
You can add constraints controlling if a record must be indexed ornot. `should_index` should be a
300
+
callable that returns a boolean.
279
301
280
302
```python
281
303
class Contact(models.model):
@@ -294,6 +316,7 @@ class ContactIndex(AlgoliaIndex):
294
316
It is possible to have several indices for a single model.
295
317
296
318
- First, define all your indices that you want for a model:
319
+
297
320
```python
298
321
from django.contrib.algoliasearch import AlgoliaIndex
299
322
@@ -307,6 +330,7 @@ class MyModelIndex2(AlgoliaIndex):
307
330
```
308
331
309
332
- Then, define a meta model which will aggregate those indices:
333
+
310
334
```python
311
335
class MyModelMetaIndex(AlgoliaIndex):
312
336
def__init__(self, model, client, settings):
@@ -347,12 +371,15 @@ class MyModelMetaIndex(AlgoliaIndex):
347
371
```
348
372
349
373
- Finally, register this `AlgoliaIndex`with your `Model`:
374
+
350
375
```python
351
376
import algoliasearch_django as algoliasearch
352
377
algoliasearch.register(MyModel, MyModelMetaIndex)
353
378
```
354
379
355
380
381
+
382
+
356
383
# Tests
357
384
358
385
@@ -364,7 +391,8 @@ To run the tests, first find your Algolia application id and Admin API key (foun
To override settings for some tests, use the [settings method](https://docs.djangoproject.com/en/1.11/topics/testing/tools/#django.test.SimpleTestCase.settings):
369
397
```python
370
398
class OverrideSettingsTestCase(TestCase):
@@ -384,3 +412,4 @@ class OverrideSettingsTestCase(TestCase):
0 commit comments