Skip to content

Commit 923d44b

Browse files
committed
Initial commit of Cloud Search client.
1 parent b0aef16 commit 923d44b

17 files changed

+3252
-1
lines changed

docs/_static/js/main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ $('.headerlink').parent().each(function() {
1616
$('.side-nav').children('ul:nth-child(2)').children().each(function() {
1717
var itemName = $(this).text();
1818
if (itemName !== 'Datastore' && itemName !== 'Storage' &&
19-
itemName !== 'Pub/Sub') {
19+
itemName !== 'Pub/Sub' && itemName !== 'Search') {
2020
$(this).css('padding-left','2em');
2121
}
2222
});

docs/index.rst

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
datastore-transactions
1111
datastore-batches
1212
datastore-dataset
13+
search-api
14+
search-usage
1315
storage-api
1416
storage-blobs
1517
storage-buckets

docs/search-api.rst

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.. toctree::
2+
:maxdepth: 1
3+
:hidden:
4+
5+
Search
6+
------

docs/search-usage.rst

+314
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,314 @@
1+
Using the API
2+
=============
3+
4+
Connection / Authorization
5+
--------------------------
6+
7+
Implicitly use the default client:
8+
9+
.. doctest::
10+
11+
>>> from gcloud import search
12+
>>> # The search module has the same methods as a client, using the default.
13+
>>> search.list_indexes() # API request
14+
[]
15+
16+
Configure the default client:
17+
18+
.. doctest::
19+
20+
>>> from gcloud import search
21+
>>> search.set_project_id('project-id')
22+
>>> search.set_credentials(credentials)
23+
>>> search.list_indexes() # API request
24+
[]
25+
26+
Explicitly use the default client:
27+
28+
.. doctest::
29+
30+
>>> from gcloud.search import default_client as client
31+
>>> # The default_client is equivalent to search.Client()
32+
>>> client.list_indexes() # API request
33+
[]
34+
35+
Explicitly configure a client:
36+
37+
.. doctest::
38+
39+
>>> from gcloud import search
40+
>>> client = search.Client(project_id='project-id', credentials=credentials)
41+
>>> client.list_indexes() # API request
42+
[]
43+
44+
Manage indexes for a project
45+
----------------------------
46+
47+
Create a new index:
48+
49+
.. doctest::
50+
51+
>>> from gcloud import search
52+
>>> client = search.Client()
53+
>>> index = client.create_index('index_id') # API request
54+
>>> index.id
55+
'index_id'
56+
57+
Create a new index with a name:
58+
59+
.. doctest::
60+
61+
>>> from gcloud import search
62+
>>> client = search.Client()
63+
>>> index = client.create_index('index_id', name='Name') # API request
64+
>>> index.name
65+
'Name'
66+
67+
Get or create an index:
68+
69+
.. doctest::
70+
71+
>>> from gcloud import search
72+
>>> client = search.Client()
73+
>>> index = client.get_or_create_index('index_id') # API request
74+
>>> index.id
75+
'index_id'
76+
77+
List the indexes:
78+
79+
.. doctest::
80+
81+
>>> from gcloud import search
82+
>>> client = search.Client()
83+
>>> [index.id for index in client.list_indexes()] # API request
84+
['index_id']
85+
86+
Retrieve an index:
87+
88+
.. doctest::
89+
90+
>>> from gcloud import search
91+
>>> client = search.Client()
92+
>>> index = client.get_index('missing_index_id') # API request
93+
>>> index is None
94+
True
95+
>>> index = client.get_index('index_id') # API request
96+
>>> index.id
97+
'index_id'
98+
99+
Get an index without making an API request
100+
101+
.. doctest::
102+
103+
>>> from gcloud import search
104+
>>> client = search.Client()
105+
>>> index = client.get_index('index_id', check=False)
106+
>>> index.id
107+
'index_id'
108+
109+
Update an index:
110+
111+
.. doctest::
112+
113+
>>> from gcloud import search
114+
>>> client = search.Client()
115+
>>> index = client.get_index('index_id') # API request
116+
>>> index.name = 'Name'
117+
>>> client.update_index(index)
118+
119+
Delete an index by ID:
120+
121+
.. doctest::
122+
123+
>>> from gcloud import search
124+
>>> client = search.Client()
125+
>>> client.delete_index('index_id') # API request
126+
127+
Delete an index:
128+
129+
.. doctest::
130+
131+
>>> from gcloud import search
132+
>>> client = search.Client()
133+
>>> index = client.get_index('index_id') # API request
134+
>>> index.id
135+
'index_id'
136+
>>> client.delete_index(index) # API request
137+
138+
Manage documents and fields
139+
---------------------------
140+
141+
Create a document
142+
143+
.. doctest::
144+
145+
>>> from gcloud import search
146+
>>> document = search.Document('document_id', rank=0)
147+
>>> document.id
148+
'document_id'
149+
150+
Add a field to a document
151+
152+
.. doctest::
153+
154+
>>> from gcloud import search
155+
>>> document = search.Document('document_id')
156+
>>> document.add_field(search.Field('fieldname'))
157+
158+
Add values to a field
159+
160+
.. doctest::
161+
162+
>>> from datetime import datetime
163+
>>> from gcloud import search
164+
>>> field = search.Field('fieldname')
165+
>>> field.add_value('string')
166+
>>> # Tokenization field ignored for non-string values.
167+
>>> field.add_value('<h1>string</h1>', tokenization='html')
168+
>>> field.add_value('string', tokenization='atom')
169+
>>> field.add_value('string', tokenization='text')
170+
>>> field.add_value(1234)
171+
>>> field.add_value(datetime.now())
172+
>>> len(field.values)
173+
9
174+
175+
Add values to a field at initialization time
176+
177+
.. doctest::
178+
179+
>>> from gcloud import search
180+
>>> field = search.Field('fieldname', values=[
181+
'string',
182+
search.Value('<h1>string2</h1>', tokenization='html')
183+
search.Value('string', tokenization='atom')])
184+
185+
Add a single document to an index:
186+
187+
.. doctest::
188+
189+
>>> from gcloud import search
190+
>>> client = search.Client()
191+
>>> index = client.get_index('index_id') # API request
192+
>>> document = search.Document('document_id', rank=0)
193+
>>> index.add_document(document) # API request
194+
195+
Add multiple documents to an index:
196+
197+
.. doctest::
198+
199+
>>> from gcloud import search
200+
>>> client = search.Client()
201+
>>> index = client.get_index('index_id') # API request
202+
>>> documents = [search.Document('document_id')]
203+
>>> index.add_documents(documents) # API request
204+
205+
Get a single document by ID:
206+
207+
.. doctest::
208+
209+
>>> from gcloud import search
210+
>>> client = search.Client()
211+
>>> index = client.get_index('index_id') # API request
212+
>>> document = index.get_document('missing_document_id') # API request
213+
>>> document is None
214+
True
215+
>>> document = index.get_document('document_id') # API request
216+
>>> document.fields
217+
[]
218+
219+
Delete a document by ID:
220+
221+
.. doctest::
222+
223+
>>> from gcloud import search
224+
>>> client = search.Client()
225+
>>> index = client.get_index('index_id') # API request
226+
>>> index.delete_document('document_id') # API request
227+
>>> index.delete_document('missing_document_id') # API request
228+
229+
Searching
230+
---------
231+
232+
Create a query
233+
234+
.. doctest::
235+
236+
>>> from gcloud import search
237+
>>> query = search.Query('query text here')
238+
>>> query.query
239+
'query text here'
240+
241+
Specify the fields to return in a query
242+
243+
.. doctest::
244+
245+
>>> from gcloud import search
246+
>>> query = search.Query('query text here', fields=['field1', 'field2'])
247+
>>> query.fields
248+
['field1', 'field2']
249+
250+
Set the sort order of a query
251+
252+
.. doctest::
253+
254+
>>> from gcloud import search
255+
>>> query = search.Query('query text here', order_by='field1')
256+
>>> query.order_by
257+
'field1'
258+
>>> query2 = search.Query('query text here', order_by=['field2', 'field3'])
259+
>>> query2.order_by
260+
['field2', 'field3']
261+
>>> # Order descending by field1 and ascending by field2
262+
>>> query4 = search.Query('query text here', order_by=['-field1', 'field2'])
263+
>>> query3.order_by
264+
['-field1', 'field2']
265+
266+
Set custom field expressions on a query
267+
268+
.. doctest::
269+
270+
>>> from gcloud import search
271+
>>> query = search.Query('query text here')
272+
>>> query.add_field_expression('total_price', '(price + tax)')
273+
>>> # We don't do any checks on the expression. These are checked at query time.
274+
>>> query.add_field_expression('invalid', 'is_prime(num)')
275+
>>> query.add_field_expression('bad_field', '(missing_field + tax)')
276+
277+
Set custom field expressions at initialization time
278+
279+
.. doctest::
280+
281+
>>> from gcloud import search
282+
>>> query = search.Query('query text here', field_expressions={
283+
'total_price': '(price + tax)'})
284+
285+
Search an index
286+
287+
.. doctest::
288+
289+
>>> from gcloud import search
290+
>>> client = search.Client()
291+
>>> index = client.get_index('index_id') # API request
292+
>>> matching = index.search(search.Query('query text here')) # API request
293+
>>> for document in matching:
294+
... print document.id
295+
296+
Search an index with a limit on number of results
297+
298+
.. doctest::
299+
300+
>>> from gcloud import search
301+
>>> client = search.Client()
302+
>>> index = client.get_index('index_id') # API request
303+
>>> matching = index.search(search.Query('query text here'),
304+
... limit=42) # API request
305+
306+
Search an index with a custom page size (advanced)
307+
308+
.. doctest::
309+
310+
>>> from gcloud import search
311+
>>> client = search.Client()
312+
>>> index = client.get_index('index_id') # API request
313+
>>> matching = index.search(search.Query('query text here'),
314+
... page_size=20) # API request

gcloud/search/__init__.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Search API wrapper.
16+
17+
The main concepts with this API are:
18+
19+
- :class:`gcloud.pubsub.topic.Topic` represents an endpoint to which messages
20+
can be published using the Cloud Storage Pubsub API.
21+
22+
- :class:`gcloud.pubsub.subscription.Subscription` represents a named
23+
subscription (either pull or push) to a topic.
24+
"""
25+
26+
from gcloud.search.client import Client
27+
from gcloud.search.connection import SCOPE

0 commit comments

Comments
 (0)