Skip to content

Commit 4e62d0b

Browse files
Merge pull request #109 from algolia/search-facets
Implement facet_search
2 parents 09ae7c6 + cba8a0c commit 4e62d0b

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
CHANGELOG
22

3+
2016-10-19 1.10.0
4+
* Add `attribute_to_retrieve` to `get_objects`
5+
* Add `no_create` with `partial_update_object`
6+
* Implement the search in facet API end point
7+
38
2016-08-08 1.9.2
49
* Fix error on large API keys by including them in the JSON body instead of as a header
510
* Fix potential parsing error in case of httpCode == 4XX

algoliasearch/index.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,24 @@ def batch(self, requests, no_create=False):
904904

905905
return self._req(False, '/batch', 'POST', data=requests)
906906

907+
def search_facet(self, facet_name, facet_query, query=None):
908+
"""
909+
Perform a search within a given facet's values
910+
@param facet_name name of the facet to search. It must have been
911+
declared in the index's `attributesForFacetting` setting with the
912+
`searchable()` modifier.
913+
@param facet_query text to search for in the facet's values.
914+
@param query an optional query to take extra search parameters into
915+
account. The parameters apply to index objects like in a regular
916+
search query. Only facet values contained in the matched objects
917+
will be returned.
918+
"""
919+
if query is None:
920+
query = {}
921+
query['facetQuery'] = facet_query
922+
path = '/facets/%s/query' % safe(facet_name)
923+
return self._req(True, path, 'POST', data={'params' : urlencode(urlify(query))})
924+
907925
def _req(self, is_search, path, meth, params=None, data=None):
908926
"""Perform an HTTPS request with retry logic."""
909927
path = '%s%s' % (self._request_path, path)

algoliasearch/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION = "1.9.2"
1+
VERSION = "1.10.0"

tests/test_index.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,61 @@ def test_synonyms(self):
135135
task = self.index.search_synonyms('', hits_per_page=5)
136136
self.assertEqual(0, int(task['nbHits']))
137137

138+
def test_facet_search(self):
139+
settings = {'attributesForFacetting': ['searchable(series)', 'kind']}
140+
objects = [
141+
{
142+
'objectID': '1',
143+
'name': 'Snoopy',
144+
'kind': [ 'dog', 'animal' ],
145+
'born': 1950,
146+
'series': 'Peanuts'
147+
},
148+
{
149+
'objectID': '2',
150+
'name': 'Woodstock',
151+
'kind': ['bird', 'animal' ],
152+
'born': 1960,
153+
'series': 'Peanuts'
154+
},
155+
{
156+
'objectID': '3',
157+
'name': 'Charlie Brown',
158+
'kind': [ 'human' ],
159+
'born': 1950,
160+
'series': 'Peanuts'
161+
},
162+
{
163+
'objectID': '4',
164+
'name': 'Hobbes',
165+
'kind': ['tiger', 'animal', 'teddy' ],
166+
'born': 1985,
167+
'series': 'Calvin & Hobbes'
168+
},
169+
{
170+
'objectID': '5',
171+
'name': 'Calvin',
172+
'kind': [ 'human' ],
173+
'born': 1985,
174+
'series': 'Calvin & Hobbes'
175+
}
176+
]
177+
178+
self.index.set_settings(settings)
179+
task = self.index.add_objects(objects)
180+
self.index.wait_task(task['taskID'])
181+
182+
# Straightforward search.
183+
facetHits = self.index.search_facet('series', 'Hobb')['facetHits']
184+
self.assertEqual(len(facetHits), 1)
185+
self.assertEqual(facetHits[0]['value'], 'Calvin & Hobbes')
186+
self.assertEqual(facetHits[0]['count'], 2)
187+
188+
# Using an addition query to restrict search.
189+
query = {'facetFilters': 'kind:animal', 'numericFilters': 'born >= 1955'}
190+
facetHits = self.index.search_facet('series', 'Peanutz', query)['facetHits']
191+
self.assertEqual(facetHits[0]['value'], 'Peanuts')
192+
self.assertEqual(facetHits[0]['count'], 1)
138193

139194
class IndexWithReadOnlyDataTest(IndexTest):
140195
"""Tests that use one index with initial data (read only)."""

0 commit comments

Comments
 (0)