Skip to content

Commit 00e6399

Browse files
committed
Added extension for Elasticsearch error handling
1 parent 22663fe commit 00e6399

File tree

2 files changed

+49
-59
lines changed

2 files changed

+49
-59
lines changed

TreeHouse.QuestIndexer/ElasticsearchExtensions.cs

+31
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
using System;
22
using System.Linq.Expressions;
3+
using System.Text;
4+
using System.Threading.Tasks;
5+
using Elastic.Clients.Elasticsearch;
36
using Elastic.Clients.Elasticsearch.IndexManagement;
47
using Elastic.Clients.Elasticsearch.Mapping;
8+
using Elastic.Transport.Products.Elasticsearch;
59

610
namespace TreeHouse.QuestIndexer;
711

@@ -32,4 +36,31 @@ public static PropertiesDescriptor<T> IdKeywordWithNumber<T>(this PropertiesDesc
3236
public static IndexSettingsDescriptor<T> SingleNode<T>(this IndexSettingsDescriptor<T> desc) => desc
3337
.NumberOfShards(1)
3438
.NumberOfReplicas(0);
39+
40+
public static TResponse CheckSuccess<TResponse>(this TResponse response, string? operation = null)
41+
where TResponse : ElasticsearchResponse
42+
{
43+
if (!response.IsSuccess())
44+
{
45+
StringBuilder builder = new();
46+
builder.Append("Elasticsearch API call failed");
47+
if (operation != null)
48+
{
49+
builder.Append(" while ");
50+
builder.Append(operation);
51+
}
52+
builder.AppendLine(":");
53+
builder.AppendLine(response.ToString());
54+
55+
throw new InvalidOperationException(builder.ToString());
56+
}
57+
58+
return response;
59+
}
60+
61+
public static async Task<TResponse> CheckSuccess<TResponse>(this Task<TResponse> responseTask, string? operation = null)
62+
where TResponse : ElasticsearchResponse
63+
{
64+
return CheckSuccess(await responseTask, operation);
65+
}
3566
}

TreeHouse.QuestIndexer/Program.cs

+18-59
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.CommandLine;
44
using System.Diagnostics;
@@ -8,7 +8,6 @@
88
using System.Threading.Tasks;
99
using Elastic.Clients.Elasticsearch;
1010
using Elastic.Clients.Elasticsearch.Core.Search;
11-
using Elastic.Clients.Elasticsearch.IndexManagement;
1211
using Microsoft.Data.Sqlite;
1312
using TreeHouse.Common;
1413
using TreeHouse.Common.CommandLine;
@@ -63,7 +62,7 @@ static async Task IndexQuests(string elasticUrl, FileInfo source)
6362
{
6463
ElasticsearchClient client = CreateClient(elasticUrl);
6564

66-
CreateIndexResponse response = await client.Indices.CreateAsync<Quest>(i => i
65+
await client.Indices.CreateAsync<Quest>(i => i
6766
.Settings(s => s.SingleNode())
6867
.Mappings(m => m
6968
.Properties(p => p
@@ -76,14 +75,7 @@ static async Task IndexQuests(string elasticUrl, FileInfo source)
7675
.TextEnglishWithKeyword(x => x.Condition)
7776
)
7877
)
79-
);
80-
81-
if (!response.IsSuccess())
82-
{
83-
Console.WriteLine("Could not create index ol-quest.");
84-
Console.WriteLine(response);
85-
return;
86-
}
78+
).CheckSuccess("creating index ol-quest");
8779

8880
using SqliteConnection connection = new(new SqliteConnectionStringBuilder()
8981
{
@@ -126,23 +118,17 @@ static async Task IndexQuests(string elasticUrl, FileInfo source)
126118
quests.Add(quest);
127119
}
128120

129-
BulkResponse indexRespose = await client.IndexManyAsync(quests);
130-
if (!indexRespose.IsSuccess())
131-
{
132-
Console.WriteLine("Failed to index quests.");
133-
Console.WriteLine(indexRespose);
134-
return;
135-
}
121+
await client.IndexManyAsync(quests).CheckSuccess("indexing quests");
136122

137-
await client.Indices.RefreshAsync<Quest>();
138-
await client.Indices.ForcemergeAsync<Quest>();
123+
await client.Indices.RefreshAsync<Quest>().CheckSuccess();
124+
await client.Indices.ForcemergeAsync<Quest>().CheckSuccess();
139125
}
140126

141127
static async Task IndexDialogs(string elasticUrl, FileInfo source)
142128
{
143129
ElasticsearchClient client = CreateClient(elasticUrl);
144130

145-
CreateIndexResponse response = await client.Indices.CreateAsync<Dialog>(i => i
131+
await client.Indices.CreateAsync<Dialog>(i => i
146132
.Settings(s => s.SingleNode())
147133
.Mappings(m => m
148134
.Properties(p => p
@@ -151,14 +137,7 @@ static async Task IndexDialogs(string elasticUrl, FileInfo source)
151137
.IntegerNumber(x => x.Ver)
152138
)
153139
)
154-
);
155-
156-
if (!response.IsSuccess())
157-
{
158-
Console.WriteLine("Could not create index ol-dialog.");
159-
Console.WriteLine(response);
160-
return;
161-
}
140+
).CheckSuccess("creating index ol-dialog");
162141

163142
using SqliteConnection connection = new(new SqliteConnectionStringBuilder()
164143
{
@@ -183,23 +162,17 @@ static async Task IndexDialogs(string elasticUrl, FileInfo source)
183162
});
184163
}
185164

186-
BulkResponse indexRespose = await client.IndexManyAsync(dialogs);
187-
if (!indexRespose.IsSuccess())
188-
{
189-
Console.WriteLine("Failed to index dialogs.");
190-
Console.WriteLine(indexRespose);
191-
return;
192-
}
165+
await client.IndexManyAsync(dialogs).CheckSuccess("indexing dialogs");
193166

194-
await client.Indices.RefreshAsync<Dialog>();
195-
await client.Indices.ForcemergeAsync<Dialog>();
167+
await client.Indices.RefreshAsync<Dialog>().CheckSuccess();
168+
await client.Indices.ForcemergeAsync<Dialog>().CheckSuccess();
196169
}
197170

198171
static async Task IndexImages(string elasticUrl, DirectoryInfo source)
199172
{
200173
ElasticsearchClient client = CreateClient(elasticUrl);
201174

202-
CreateIndexResponse response = await client.Indices.CreateAsync<Image>(i => i
175+
await client.Indices.CreateAsync<Image>(i => i
203176
.Settings(s => s.SingleNode())
204177
.Mappings(m => m
205178
.Properties(p => p
@@ -211,14 +184,7 @@ static async Task IndexImages(string elasticUrl, DirectoryInfo source)
211184
)
212185
)
213186
)
214-
);
215-
216-
if (!response.IsSuccess())
217-
{
218-
Console.WriteLine("Could not create index ol-image.");
219-
Console.WriteLine(response);
220-
return;
221-
}
187+
).CheckSuccess("creating index ol-image");
222188

223189
int batchSize = 100;
224190

@@ -245,23 +211,16 @@ static async Task IndexImages(string elasticUrl, DirectoryInfo source)
245211
stopwatch.Stop();
246212
Console.WriteLine($"{batchIdx + 1}/{fileBatches.Count} {stopwatch.Elapsed}");
247213

248-
BulkResponse indexRespose = await client.IndexManyAsync(
214+
await client.IndexManyAsync(
249215
fileBatch.Zip(featuresBatch).Select(x => new Image{
250216
FileName = x.First.FullName,
251217
Features = x.Second
252218
})
253-
);
254-
255-
if (!indexRespose.IsSuccess())
256-
{
257-
Console.WriteLine("Failed to index dialogs.");
258-
Console.WriteLine(indexRespose);
259-
return;
260-
}
219+
).CheckSuccess("indexing images");
261220
}
262221

263-
await client.Indices.RefreshAsync<Image>();
264-
await client.Indices.ForcemergeAsync<Image>();
222+
await client.Indices.RefreshAsync<Image>().CheckSuccess();
223+
await client.Indices.ForcemergeAsync<Image>().CheckSuccess();
265224

266225
Console.WriteLine("done");
267226
}
@@ -286,7 +245,7 @@ static async Task SearchImages(string elasticUrl, int size, FileInfo image)
286245
.DocvalueFields(f => f
287246
.Field(x => x.FileName)
288247
)
289-
);
248+
).CheckSuccess();
290249

291250
foreach (Hit<Image> hit in response.Hits)
292251
{

0 commit comments

Comments
 (0)