Skip to content

Commit a8cf4ee

Browse files
committed
feat(csharp): guides
1 parent 645ce8e commit a8cf4ee

17 files changed

+490
-17
lines changed

docs/guides/csharp/src/program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
public class Program
1+
namespace Algolia;
2+
3+
public static class Program
24
{
35
public static void Main() { }
46
}
Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
1+
namespace Algolia;
2+
13
using System;
24
using System.Text.Json;
35
using System.Collections.Generic;
46

57
{{> snippets/import}}
68

79
using Action = Algolia.Search.Models.Ingestion.Action;
8-
using static Algolia.Search.Models.Ingestion.PushTaskRecords;
910

1011
class PushSetup
1112
{
12-
public static async Task Main(string[] args)
13+
async Task Main(string[] args)
1314
{
14-
string jsonContent = File.ReadAllText("records.json");
15+
try
16+
{
17+
var jsonContent = await File.ReadAllTextAsync("records.json");
1518
1619
var records = JsonSerializer.Deserialize<List<PushTaskRecords>>(jsonContent);
1720
1821
// use the region matching your applicationID
1922
{{> snippets/init}}
2023

21-
try
22-
{
23-
// setting `watch` to `true` will make the call synchronous
24-
var resp = {{#dynamicSnippet}}pushSetup{{/dynamicSnippet}};
25-
26-
Console.WriteLine(resp);
27-
}
28-
catch (Exception e)
29-
{
30-
Console.WriteLine(e.Message);
31-
}
24+
// setting `watch` to `true` will make the call synchronous
25+
var resp = {{#dynamicSnippet}}pushSetup{{/dynamicSnippet}};
26+
27+
Console.WriteLine(resp);
28+
}
29+
catch (Exception e)
30+
{
31+
Console.WriteLine(e.Message);
32+
}
3233
}
3334
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
namespace Algolia;
2+
3+
using System;
4+
using System.Text.Json;
5+
using System.Net.Http;
6+
using System.Collections.Generic;
7+
8+
{{> snippets/import}}
9+
10+
class DeleteMultipleIndices {
11+
12+
async Task Main(string[] args)
13+
{
14+
15+
// You need an API key with `deleteIndex`
16+
try {
17+
{{> snippets/init}}
18+
19+
// List all indices
20+
var indices = {{#dynamicSnippet}}listIndicesSimple{{/dynamicSnippet}};
21+
22+
// Primary indices don't have a `primary` key
23+
var primaryIndices = indices.Items.Where(item => item.Primary == null).ToList();
24+
var replicaIndices = indices.Items.Where(item => item.Primary != null).ToList();
25+
26+
// Delete primary indices first
27+
if (primaryIndices.Count > 0)
28+
{
29+
var requests = primaryIndices
30+
.Select(index => new MultipleBatchRequest(Search.Models.Search.Action.Delete, index.Name)).ToList();
31+
{{#dynamicSnippet}}deleteMultipleIndicesPrimary{{/dynamicSnippet}};
32+
Console.WriteLine("Deleted primary indices.");
33+
}
34+
35+
// Now, delete replica indices
36+
if (replicaIndices.Count > 0)
37+
{
38+
var requests = replicaIndices
39+
.Select(index => new MultipleBatchRequest(Search.Models.Search.Action.Delete, index.Name)).ToList();
40+
{{#dynamicSnippet}}deleteMultipleIndicesReplica{{/dynamicSnippet}};
41+
Console.WriteLine("Deleted replica indices.");
42+
}
43+
} catch (Exception e) {
44+
Console.WriteLine(e.Message);
45+
}
46+
}
47+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
namespace Algolia;
2+
3+
using System;
4+
using System.Text.Json;
5+
using System.Net.Http;
6+
using System.Collections.Generic;
7+
8+
{{> snippets/import}}
9+
10+
class SaveObjectsChunks {
11+
12+
async Task Main(string[] args)
13+
{
14+
15+
try {
16+
{{> snippets/init}}
17+
var jsonContent = await File.ReadAllTextAsync("actors.json");
18+
var records = JsonSerializer.Deserialize<List<Dictionary<string, object>>>(jsonContent);
19+
20+
const int chunkSize = 10000;
21+
22+
for (var beginIndex = 0; beginIndex < records?.Count; beginIndex += chunkSize)
23+
{
24+
var chunk = records.Slice(beginIndex, Math.Min(beginIndex + chunkSize, records.Count));
25+
{{#dynamicSnippet}}saveObjectsChunks{{/dynamicSnippet}};
26+
}
27+
} catch (Exception e) {
28+
Console.WriteLine(e.Message);
29+
}
30+
}
31+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
namespace Algolia;
2+
3+
using System;
4+
using System.Text.Json;
5+
using System.Net.Http;
6+
using System.Collections.Generic;
7+
8+
{{> snippets/import}}
9+
10+
class SaveObjectsMcm {
11+
12+
private readonly List<Dictionary<string, object>> playlists = []; // Your records
13+
14+
private static List<Dictionary<string, string>> GetAllAppIdConfigurations()
15+
{
16+
return [/* A list of your MCM AppID/ApiKey pairs */];
17+
}
18+
19+
async Task Main(string[] args)
20+
{
21+
22+
// Fetch from your own data storage and with your own code
23+
// the list of application IDs and API keys to target each cluster
24+
var configurations = GetAllAppIdConfigurations();
25+
26+
// Send the records to each cluster
27+
foreach (var config in configurations)
28+
{
29+
try
30+
{
31+
var client = new SearchClient(new SearchConfig(config.GetValueOrDefault("appID", ""), config.GetValueOrDefault("apiKey", "")));
32+
{{#dynamicSnippet}}saveObjectsPlaylists{{/dynamicSnippet}};
33+
} catch (Exception e)
34+
{
35+
Console.WriteLine(e.Message);
36+
}
37+
}
38+
}
39+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
namespace Algolia;
2+
3+
using System;
4+
using System.Text.Json;
5+
using System.Net.Http;
6+
using System.Collections.Generic;
7+
8+
{{> snippets/import}}
9+
10+
class SaveObjectsModified {
11+
12+
async Task Main(string[] args)
13+
{
14+
15+
try {
16+
{{> snippets/init}}
17+
18+
var jsonContent = await File.ReadAllTextAsync("products.json");
19+
var products = JsonSerializer.Deserialize<List<Dictionary<string, object>>>(jsonContent);
20+
21+
var records = products?.Select(product =>
22+
{
23+
var reference = product.GetValueOrDefault("product_reference", "") as string;
24+
var suffixes = new List<string>();
25+
26+
for (var i = reference!.Length; i > 1; i--)
27+
{
28+
suffixes.Add(reference[i..]);
29+
}
30+
31+
return new Dictionary<string, object>(product)
32+
{
33+
["suffixes"] = suffixes
34+
};
35+
}).ToList();
36+
37+
{{#dynamicSnippet}}saveObjectsRecords{{/dynamicSnippet}};
38+
} catch (Exception e) {
39+
Console.WriteLine(e.Message);
40+
}
41+
}
42+
}

templates/csharp/guides/search/saveObjectsMovies.mustache

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
namespace Algolia;
2+
13
using System;
24
using System.Text.Json;
35
using System.Net.Http;
@@ -7,7 +9,7 @@ using System.Collections.Generic;
79

810
class SaveObjectsMovies
911
{
10-
public static async Task Main(string[] args)
12+
async Task Main(string[] args)
1113
{
1214
// read json file from url
1315
var url = "https://dashboard.algolia.com/api/1/sample_datasets?type=movie";
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace Algolia;
2+
3+
using System;
4+
using System.Text.Json;
5+
using System.Net.Http;
6+
using System.Collections.Generic;
7+
8+
{{> snippets/import}}
9+
10+
class SaveObjectsPublicUser {
11+
12+
private static readonly List<Dictionary<string, object>> playlists = []; // Your records
13+
14+
async Task Main(string[] args)
15+
{
16+
17+
try {
18+
{{> snippets/init}}
19+
{{#dynamicSnippet}}saveObjectsPlaylistsWithUserIDPublic{{/dynamicSnippet}};
20+
} catch (Exception e) {
21+
Console.WriteLine(e.Message);
22+
}
23+
}
24+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
namespace Algolia;
2+
3+
using System;
4+
using System.Text.Json;
5+
using System.Net.Http;
6+
using System.Collections.Generic;
7+
8+
{{> snippets/import}}
9+
10+
class SavePopularRecords {
11+
12+
async Task Main(string[] args)
13+
{
14+
15+
try {
16+
{{> snippets/init}}
17+
var records = new List<Dictionary<string, object>>();
18+
19+
var hits = await client.BrowseObjectsAsync<Hit>("<YOUR_INDEX_NAME>", new BrowseParamsObject());
20+
21+
foreach (var hit in hits)
22+
{
23+
var props = hit.AdditionalProperties ?? new Dictionary<string, object>();
24+
var nbFollowers = props["nbFollowers"] as int? ?? 0;
25+
records.Add(
26+
new Dictionary<string, object>
27+
{
28+
["twitterHandle"] = props["twitterHandle"],
29+
["nbFollowers"] = nbFollowers,
30+
["isPopular"] = nbFollowers >= 1_000_000
31+
}
32+
);
33+
}
34+
35+
{{#dynamicSnippet}}saveObjectsRecords{{/dynamicSnippet}};
36+
} catch (Exception e) {
37+
Console.WriteLine(e.Message);
38+
}
39+
}
40+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace Algolia;
2+
3+
using System;
4+
using System.Text.Json;
5+
using System.Net.Http;
6+
using System.Collections.Generic;
7+
8+
{{> snippets/import}}
9+
10+
class SearchRecentlyPublishedBooks {
11+
12+
async Task Main(string[] args)
13+
{
14+
15+
try {
16+
{{> snippets/init}}
17+
var dateTimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
18+
var searchParams = new SearchParams(new SearchParamsObject
19+
{
20+
Query = "<YOUR_SEARCH_QUERY>",
21+
Filters = $"date_timestamp > {dateTimestamp}"
22+
}
23+
);
24+
25+
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}};
26+
} catch (Exception e) {
27+
Console.WriteLine(e.Message);
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)