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

Commit 4e4f214

Browse files
committed
Added support for listing documents
1 parent c16d2cf commit 4e4f214

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,24 @@ The client can be configured to use a managed deploy by adjusting the `base_endp
102102
[{'id': 'INscMGmhmX4','url': 'https://www.youtube.com/watch?v=INscMGmhmX4','title': 'The Original Grumpy Cat','body': 'A wonderful video of a magnificent cat.'}]
103103
```
104104

105+
### List Documents
106+
```python
107+
>>> engine_name = 'favorite-videos'
108+
>>> client.list_documents(engine_name, current=1, size=20)
109+
{
110+
'meta': {
111+
'page': {
112+
'current': 1,
113+
'total_pages': 1,
114+
'total_results': 2,
115+
'size': 20
116+
}
117+
},
118+
'results': [{'id': 'INscMGmhmX4','url': 'https://www.youtube.com/watch?v=INscMGmhmX4','title': 'The Original Grumpy Cat','body': 'A wonderful video of a magnificent cat.'}]
119+
}
120+
```
121+
122+
105123
### Destroy Documents
106124

107125
```python

swiftype_app_search/client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ def get_documents(self, engine_name, document_ids):
3535
data = json.dumps(document_ids)
3636
return self.swiftype_session.request('get', endpoint, data=data)
3737

38+
def list_documents(self, engine_name, current=1, size=20):
39+
"""
40+
Lists all documents in engine.
41+
42+
:param current: Page of documents
43+
:param size: Number of documents to return per page
44+
:return: List of documemts.
45+
"""
46+
data = { 'page': { 'current': current, 'size': size } }
47+
return self.swiftype_session.request('get', "engines/{}/documents/list".format(engine_name), json=data)
48+
3849
def index_document(self, engine_name, document):
3950
"""
4051
Create or update a document for an engine. Raises

tests/test_client.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,29 @@ def test_get_documents(self):
8787
response = self.client.get_documents(self.engine_name, [id])
8888
self.assertEqual(response, expected_return)
8989

90+
def test_list_documents(self):
91+
expected_return = {
92+
'meta': {
93+
'page': {'current': 1, 'total_results': 1, 'total_pages': 1, 'size': 20},
94+
'results': [
95+
{'body': 'this is a test', 'id': '1'},
96+
{'body': 'this is also a test', 'id': '2'}
97+
]
98+
}
99+
}
100+
101+
with requests_mock.Mocker() as m:
102+
url = "{}/engines/{}/documents/list".format(self.client.swiftype_session.base_url, self.engine_name)
103+
m.register_uri('GET',
104+
url,
105+
additional_matcher=lambda x: x.text == '{"page": {"current": 1, "size": 20}}',
106+
json=expected_return,
107+
status_code=200
108+
)
109+
110+
response = self.client.list_documents(self.engine_name)
111+
self.assertEqual(response, expected_return)
112+
90113
def test_destroy_documents(self):
91114
id = 'INscMGmhmX4'
92115
expected_return = [

0 commit comments

Comments
 (0)