diff --git a/Elastic.SemanticKernel.Connectors.Elasticsearch/ElasticsearchKernelBuilderExtensions.cs b/Elastic.SemanticKernel.Connectors.Elasticsearch/ElasticsearchKernelBuilderExtensions.cs index 28ec951..b860331 100644 --- a/Elastic.SemanticKernel.Connectors.Elasticsearch/ElasticsearchKernelBuilderExtensions.cs +++ b/Elastic.SemanticKernel.Connectors.Elasticsearch/ElasticsearchKernelBuilderExtensions.cs @@ -31,16 +31,62 @@ public static IKernelBuilder AddElasticsearchVectorStore(this IKernelBuilder bui } /// - /// Register an Elasticsearch with the specified service ID and where is constructed using the provided settings. + /// Register an Elasticsearch with the specified service ID and where is constructed using the provided client settings. /// /// The builder to register the on. - /// The Elasticsearch client settings. + /// The Elasticsearch client settings. /// Optional options to further configure the . /// An optional service id to use as the service key. /// The kernel builder. - public static IKernelBuilder AddElasticsearchVectorStore(this IKernelBuilder builder, IElasticsearchClientSettings settings, ElasticsearchVectorStoreOptions? options = default, string? serviceId = default) + public static IKernelBuilder AddElasticsearchVectorStore(this IKernelBuilder builder, IElasticsearchClientSettings clientSettings, ElasticsearchVectorStoreOptions? options = default, string? serviceId = default) { - builder.Services.AddElasticsearchVectorStore(settings, options, serviceId); + builder.Services.AddElasticsearchVectorStore(clientSettings, options, serviceId); + return builder; + } + + /// + /// Register an Elasticsearch and with the specified service ID + /// and where the is retrieved from the dependency injection container. + /// + /// The type of the key. + /// The type of the record. + /// The builder to register the on. + /// The name of the collection. + /// Optional options to further configure the . + /// An optional service id to use as the service key. + /// The kernel builder. + public static IKernelBuilder AddElasticsearchVectorStoreRecordCollection( + this IKernelBuilder builder, + string collectionName, + ElasticsearchVectorStoreRecordCollectionOptions? options = default, + string? serviceId = default) + where TKey : notnull + { + builder.Services.AddElasticsearchVectorStoreRecordCollection(collectionName, options, serviceId); + return builder; + } + + /// + /// Register an Elasticsearch and with the specified service ID + /// and where the is constructed using the provided client settings. + /// + /// The type of the key. + /// The type of the record. + /// The builder to register the on. + /// The name of the collection. + /// The Elasticsearch client settings. + /// Optional options to further configure the . + /// An optional service id to use as the service key. + /// The kernel builder. + public static IKernelBuilder AddElasticsearchVectorStoreRecordCollection( + this IKernelBuilder builder, + string collectionName, + IElasticsearchClientSettings clientSettings, + ElasticsearchVectorStoreRecordCollectionOptions? options = default, + string? serviceId = default) + where TKey : notnull + { + builder.Services.AddElasticsearchVectorStoreRecordCollection(collectionName, clientSettings, options, serviceId); return builder; } } diff --git a/Elastic.SemanticKernel.Connectors.Elasticsearch/ElasticsearchServiceCollectionExtensions.cs b/Elastic.SemanticKernel.Connectors.Elasticsearch/ElasticsearchServiceCollectionExtensions.cs index 2d15608..046f621 100644 --- a/Elastic.SemanticKernel.Connectors.Elasticsearch/ElasticsearchServiceCollectionExtensions.cs +++ b/Elastic.SemanticKernel.Connectors.Elasticsearch/ElasticsearchServiceCollectionExtensions.cs @@ -29,7 +29,7 @@ public static IServiceCollection AddElasticsearchVectorStore(this IServiceCollec // cannot make assumptions about how ElasticsearchClient is being managed. services.AddKeyedTransient( serviceId, - (sp, obj) => + (sp, _) => { var elasticsearchClient = sp.GetRequiredService(); var selectedOptions = options ?? sp.GetService(); @@ -43,20 +43,20 @@ public static IServiceCollection AddElasticsearchVectorStore(this IServiceCollec } /// - /// Register an Elasticsearch with the specified service ID and where is constructed using the provided settings. + /// Register an Elasticsearch with the specified service ID and where is constructed using the provided client settings. /// /// The to register the on. - /// The Elasticsearch client settings. + /// The Elasticsearch client settings. /// Optional options to further configure the . /// An optional service id to use as the service key. /// The service collection. - public static IServiceCollection AddElasticsearchVectorStore(this IServiceCollection services, IElasticsearchClientSettings settings, ElasticsearchVectorStoreOptions? options = default, string? serviceId = default) + public static IServiceCollection AddElasticsearchVectorStore(this IServiceCollection services, IElasticsearchClientSettings clientSettings, ElasticsearchVectorStoreOptions? options = default, string? serviceId = default) { services.AddKeyedSingleton( serviceId, - (sp, obj) => + (sp, _) => { - var elasticsearchClient = new ElasticsearchClient(settings); + var elasticsearchClient = new ElasticsearchClient(clientSettings); var selectedOptions = options ?? sp.GetService(); return new ElasticsearchVectorStore( @@ -66,4 +66,90 @@ public static IServiceCollection AddElasticsearchVectorStore(this IServiceCollec return services; } + + /// + /// Register an Elasticsearch and with the specified service ID + /// and where the is retrieved from the dependency injection container. + /// + /// The type of the key. + /// The type of the record. + /// The to register the on. + /// The name of the collection. + /// Optional options to further configure the . + /// An optional service id to use as the service key. + /// Service collection. + public static IServiceCollection AddElasticsearchVectorStoreRecordCollection( + this IServiceCollection services, + string collectionName, + ElasticsearchVectorStoreRecordCollectionOptions? options = default, + string? serviceId = default) + where TKey : notnull + { + services.AddKeyedTransient>( + serviceId, + (sp, _) => + { + var elasticsearchClient = sp.GetRequiredService(); + var selectedOptions = options ?? sp.GetService>(); + + return (new ElasticsearchVectorStoreRecordCollection(elasticsearchClient, collectionName, selectedOptions) as IVectorStoreRecordCollection)!; + }); + + AddVectorizedSearch(services, serviceId); + + return services; + } + + /// + /// Register an Elasticsearch and with the specified service ID + /// and where the is constructed using the provided client settings. + /// + /// The type of the key. + /// The type of the record. + /// The to register the on. + /// The name of the collection. + /// The Elasticsearch client settings. + /// Optional options to further configure the . + /// An optional service id to use as the service key. + /// Service collection. + public static IServiceCollection AddElasticsearchVectorStoreRecordCollection( + this IServiceCollection services, + string collectionName, + IElasticsearchClientSettings clientSettings, + ElasticsearchVectorStoreRecordCollectionOptions? options = default, + string? serviceId = default) + where TKey : notnull + { + services.AddKeyedSingleton>( + serviceId, + (sp, _) => + { + var elasticsearchClient = new ElasticsearchClient(clientSettings); + var selectedOptions = options ?? sp.GetService>(); + + return (new ElasticsearchVectorStoreRecordCollection(elasticsearchClient, collectionName, selectedOptions) as IVectorStoreRecordCollection)!; + }); + + AddVectorizedSearch(services, serviceId); + + return services; + } + + /// + /// Also register the with the given as a . + /// + /// The type of the key. + /// The type of the data model that the collection should contain. + /// The service collection to register on. + /// The service id that the registrations should use. + private static void AddVectorizedSearch(IServiceCollection services, string? serviceId) + where TKey : notnull + { + services.AddKeyedTransient>( + serviceId, + (sp, _) => + { + return sp.GetRequiredKeyedService>(serviceId); + }); + } }