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

Commit ecf3d1b

Browse files
committed
Added query suggestions
1 parent b5331a6 commit ecf3d1b

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ The client can be configured to use a managed deploy by adjusting the `base_endp
186186
{'meta': {'page': {'current': 1, 'total_pages': 1, 'total_results': 2, 'size': 10}, ...}, 'results': [...]}
187187
```
188188

189-
#### Multi-Search
189+
### Multi-Search
190190

191191
```python
192192
>>> client.multi_search('favorite-videos', [{
@@ -199,6 +199,36 @@ The client can be configured to use a managed deploy by adjusting the `base_endp
199199
[{'meta': {...}, 'results': [...]}, {'meta': {...}, 'results': [...]}]
200200
```
201201

202+
### Query Suggestion
203+
204+
```python
205+
>>> client.query_suggestion('favorite-videos', 'cat', {
206+
'size': 10,
207+
'types': {
208+
'documents': {
209+
'fields': ['title']
210+
}
211+
}
212+
})
213+
{'results': {'documents': [{'suggestion': 'cat'}]}, 'meta': {'request_id': '390be384ad5888353e1b32adcfaaf1c9'}}
214+
```
215+
216+
```ruby
217+
engine_name = 'favorite-videos'
218+
219+
options = {
220+
:size => 3,
221+
:types => {
222+
:documents => {
223+
:fields => ['title']
224+
}
225+
}
226+
}
227+
228+
client.query_suggestion(engine_name, 'cat', options)
229+
```
230+
231+
202232
### Create a Signed Search Key
203233

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

swiftype_app_search/client.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,20 @@ def build_options_from_search(search):
186186
}
187187
return self.swiftype_session.request('get', endpoint, json=options)
188188

189+
def query_suggestion(self, engine_name, query, options=None):
190+
"""
191+
Request Query Suggestions. See https://swiftype.com/documentation/app-search/ for more details
192+
on options and return values.
193+
194+
:param engine_name: Name of engine to search over.
195+
:param query: Query string to search for.
196+
:param options: Dict of search options.
197+
"""
198+
endpoint = "engines/{}/query_suggestion".format(engine_name)
199+
options = options or {}
200+
options['query'] = query
201+
return self.swiftype_session.request('get', endpoint, json=options)
202+
189203

190204
@staticmethod
191205
def create_signed_search_key(api_key, api_key_name, options):

tests/test_client.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,16 @@ def test_multi_search(self):
229229
m.register_uri('GET', url, json=expected_return, status_code=200)
230230
response = self.client.multi_search(self.engine_name, {})
231231
self.assertEqual(response, expected_return)
232+
233+
def test_query_suggestion(self):
234+
query = 'query'
235+
expected_return = { 'meta': {}, 'results': {}}
236+
237+
with requests_mock.Mocker() as m:
238+
url = "{}/{}".format(
239+
self.client.swiftype_session.base_url,
240+
"engines/{}/query_suggestion".format(self.engine_name)
241+
)
242+
m.register_uri('GET', url, json=expected_return, status_code=200)
243+
response = self.client.query_suggestion(self.engine_name, query, {})
244+
self.assertEqual(response, expected_return)

0 commit comments

Comments
 (0)