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
+80-25Lines changed: 80 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,9 +3,9 @@ Algolia Search for Django
3
3
4
4
This package let you easily integrate the Algolia Search API to your favorite ORM. It's based on the [algoliasearch-client-python](https://github.com/algolia/algoliasearch-client-python) package. Both Python 2.x and 3.x are supported.
5
5
6
-
You might be interested in the sample Django application providing a typeahead.js based auto-completion and Google-like instant search: (algoliasearch-django-example)[] (TODO)
6
+
You might be interested in the 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)
There is also two optionals settings that take a string:
41
+
42
+
*`ALGOLIA_INDEX_PREFIX`: prefix all index. You can use it to seperate different application, like `site1_Products` and `site2_Products`.
43
+
*`ALGOLIA_INDEX_SUFFIX`: suffix all index. You can use it to differenciate development and production environement, like `Location_dev` and `Location_prod`.
44
+
36
45
37
46
Quick Start
38
47
-------------
39
48
40
-
Simply call `AlgoliaSearch.register()` for each of the models you want to index. A good place to do this is in your application's AppConfig (generally your `app.py` file):
49
+
Simply call `AlgoliaSearch.register()` for each of the models you want to index. A good place to do this is in your application's AppConfig (generally named `apps.py`).
41
50
42
51
```python
43
52
from django.apps import AppConfig
@@ -51,41 +60,87 @@ class YourAppConfig(AppConfig):
51
60
AlgoliaSearch.register(YourModel)
52
61
```
53
62
54
-
By default, all the fields of your model will be used. You can configure the index by creating a subclass of `AlgoliaIndex`. A good place to do this is in a separeted file `index.py`:
63
+
And then, don't forget the line below in the `__init__.py` file of your Django application.
By default, all the fields of your model will be used. You can configure the index by creating a subclass of `AlgoliaIndex`. A good place to do this is in a separeted file, like `index.py`.
55
70
56
71
```python
57
72
from AlgoliaSearch import AlgoliaIndex
58
73
59
74
classYourModelIndex(AlgoliaIndex):
60
-
fields = ('name', 'date',) # default: all the fields of your model
61
-
geo_field ='location'# tuple or callable that returns tuple (lat, lng)
62
-
settings = {'attributesToIndex': ['name']}# index settings
63
-
index_name ='my_index'# default: model.__name__
75
+
fields = ('name', 'date')
76
+
geo_field ='location'
77
+
settings = {'attributesToIndex': ['name']}
78
+
index_name ='my_index'
64
79
```
65
80
66
-
`fields` and `geo_field` can be a field or a callable.
67
-
Please take a look at [the documentation about index settings](https://www.algolia.com/doc/python#Settings). `settings` is applied when the command `algolia_buildindex`, `algolia_reindex` or `algolia_applysettings` is executed.
81
+
And then replace `AlgoliaSearch.register(YourModel)` with `AlgoliaSearch.register(YourModel, YourModelIndex)`.
68
82
83
+
## Commands
84
+
85
+
*`python manage.py algolia_buildindex`: index all the registered models. This one should be use the first time. Be careful, if the index already exist on Algolia, it will clear it first.
86
+
*`python manage.py algolia_reindex`: reindex all the registered models. This command will first send all the record to a temporary index and then moves it when the build operation is completed. **We highly recommand this command in production environement.**
87
+
*`python manage.py algolia_applysettings`: (re)apply the index settings.
88
+
*`python manage.py algolia_clearindex`: clear the index
69
89
90
+
## Search
70
91
92
+
We recommend the usage of our [JavaScript API Client](https://github.com/algolia/algoliasearch-client-js) to perform queries directly from the end-user browser without going through your server.
71
93
94
+
## Geo-Search
72
95
73
-
## Setup
96
+
Use the `geo_field` attribute to localize your record. `geo_field` can be a tuple or a callable that return a tuple (latitude, longitude).
74
97
75
-
* Install via pip: `pip install algoliasearch-django`
76
-
* In your `setting.py`, add `AlgoliaSearch` to the `INTALLED_APPS`
77
-
* In the same file, adds to variable `ALGOLIA_APPLICATION_ID` and `ALGOLIA_API_KEY`
78
-
* Register your model in your AppConfig
79
-
* Run the command to index the models: `python manage.py algolia_buildindex`
98
+
```python
99
+
classContact(models.Model):
100
+
name = models.CharField()
101
+
lat = models.FloatField()
102
+
lng = models.FloatField()
103
+
104
+
deflocation(self):
105
+
return (self.lat, self.lng)
80
106
81
-
## Settings
82
107
83
-
*`ALGOLIA_INDEX_PREFIX`: a prefix for all the index (useful for diff app)
84
-
*`ALGOLIA_INDEX_SUFFIX`: a suffix for all the index (useful for dev/prod/test)
85
-
*`ALGOLIA_APPLICATION_ID`: your application ID
86
-
*`ALGOLIA_API_KEY`: your API key (need write access)
108
+
classContactIndex(AlgoliaIndex):
109
+
fields ='name'
110
+
geo_field ='location'
87
111
88
-
## Commands
89
112
90
-
*`./manage.py algolia_buildindex`: hard index all the models
91
-
*`./manage.py algolia_reindex`: solf reindex all the models (create another index, send data, wait the indexing task and then rename the index)
113
+
AlgoliaSearch.register(Contact, ContactIndex)
114
+
```
115
+
116
+
# Options
117
+
118
+
## Custom `objectID`
119
+
120
+
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.
121
+
122
+
```python
123
+
classArticleIndex(AlgoliaIndex):
124
+
custom_objectID ='post_id'
125
+
```
126
+
127
+
## Custom index name
128
+
129
+
You can customize the inde name. By default, the index name will be the name of the model class.
130
+
131
+
```python
132
+
classContactIndex(AlgoliaIndex):
133
+
index_name ='Entreprise'
134
+
```
135
+
136
+
## Index settings
137
+
138
+
We provide many ways to configure your index allowing you to tune your overall index relevancy. All the configuration are explained on [our website](https://www.algolia.com/doc/python#Settings).
0 commit comments