Skip to content
This repository was archived by the owner on Sep 1, 2021. It is now read-only.

Commit c16d2cf

Browse files
committed
Added multi_search
1 parent da1fff2 commit c16d2cf

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,19 @@ The client can be configured to use a managed deploy by adjusting the `base_endp
155155
{'meta': {'page': {'current': 1, 'total_pages': 1, 'total_results': 2, 'size': 10}, ...}, 'results': [...]}
156156
```
157157

158+
#### Multi-Search
159+
160+
```python
161+
>>> client.multi_search('favorite-videos', [{
162+
'query': 'cat',
163+
'options': { 'search_fields': { 'title': {} }}
164+
},{
165+
'query': 'dog',
166+
'options': { 'search_fields': { 'body': {} }}
167+
}])
168+
[{'meta': {...}, 'results': [...]}, {'meta': {...}, 'results': [...]}]
169+
```
170+
158171
### Create a Signed Search Key
159172

160173
Creating a search key that will only search over the body field.

swiftype_app_search/client.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,31 @@ def search(self, engine_name, query, options=None):
134134
options['query'] = query
135135
return self.swiftype_session.request('get', endpoint, json=options)
136136

137+
def multi_search(self, engine_name, searches=None):
138+
"""
139+
Run multiple searches for documents on a single request.
140+
See https://swiftype.com/documentation/app-search/ for more details
141+
on options and return values.
142+
143+
:param engine_name: Name of engine to search over.
144+
:param options: Array of search options. ex. {query: String, options: Dict}
145+
"""
146+
147+
def build_options_from_search(search):
148+
if 'options' in search:
149+
options = search['options']
150+
else:
151+
options = {}
152+
options['query'] = search['query']
153+
return options
154+
155+
endpoint = "engines/{}/multi_search".format(engine_name)
156+
options = {
157+
'queries': map(build_options_from_search, searches)
158+
}
159+
return self.swiftype_session.request('get', endpoint, json=options)
160+
161+
137162
@staticmethod
138163
def create_signed_search_key(api_key, api_key_name, options):
139164
"""

tests/test_client.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,25 @@ def test_destroy_engine(self):
169169

170170
def test_search(self):
171171
query = 'query'
172+
expected_return = { 'meta': {}, 'results': []}
172173

173174
with requests_mock.Mocker() as m:
174175
url = "{}/{}".format(
175176
self.client.swiftype_session.base_url,
176177
"engines/{}/search".format(self.engine_name)
177178
)
178-
m.register_uri('GET', url, json={}, status_code=200)
179-
self.client.search(self.engine_name, query, {})
179+
m.register_uri('GET', url, json=expected_return, status_code=200)
180+
response = self.client.search(self.engine_name, query, {})
181+
self.assertEqual(response, expected_return)
182+
183+
def test_multi_search(self):
184+
expected_return = [{ 'meta': {}, 'results': []}, { 'meta': {}, 'results': []}]
185+
186+
with requests_mock.Mocker() as m:
187+
url = "{}/{}".format(
188+
self.client.swiftype_session.base_url,
189+
"engines/{}/multi_search".format(self.engine_name)
190+
)
191+
m.register_uri('GET', url, json=expected_return, status_code=200)
192+
response = self.client.multi_search(self.engine_name, {})
193+
self.assertEqual(response, expected_return)

0 commit comments

Comments
 (0)