diff --git a/gcloud/search/client.py b/gcloud/search/client.py index 138d62385e25..757533fc1f21 100644 --- a/gcloud/search/client.py +++ b/gcloud/search/client.py @@ -97,7 +97,7 @@ def index(self, name): """Construct an index bound to this client. :type name: string - :param name: Name of the zone. + :param name: Name of the index. :rtype: :class:`gcloud.search.index.Index` :returns: a new ``Index`` instance diff --git a/gcloud/search/index.py b/gcloud/search/index.py index 5f418fa2b188..0dc979a740c1 100644 --- a/gcloud/search/index.py +++ b/gcloud/search/index.py @@ -197,3 +197,18 @@ def list_documents(self, max_results=None, page_token=None, zones = [Document.from_api_repr(resource, self) for resource in resp['documents']] return zones, resp.get('nextPageToken') + + def document(self, name, rank=None): + """Construct a document bound to this index. + + :type name: string + :param name: Name of the document. + + :type rank: integer + :param rank: Rank of the document (defaults to a server-assigned + value based on timestamp). + + :rtype: :class:`gcloud.search.document.Document` + :returns: a new ``Document`` instance + """ + return Document(name, index=self, rank=rank) diff --git a/gcloud/search/test_index.py b/gcloud/search/test_index.py index 54d31ac17646..4627bc524e11 100644 --- a/gcloud/search/test_index.py +++ b/gcloud/search/test_index.py @@ -200,6 +200,33 @@ def test_list_documents_explicit(self): 'pageToken': TOKEN, 'view': 'FULL'}) + def test_document_defaults(self): + from gcloud.search.document import Document + DOCUMENT_ID = 'document-id' + client = _Client(self.PROJECT) + index = self._makeOne(self.INDEX_ID, client) + + document = index.document(DOCUMENT_ID) + + self.assertTrue(isinstance(document, Document)) + self.assertEqual(document.name, DOCUMENT_ID) + self.assertEqual(document.rank, None) + self.assertTrue(document.index is index) + + def test_document_explicit(self): + from gcloud.search.document import Document + DOCUMENT_ID = 'document-id' + RANK = 1234 + client = _Client(self.PROJECT) + index = self._makeOne(self.INDEX_ID, client) + + document = index.document(DOCUMENT_ID, rank=RANK) + + self.assertTrue(isinstance(document, Document)) + self.assertEqual(document.name, DOCUMENT_ID) + self.assertEqual(document.rank, RANK) + self.assertTrue(document.index is index) + class _Client(object):