Skip to content

Commit cb67bbb

Browse files
committed
update doc
1 parent e3758d4 commit cb67bbb

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Table of Content
2121
1. [Commands](#commands)
2222
1. [Search](#search)
2323
1. [Geo-search](#geo-search)
24+
1. [Tags](#tags)
2425
1. [Options](#options)
2526

2627

@@ -96,13 +97,22 @@ And then replace `algoliasearch.register(YourModel)` with `algoliasearch.registe
9697

9798
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.
9899

100+
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.
101+
102+
```python
103+
from django.contrib.algoliasearch import raw_search
104+
105+
params = { "hitsPerPage": 5 }
106+
raw_search(Contact, "jim", params)
107+
```
108+
99109
## Geo-Search
100110

101111
Use the `geo_field` attribute to localize your record. `geo_field` should be a callable that returns a tuple (latitude, longitude).
102112

103113
```python
104114
class Contact(models.model):
105-
name = models.CharField()
115+
name = models.CharField(max_lenght=20)
106116
lat = models.FloatField()
107117
lng = models.FloatField()
108118

@@ -160,3 +170,19 @@ class ArticleIndex(AlgoliaIndex):
160170
'customRanking': ['desc(vote_count)', 'asc(name)']
161171
}
162172
```
173+
174+
## Restrict indexing to a subset of your data
175+
176+
You can add constraints controlling if a record must be indexed or not. `should_index` should be a callable that returns a boolean.
177+
178+
```python
179+
class Contact(models.model):
180+
name = models.CharField(max_lenght=20)
181+
age = models.IntegerField()
182+
183+
def is_adult(self):
184+
return (self.age >= 18)
185+
186+
class ContactIndex(AlgoliaIndex):
187+
should_index = 'is_adult'
188+
```

src/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
from __future__ import unicode_literals
77

88
from django.contrib.algoliasearch.models import AlgoliaIndex
9-
from django.contrib.algoliasearch.registration import AlgoliaEngine, algolia_engine
9+
from django.contrib.algoliasearch.registration import AlgoliaEngine
10+
from django.contrib.algoliasearch.registration import algolia_engine
1011

1112
algolia_engine = algolia_engine
1213
register = algolia_engine.register
1314
unregister = algolia_engine.unregister
1415
get_registered_model = algolia_engine.get_registered_models
1516
get_adapter = algolia_engine.get_adapter
16-
search = algolia_engine.search
17+
raw_search = algolia_engine.raw_search

src/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class AlgoliaIndex(object):
3838
settings = {}
3939

4040
# Use to specify a callable that say if the instance should be indexed.
41-
# The attribute should be a callable that returns a booleen.
41+
# The attribute should be a callable that returns a boolean.
4242
should_index = None
4343

4444
# Instance of the index from algoliasearch client
@@ -187,7 +187,7 @@ def delete_obj_index(self, instance):
187187
self.__index.delete_object(objectID)
188188
logger.debug('DELETE %s FROM %s', objectID, self.model)
189189

190-
def search(self, query='', params={}):
190+
def raw_search(self, query='', params={}):
191191
return self.__index.search(query, params)
192192

193193
def set_settings(self):

src/registration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ def delete_obj_index(self, obj):
103103
adapter = self.get_adapter_from_instance(obj)
104104
adapter.delete_obj_index(obj)
105105

106-
def search(self, model, query='', params={}):
106+
def raw_search(self, model, query='', params={}):
107107
adapter = self.get_adapter(model)
108-
return adapater.search(query, params)
108+
return adapater.raw_search(query, params)
109109

110110
# Signalling hooks.
111111

0 commit comments

Comments
 (0)