Skip to content

Commit f21411a

Browse files
committed
Refactor Elasticsearch exporter mappings, optimize ApplicableTo handling, and improve index creation logic
1 parent 6ff258b commit f21411a

File tree

3 files changed

+38
-41
lines changed

3 files changed

+38
-41
lines changed

src/Elastic.Documentation/AppliesTo/ApplicableToJsonConverter.cs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,7 @@ public override void Write(Utf8JsonWriter writer, ApplicableTo value, JsonSerial
167167

168168
// Stack
169169
if (value.Stack != null)
170-
{
171170
WriteApplicabilityEntries(writer, "stack", "stack", value.Stack);
172-
}
173171

174172
// Deployment
175173
if (value.Deployment != null)
@@ -197,9 +195,7 @@ public override void Write(Utf8JsonWriter writer, ApplicableTo value, JsonSerial
197195

198196
// Product (simple)
199197
if (value.Product != null)
200-
{
201198
WriteApplicabilityEntries(writer, "product", "product", value.Product);
202-
}
203199

204200
// ProductApplicability (specific products)
205201
if (value.ProductApplicability != null)
@@ -210,8 +206,7 @@ public override void Write(Utf8JsonWriter writer, ApplicableTo value, JsonSerial
210206
var yamlAlias = property.GetCustomAttribute<YamlMemberAttribute>()?.Alias;
211207
if (yamlAlias != null)
212208
{
213-
var propertyValue = property.GetValue(value.ProductApplicability) as AppliesCollection;
214-
if (propertyValue != null)
209+
if (property.GetValue(value.ProductApplicability) is AppliesCollection propertyValue)
215210
WriteApplicabilityEntries(writer, "product", yamlAlias, propertyValue);
216211
}
217212
}
@@ -220,22 +215,19 @@ public override void Write(Utf8JsonWriter writer, ApplicableTo value, JsonSerial
220215
writer.WriteEndArray();
221216
}
222217

223-
private static ProductLifecycle ParseLifecycle(string lifecycleStr)
218+
private static ProductLifecycle ParseLifecycle(string lifecycleStr) => lifecycleStr.ToLowerInvariant() switch
224219
{
225-
return lifecycleStr.ToLowerInvariant() switch
226-
{
227-
"preview" => ProductLifecycle.TechnicalPreview,
228-
"beta" => ProductLifecycle.Beta,
229-
"ga" => ProductLifecycle.GenerallyAvailable,
230-
"deprecated" => ProductLifecycle.Deprecated,
231-
"removed" => ProductLifecycle.Removed,
232-
"unavailable" => ProductLifecycle.Unavailable,
233-
"development" => ProductLifecycle.Development,
234-
"planned" => ProductLifecycle.Planned,
235-
"discontinued" => ProductLifecycle.Discontinued,
236-
_ => ProductLifecycle.GenerallyAvailable
237-
};
238-
}
220+
"preview" => ProductLifecycle.TechnicalPreview,
221+
"beta" => ProductLifecycle.Beta,
222+
"ga" => ProductLifecycle.GenerallyAvailable,
223+
"deprecated" => ProductLifecycle.Deprecated,
224+
"removed" => ProductLifecycle.Removed,
225+
"unavailable" => ProductLifecycle.Unavailable,
226+
"development" => ProductLifecycle.Development,
227+
"planned" => ProductLifecycle.Planned,
228+
"discontinued" => ProductLifecycle.Discontinued,
229+
_ => ProductLifecycle.GenerallyAvailable
230+
};
239231

240232
private static void WriteApplicabilityEntries(Utf8JsonWriter writer, string type, string subType, AppliesCollection collection)
241233
{
@@ -261,7 +253,7 @@ private static void WriteApplicabilityEntries(Utf8JsonWriter writer, string type
261253
};
262254
writer.WriteString("lifecycle", lifecycleName);
263255

264-
// Write version
256+
// Write the version
265257
if (applicability.Version is not null)
266258
writer.WriteString("version", applicability.Version.ToString());
267259

src/Elastic.Markdown/Exporters/Elasticsearch/ElasticsearchExporter.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,15 @@ protected static string CreateMapping(string? inferenceId) =>
193193
"prefix": { "type": "text", "analyzer" : "hierarchy_analyzer" }
194194
}
195195
},
196-
"applies" : {
197-
"type" : "nested"
196+
"applies_to" : {
197+
"type" : "nested",
198198
"properties" : {
199199
"type" : { "type" : "keyword" },
200200
"sub-type" : { "type" : "keyword" },
201201
"lifecycle" : { "type" : "keyword" },
202-
"version" : { "type" : "version" },
203-
}
204-
},
202+
"version" : { "type" : "version" }
203+
}
204+
},
205205
"hash" : { "type" : "keyword" },
206206
"title": {
207207
"type": "text",

src/Elastic.Markdown/Exporters/Elasticsearch/ElasticsearchMarkdownExporter.cs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information
44

55
using System.IO.Abstractions;
6+
using Elastic.Documentation.AppliesTo;
67
using Elastic.Documentation.Configuration;
78
using Elastic.Documentation.Diagnostics;
89
using Elastic.Documentation.Search;
@@ -82,17 +83,16 @@ public async ValueTask StartAsync(Cancel ctx = default)
8283
{
8384
_ = await _lexicalChannel.Channel.BootstrapElasticsearchAsync(BootstrapMethod.Failure, null, ctx);
8485

85-
var semanticIndex = _semanticChannel.Channel.IndexName;
8686
var semanticWriteAlias = string.Format(_semanticChannel.Channel.Options.IndexFormat, "latest");
87-
var semanticIndexHead = await _transport.HeadAsync(semanticWriteAlias, ctx);
88-
if (!semanticIndexHead.ApiCallDetails.HasSuccessfulStatusCode)
87+
var semanticIndexAvailable = await _transport.HeadAsync(semanticWriteAlias, ctx);
88+
_ = await _semanticChannel.Channel.BootstrapElasticsearchAsync(BootstrapMethod.Failure, null, ctx);
89+
var semanticIndex = _semanticChannel.Channel.IndexName;
90+
if (!semanticIndexAvailable.ApiCallDetails.HasSuccessfulStatusCode)
8991
{
90-
_logger.LogInformation("No semantic index exists yet, creating index {Index} for semantic search", semanticIndex);
91-
_ = await _semanticChannel.Channel.BootstrapElasticsearchAsync(BootstrapMethod.Failure, null, ctx);
92+
_logger.LogInformation("No semantic index existed yet, creating index {Index} for semantic search", semanticIndex);
9293
var semanticIndexPut = await _transport.PutAsync<StringResponse>(semanticIndex, PostData.String("{}"), ctx);
9394
if (!semanticIndexPut.ApiCallDetails.HasSuccessfulStatusCode)
9495
throw new Exception($"Failed to create index {semanticIndex}: {semanticIndexPut}");
95-
_ = await _semanticChannel.Channel.ApplyAliasesAsync(ctx);
9696
if (!_endpoint.ForceReindex)
9797
{
9898
_indexStrategy = IngestStrategy.Multiplex;
@@ -152,12 +152,13 @@ public async ValueTask StopAsync(Cancel ctx = default)
152152
throw new Exception($"Failed to create index {semanticIndex}: {semanticIndexPut}");
153153
_ = await _semanticChannel.Channel.ApplyAliasesAsync(ctx);
154154
}
155+
var destinationIndex = _semanticChannel.Channel.IndexName;
155156

156-
_logger.LogInformation("_reindex updates: '{SourceIndex}' => '{DestinationIndex}'", lexicalWriteAlias, semanticWriteAlias);
157+
_logger.LogInformation("_reindex updates: '{SourceIndex}' => '{DestinationIndex}'", lexicalWriteAlias, destinationIndex);
157158
var request = PostData.String(@"
158159
{
159160
""dest"": {
160-
""index"": """ + semanticWriteAlias + @"""
161+
""index"": """ + destinationIndex + @"""
161162
},
162163
""source"": {
163164
""index"": """ + lexicalWriteAlias + @""",
@@ -171,13 +172,13 @@ public async ValueTask StopAsync(Cancel ctx = default)
171172
}
172173
}
173174
}");
174-
await DoReindex(request, lexicalWriteAlias, semanticWriteAlias, "updates", ctx);
175+
await DoReindex(request, lexicalWriteAlias, destinationIndex, "updates", ctx);
175176

176-
_logger.LogInformation("_reindex deletions: '{SourceIndex}' => '{DestinationIndex}'", lexicalWriteAlias, semanticWriteAlias);
177+
_logger.LogInformation("_reindex deletions: '{SourceIndex}' => '{DestinationIndex}'", lexicalWriteAlias, destinationIndex);
177178
request = PostData.String(@"
178179
{
179180
""dest"": {
180-
""index"": """ + semanticWriteAlias + @"""
181+
""index"": """ + destinationIndex + @"""
181182
},
182183
""script"": {
183184
""source"": ""ctx.op = \""delete\""""
@@ -194,7 +195,7 @@ public async ValueTask StopAsync(Cancel ctx = default)
194195
}
195196
}
196197
}");
197-
await DoReindex(request, lexicalWriteAlias, semanticWriteAlias, "deletions", ctx);
198+
await DoReindex(request, lexicalWriteAlias, destinationIndex, "deletions", ctx);
198199

199200
await DoDeleteByQuery(lexicalWriteAlias, ctx);
200201

@@ -336,6 +337,10 @@ public async ValueTask<bool> ExportAsync(MarkdownExportFileContext fileContext,
336337
? body[..Math.Min(body.Length, 400)] + " " + string.Join(" \n- ", headings)
337338
: string.Empty;
338339

340+
// this is temporary until https://github.com/elastic/docs-builder/pull/2070 lands
341+
// this PR will add a service for us to resolve to a versioning scheme.
342+
var appliesTo = fileContext.SourceFile.YamlFrontMatter?.AppliesTo ?? ApplicableTo.Default;
343+
339344
var doc = new DocumentationDocument
340345
{
341346
Url = url,
@@ -344,7 +349,7 @@ public async ValueTask<bool> ExportAsync(MarkdownExportFileContext fileContext,
344349
StrippedBody = body.StripMarkdown(),
345350
Description = fileContext.SourceFile.YamlFrontMatter?.Description,
346351
Abstract = @abstract,
347-
Applies = fileContext.SourceFile.YamlFrontMatter?.AppliesTo,
352+
Applies = appliesTo,
348353
UrlSegmentCount = url.Split('/', StringSplitOptions.RemoveEmptyEntries).Length,
349354
Parents = navigation.GetParentsOfMarkdownFile(file).Select(i => new ParentDocument
350355
{
@@ -357,7 +362,7 @@ public async ValueTask<bool> ExportAsync(MarkdownExportFileContext fileContext,
357362
var semanticHash = _semanticChannel.Channel.ChannelHash;
358363
var lexicalHash = _lexicalChannel.Channel.ChannelHash;
359364
var hash = HashedBulkUpdate.CreateHash(semanticHash, lexicalHash,
360-
doc.Url, doc.Body ?? string.Empty, string.Join(",", doc.Headings.OrderBy(h => h))
365+
doc.Url, doc.Body ?? string.Empty, string.Join(",", doc.Headings.OrderBy(h => h)), doc.Url
361366
);
362367
doc.Hash = hash;
363368
doc.LastUpdated = _batchIndexDate;

0 commit comments

Comments
 (0)