Skip to content

Commit 48e3f64

Browse files
DOC-9567 - Add scoped index management examples (#253)
Also covers missing examples for bucket management and views. - Use changes made in this sdk-common PR: couchbase/docs-sdk-common#47 - Add Index management examples with scopes and collections (new in 3.3) - Fills in examples that were missing/commented out
1 parent 6a5cf54 commit 48e3f64

File tree

5 files changed

+384
-73
lines changed

5 files changed

+384
-73
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Run this using dotnet-script: https://github.com/filipw/dotnet-script
2+
//
3+
// dotnet script ProvisioningResourcesBuckets.csx
4+
//
5+
6+
#r "nuget: CouchbaseNetClient, 3.3.0"
7+
8+
using System;
9+
using System.Threading.Tasks;
10+
using Couchbase;
11+
using Couchbase.Management.Buckets;
12+
13+
await new ProvisioningResourcesBuckets().ExampleAsync();
14+
15+
public class ProvisioningResourcesBuckets
16+
{
17+
public async Task ExampleAsync()
18+
{
19+
// tag::creatingBucketMgr[]
20+
var cluster = await Cluster.ConnectAsync("couchbase://localhost", "Administrator", "password");
21+
var bucketMgr = cluster.Buckets;
22+
// end::creatingBucketMgr[]
23+
24+
{
25+
Console.WriteLine("[createBucket]");
26+
// tag::createBucket[]
27+
await bucketMgr.CreateBucketAsync(
28+
new BucketSettings{
29+
Name = "hello",
30+
FlushEnabled = false,
31+
ReplicaIndexes = true,
32+
RamQuotaMB = 150,
33+
NumReplicas = 1,
34+
BucketType = BucketType.Couchbase,
35+
ConflictResolutionType = ConflictResolutionType.SequenceNumber
36+
}
37+
);
38+
// end::createBucket[]
39+
}
40+
41+
{
42+
Console.WriteLine("[updateBucket]");
43+
// tag::updateBucket[]
44+
var settings = await bucketMgr.GetBucketAsync("hello");
45+
settings.FlushEnabled = true;
46+
47+
await bucketMgr.UpdateBucketAsync(settings);
48+
// end::updateBucket[]
49+
}
50+
51+
{
52+
Console.WriteLine("[flushBucket]");
53+
// tag::flushBucket[]
54+
await bucketMgr.FlushBucketAsync("hello");
55+
// end::flushBucket[]
56+
}
57+
58+
{
59+
Console.WriteLine("[removeBucket]");
60+
// tag::removeBucket[]
61+
await bucketMgr.DropBucketAsync("hello");
62+
// end::removeBucket[]
63+
}
64+
}
65+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Run this using dotnet-script: https://github.com/filipw/dotnet-script
2+
//
3+
// dotnet script ProvisioningResourcesViews.csx
4+
//
5+
6+
#r "nuget: CouchbaseNetClient, 3.3.0"
7+
8+
using System;
9+
using System.Threading.Tasks;
10+
using Couchbase;
11+
using Couchbase.Views;
12+
using Couchbase.Management.Views;
13+
14+
await new ProvisioningResourcesViews().ExampleAsync();
15+
16+
public class ProvisioningResourcesViews
17+
{
18+
public async Task ExampleAsync()
19+
{
20+
// tag::viewmgr[]
21+
var cluster = await Cluster.ConnectAsync("couchbase://localhost", "Administrator", "password");
22+
var bucket = await cluster.BucketAsync("travel-sample");
23+
var viewMgr = bucket.ViewIndexes;
24+
// end::viewmgr[]
25+
26+
{
27+
Console.WriteLine("[createView]");
28+
// tag::createView[]
29+
var views = new Dictionary<string, View>();
30+
views.Add(
31+
"by_country",
32+
new View{ Map = "function (doc, meta) { if (doc.type == 'landmark') { emit([doc.country, doc.city], null); } }" }
33+
);
34+
views.Add(
35+
"by_activity",
36+
new View{ Map="function (doc, meta) { if (doc.type == 'landmark') { emit([doc.country, doc.city], null); } }",
37+
Reduce="_count" }
38+
);
39+
40+
var designDocument = new DesignDocument { Name = "landmarks", Views = views };
41+
await viewMgr.UpsertDesignDocumentAsync(designDocument, DesignDocumentNamespace.Development);
42+
// end::createView[]
43+
}
44+
45+
{
46+
Console.WriteLine("[getView]");
47+
// tag::getView[]
48+
var designDocument = await viewMgr.GetDesignDocumentAsync("landmarks", DesignDocumentNamespace.Development);
49+
Console.WriteLine($"Design Document: {designDocument.Name}");
50+
// end::getView[]
51+
}
52+
53+
54+
{
55+
Console.WriteLine("[publishView]");
56+
// tag::publishView[]
57+
await viewMgr.PublishDesignDocumentAsync("landmarks");
58+
// end::publishView[]
59+
}
60+
61+
{
62+
Console.WriteLine("[removeView]");
63+
// tag::removeView[]
64+
await viewMgr.DropDesignDocumentAsync("landmarks", DesignDocumentNamespace.Production);
65+
// end::removeView[]
66+
}
67+
68+
}
69+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// Run this using dotnet-script: https://github.com/filipw/dotnet-script
2+
//
3+
// dotnet script QueryIndexManagerExample.csx
4+
//
5+
6+
#r "nuget: CouchbaseNetClient, 3.3.0"
7+
8+
using System;
9+
using System.Threading.Tasks;
10+
using Couchbase;
11+
using Couchbase.Management.Query;
12+
using Couchbase.Core.Exceptions;
13+
14+
await new QueryIndexManagerExample().ExampleAsync();
15+
16+
public class QueryIndexManagerExample
17+
{
18+
public async Task ExampleAsync()
19+
{
20+
// tag::creating-index-mgr[]
21+
var cluster = await Cluster.ConnectAsync("couchbase://localhost", "Administrator", "password");
22+
var queryIndexMgr = cluster.QueryIndexes;
23+
// end::creating-index-mgr[]
24+
25+
{
26+
Console.WriteLine("[primary]");
27+
// tag::primary[]
28+
await queryIndexMgr.CreatePrimaryIndexAsync(
29+
"travel-sample",
30+
new CreatePrimaryQueryIndexOptions()
31+
.ScopeName("tenant_agent_01")
32+
.CollectionName("users")
33+
// Set this if you wish to use a custom name
34+
// .IndexName("custom_name")
35+
.IgnoreIfExists(true)
36+
);
37+
// end::primary[]
38+
}
39+
40+
{
41+
Console.WriteLine("[secondary]");
42+
// tag::secondary[]
43+
try
44+
{
45+
await queryIndexMgr.CreateIndexAsync(
46+
"travel-sample",
47+
"tenant_agent_01_users_email",
48+
new[] { "preferred_email" },
49+
new CreateQueryIndexOptions()
50+
.ScopeName("tenant_agent_01")
51+
.CollectionName("users")
52+
);
53+
}
54+
catch (IndexExistsException)
55+
{
56+
Console.WriteLine("Index already exists!");
57+
}
58+
// end::secondary[]
59+
}
60+
61+
{
62+
Console.WriteLine("[defer-indexes]");
63+
// tag::defer-indexes[]
64+
try
65+
{
66+
// Create a deferred index
67+
await queryIndexMgr.CreateIndexAsync(
68+
"travel-sample",
69+
"tenant_agent_01_users_phone",
70+
new[] { "preferred_phone" },
71+
new CreateQueryIndexOptions()
72+
.ScopeName("tenant_agent_01")
73+
.CollectionName("users")
74+
.Deferred(true)
75+
);
76+
77+
// Build any deferred indexes within `travel-sample`.tenant_agent_01.users
78+
await queryIndexMgr.BuildDeferredIndexesAsync(
79+
"travel-sample",
80+
new BuildDeferredQueryIndexOptions()
81+
.ScopeName("tenant_agent_01")
82+
.CollectionName("users")
83+
);
84+
85+
// Wait for indexes to come online
86+
await queryIndexMgr.WatchIndexesAsync(
87+
"travel-sample",
88+
new[] { "tenant_agent_01_users_phone" },
89+
new WatchQueryIndexOptions()
90+
.ScopeName("users")
91+
.CollectionName("tenant_agent_01")
92+
);
93+
}
94+
catch (IndexExistsException)
95+
{
96+
Console.WriteLine("Index already exists!");
97+
}
98+
// end::defer-indexes[]
99+
}
100+
101+
{
102+
Console.WriteLine("[drop-primary-or-secondary-index]");
103+
// tag::drop-primary-or-secondary-index[]
104+
// Drop primary index
105+
await queryIndexMgr.DropPrimaryIndexAsync(
106+
"travel-sample",
107+
new DropPrimaryQueryIndexOptions()
108+
.ScopeName("tenant_agent_01")
109+
.CollectionName("users")
110+
);
111+
112+
// Drop secondary index
113+
await queryIndexMgr.DropIndexAsync(
114+
"travel-sample",
115+
"tenant_agent_01_users_email",
116+
new DropQueryIndexOptions()
117+
.ScopeName("tenant_agent_01")
118+
.CollectionName("users")
119+
);
120+
// end::drop-primary-or-secondary-index[]
121+
}
122+
}
123+
}

0 commit comments

Comments
 (0)