@@ -25,16 +25,6 @@ namespace Elastic.Markdown.Exporters.Elasticsearch;
2525[ EnumExtensions ]
2626public enum IngestStrategy { Reindex , Multiplex }
2727
28- internal sealed record SynonymSetRequest
29- {
30- [ JsonPropertyName ( "synonyms" ) ]
31- public required string [ ] Synonyms { get ; init ; }
32- }
33-
34- [ JsonSourceGenerationOptions ( PropertyNamingPolicy = JsonKnownNamingPolicy . SnakeCaseLower ) ]
35- [ JsonSerializable ( typeof ( SynonymSetRequest ) ) ]
36- internal sealed partial class SynonymSerializerContext : JsonSerializerContext { } ;
37-
3828public class ElasticsearchMarkdownExporter : IMarkdownExporter , IDisposable
3929{
4030 private readonly IDiagnosticsCollector _collector ;
@@ -47,6 +37,7 @@ public class ElasticsearchMarkdownExporter : IMarkdownExporter, IDisposable
4737 private readonly DateTimeOffset _batchIndexDate = DateTimeOffset . UtcNow ;
4838 private readonly DistributedTransport _transport ;
4939 private IngestStrategy _indexStrategy ;
40+ private readonly string _indexNamespace ;
5041 private string _currentLexicalHash = string . Empty ;
5142 private string _currentSemanticHash = string . Empty ;
5243
@@ -64,7 +55,7 @@ SynonymsConfiguration synonyms
6455 _logger = logFactory . CreateLogger < ElasticsearchMarkdownExporter > ( ) ;
6556 _endpoint = endpoints . Elasticsearch ;
6657 _indexStrategy = IngestStrategy . Reindex ;
67-
58+ _indexNamespace = indexNamespace ;
6859 var es = endpoints . Elasticsearch ;
6960
7061 var configuration = new ElasticsearchConfiguration ( es . Uri )
@@ -102,7 +93,7 @@ public async ValueTask StartAsync(Cancel ctx = default)
10293 _currentLexicalHash = await _lexicalChannel . Channel . GetIndexTemplateHashAsync ( ctx ) ?? string . Empty ;
10394 _currentSemanticHash = await _semanticChannel . Channel . GetIndexTemplateHashAsync ( ctx ) ?? string . Empty ;
10495
105- await PublishSynonymsAsync ( "docs" , ctx ) ;
96+ await PublishSynonymsAsync ( $ "docs- { _indexNamespace } ", ctx ) ;
10697 _ = await _lexicalChannel . Channel . BootstrapElasticsearchAsync ( BootstrapMethod . Failure , null , ctx ) ;
10798
10899 // if the previous hash does not match the current hash, we know already we want to multiplex to a new index
@@ -153,8 +144,22 @@ private async Task PublishSynonymsAsync(string setName, CancellationToken ctx)
153144 {
154145 _logger . LogInformation ( "Publishing synonym set '{SetName}' to Elasticsearch" , setName ) ;
155146
156- var requestBody = new SynonymSetRequest { Synonyms = _synonyms . ToArray ( ) } ;
157- var json = JsonSerializer . Serialize ( requestBody , SynonymSerializerContext . Default . SynonymSetRequest ) ;
147+ var synonymRules = _synonyms . Aggregate ( new List < SynonymRule > ( ) , ( acc , synonym ) =>
148+ {
149+ acc . Add ( new SynonymRule
150+ {
151+ Id = synonym . Split ( "," , StringSplitOptions . RemoveEmptyEntries ) [ 0 ] . Trim ( ) ,
152+ Synonyms = synonym
153+ } ) ;
154+ return acc ;
155+ } ) ;
156+
157+ var synonymsSet = new SynonymsSet
158+ {
159+ Synonyms = synonymRules
160+ } ;
161+
162+ var json = JsonSerializer . Serialize ( synonymsSet , SynonymSerializerContext . Default . SynonymsSet ) ;
158163
159164 var response = await _transport . PutAsync < StringResponse > ( $ "_synonyms/{ setName } ", PostData . String ( json ) , ctx ) ;
160165
@@ -456,3 +461,20 @@ public void Dispose()
456461 GC . SuppressFinalize ( this ) ;
457462 }
458463}
464+
465+ internal sealed record SynonymsSet
466+ {
467+ [ JsonPropertyName ( "synonyms_set" ) ]
468+ public required List < SynonymRule > Synonyms { get ; init ; } = [ ] ;
469+ }
470+
471+ internal sealed record SynonymRule
472+ {
473+ public required string Id { get ; init ; }
474+ public required string Synonyms { get ; init ; }
475+ }
476+
477+ [ JsonSourceGenerationOptions ( PropertyNamingPolicy = JsonKnownNamingPolicy . SnakeCaseLower ) ]
478+ [ JsonSerializable ( typeof ( SynonymsSet ) ) ]
479+ [ JsonSerializable ( typeof ( SynonymRule ) ) ]
480+ internal sealed partial class SynonymSerializerContext : JsonSerializerContext ;
0 commit comments