Repository navigation
Expand file tree
/
Copy pathexample_request.py
More file actions
56 lines (43 loc) · 1.74 KB
/
Copy pathexample_request.py
File metadata and controls
56 lines (43 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# section: code
from ibmcloudant.cloudant_v1 import Analyzer, AnalyzerConfiguration, CloudantV1, DesignDocument, DesignDocumentOptions, DesignDocumentViewsMapReduce, SearchIndexDefinition
service = CloudantV1.new_instance()
email_view_map_reduce = DesignDocumentViewsMapReduce(
map='function(doc) { if(doc.email_verified === true) { emit(doc.email, [doc.name, doc.email_verified, doc.joined]); }}'
)
user_index = SearchIndexDefinition(
index='function(doc) { index("name", doc.name); index("active", doc.active); }',
analyzer=AnalyzerConfiguration(name="standard", fields={"email": Analyzer(name="email")}))
design_document = DesignDocument(
views={'getVerifiedEmails': email_view_map_reduce},
indexes={'activeUsers': user_index}
)
response = service.put_design_document(
db='users',
design_document=design_document,
ddoc='allusers'
).get_result()
print(response)
# Partitioned DesignDocument Example
product_map = DesignDocumentViewsMapReduce(
map='function(doc) { emit(doc.productId, [doc.date, doc.eventType, doc.userId]); }'
)
date_index = SearchIndexDefinition(
index='function(doc) { index("date", doc.date); }',
analyzer=AnalyzerConfiguration(name="classic", fields={"description": Analyzer(name="english")})
)
design_document_options = DesignDocumentOptions(
partitioned=True
)
partitioned_design_doc = DesignDocument(
views={'byProductId': product_map},
indexes={'findByDate': date_index},
options=design_document_options
)
response = service.put_design_document(
db='events',
design_document=partitioned_design_doc,
ddoc='checkout'
).get_result()
print(response)
# section: markdown
# This example creates `allusers` design document in the `users` database and `checkout` design document in the partitioned `events` database.