Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ DistributedTransport transport
{ "batch_index_date", d.BatchIndexDate.ToString("o") }
}),
GetMapping = () => CreateMapping(null),
GetMappingSettings = () => CreateMappingSetting("docs"),
GetMappingSettings = () => CreateMappingSetting($"docs-{indexNamespace}"),
IndexFormat =
$"{endpoint.IndexNamePrefix.Replace("semantic", "lexical").ToLowerInvariant()}-{indexNamespace.ToLowerInvariant()}-{{0:yyyy.MM.dd.HHmmss}}",
ActiveSearchAlias = $"{endpoint.IndexNamePrefix.Replace("semantic", "lexical").ToLowerInvariant()}-{indexNamespace.ToLowerInvariant()}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,6 @@ namespace Elastic.Markdown.Exporters.Elasticsearch;
[EnumExtensions]
public enum IngestStrategy { Reindex, Multiplex }

internal sealed record SynonymSetRequest
{
[JsonPropertyName("synonyms")]
public required string[] Synonyms { get; init; }
}

[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.SnakeCaseLower)]
[JsonSerializable(typeof(SynonymSetRequest))]
internal sealed partial class SynonymSerializerContext : JsonSerializerContext { };

public class ElasticsearchMarkdownExporter : IMarkdownExporter, IDisposable
{
private readonly IDiagnosticsCollector _collector;
Expand All @@ -47,6 +37,7 @@ public class ElasticsearchMarkdownExporter : IMarkdownExporter, IDisposable
private readonly DateTimeOffset _batchIndexDate = DateTimeOffset.UtcNow;
private readonly DistributedTransport _transport;
private IngestStrategy _indexStrategy;
private readonly string _indexNamespace;
private string _currentLexicalHash = string.Empty;
private string _currentSemanticHash = string.Empty;

Expand All @@ -64,7 +55,7 @@ SynonymsConfiguration synonyms
_logger = logFactory.CreateLogger<ElasticsearchMarkdownExporter>();
_endpoint = endpoints.Elasticsearch;
_indexStrategy = IngestStrategy.Reindex;

_indexNamespace = indexNamespace;
var es = endpoints.Elasticsearch;

var configuration = new ElasticsearchConfiguration(es.Uri)
Expand Down Expand Up @@ -102,7 +93,7 @@ public async ValueTask StartAsync(Cancel ctx = default)
_currentLexicalHash = await _lexicalChannel.Channel.GetIndexTemplateHashAsync(ctx) ?? string.Empty;
_currentSemanticHash = await _semanticChannel.Channel.GetIndexTemplateHashAsync(ctx) ?? string.Empty;

await PublishSynonymsAsync("docs", ctx);
await PublishSynonymsAsync($"docs-{_indexNamespace}", ctx);
_ = await _lexicalChannel.Channel.BootstrapElasticsearchAsync(BootstrapMethod.Failure, null, ctx);

// if the previous hash does not match the current hash, we know already we want to multiplex to a new index
Expand Down Expand Up @@ -153,8 +144,22 @@ private async Task PublishSynonymsAsync(string setName, CancellationToken ctx)
{
_logger.LogInformation("Publishing synonym set '{SetName}' to Elasticsearch", setName);

var requestBody = new SynonymSetRequest { Synonyms = _synonyms.ToArray() };
var json = JsonSerializer.Serialize(requestBody, SynonymSerializerContext.Default.SynonymSetRequest);
var synonymRules = _synonyms.Aggregate(new List<SynonymRule>(), (acc, synonym) =>
{
acc.Add(new SynonymRule
{
Id = synonym.Split(",", StringSplitOptions.RemoveEmptyEntries)[0].Trim(),
Synonyms = synonym
});
return acc;
});

var synonymsSet = new SynonymsSet
{
Synonyms = synonymRules
};

var json = JsonSerializer.Serialize(synonymsSet, SynonymSerializerContext.Default.SynonymsSet);

var response = await _transport.PutAsync<StringResponse>($"_synonyms/{setName}", PostData.String(json), ctx);

Expand Down Expand Up @@ -456,3 +461,20 @@ public void Dispose()
GC.SuppressFinalize(this);
}
}

internal sealed record SynonymsSet
{
[JsonPropertyName("synonyms_set")]
public required List<SynonymRule> Synonyms { get; init; } = [];
}

internal sealed record SynonymRule
{
public required string Id { get; init; }
public required string Synonyms { get; init; }
}

[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.SnakeCaseLower)]
[JsonSerializable(typeof(SynonymsSet))]
[JsonSerializable(typeof(SynonymRule))]
internal sealed partial class SynonymSerializerContext : JsonSerializerContext;
Loading