Skip to content

Commit 18f2f8f

Browse files
committed
documentation + setup stuff
1 parent 5f1bd91 commit 18f2f8f

File tree

3 files changed

+82
-27
lines changed

3 files changed

+82
-27
lines changed

README.md

Lines changed: 80 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ Algolia Search for Django
33

44
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.
55

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)
77

8-
TODO: travis + other badges
8+
[![PyPI version](https://badge.fury.io/py/algoliasearch-django.svg)](http://badge.fury.io/py/algoliasearch-django)
99

1010
Table of Content
1111
-------------
@@ -14,6 +14,11 @@ Table of Content
1414
1. [Install](#install)
1515
1. [Setup](#setup)
1616
1. [Quick Start](#quick-start)
17+
1. [Commands](#commands)
18+
1. [Search](#search)
19+
1. [Geo-search](#geo-search)
20+
1. [Options](#options)
21+
1722

1823
Install
1924
-------------
@@ -32,12 +37,16 @@ ALGOLIA_APPLICATION_ID = 'MyAppID'
3237
ALGOLIA_API_KEY = 'MyApiKey'
3338
```
3439

35-
TODO: speak about prefix/suffix usage
40+
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+
3645

3746
Quick Start
3847
-------------
3948

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`).
4150

4251
```python
4352
from django.apps import AppConfig
@@ -51,41 +60,87 @@ class YourAppConfig(AppConfig):
5160
AlgoliaSearch.register(YourModel)
5261
```
5362

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.
64+
65+
```python
66+
default_app_config = 'your_django_app.apps.YourAppConfig'
67+
```
68+
69+
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`.
5570

5671
```python
5772
from AlgoliaSearch import AlgoliaIndex
5873

5974
class YourModelIndex(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'
6479
```
6580

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)`.
6882

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
6989

90+
## Search
7091

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.
7193

94+
## Geo-Search
7295

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).
7497

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+
class Contact(models.Model):
100+
name = models.CharField()
101+
lat = models.FloatField()
102+
lng = models.FloatField()
103+
104+
def location(self):
105+
return (self.lat, self.lng)
80106

81-
## Settings
82107

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+
class ContactIndex(AlgoliaIndex):
109+
fields = 'name'
110+
geo_field = 'location'
87111

88-
## Commands
89112

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+
class ArticleIndex(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+
class ContactIndex(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).
139+
140+
```python
141+
class ArticleIndex(AlgoliaIndex):
142+
settings = {
143+
'attributesToIndex': ['name', 'description', 'url'],
144+
'customRanking': ['desc(vote_count)', 'asc(name)']
145+
}
146+
```

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[bdist_wheel]
2+
universal=1

setup.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,5 @@
4545
'Programming Language :: Python :: 2',
4646
'Programming Language :: Python :: 3',
4747
'Topic :: Internet :: WWW/HTTP',
48-
'Topic :: Interne :: WWW/HTTP :: Dynamic Content',
49-
'Topic :: Software Developement :: Libraries :: Python Modules'
5048
]
5149
)

0 commit comments

Comments
 (0)