Skip to content

Commit

Permalink
Switched Elastic client from NEST to Elastic.Clients.Elasticsearch
Browse files Browse the repository at this point in the history
Bumped also other dependencies (OpenTelemetry, Marten, etc.)
  • Loading branch information
oskardudycz committed Mar 5, 2023
1 parent 1abd4e2 commit 2ee4a70
Show file tree
Hide file tree
Showing 101 changed files with 185 additions and 185 deletions.
10 changes: 5 additions & 5 deletions Core.ElasticSearch/Config.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Core.Configuration;
using Elastic.Clients.Elasticsearch;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Nest;

namespace Core.ElasticSearch;

Expand All @@ -16,17 +16,17 @@ public static class ElasticSearchConfigExtensions
private const string DefaultConfigKey = "ElasticSearch";

public static IServiceCollection AddElasticsearch(
this IServiceCollection services, IConfiguration configuration, Action<ConnectionSettings>? config = null)
this IServiceCollection services, IConfiguration configuration, Action<ElasticsearchClientSettings>? config = null)
{
var elasticSearchConfig = configuration.GetRequiredConfig<ElasticSearchConfig>(DefaultConfigKey);

var settings = new ConnectionSettings(new Uri(elasticSearchConfig.Url))
var settings = new ElasticsearchClientSettings(new Uri(elasticSearchConfig.Url))
.DefaultIndex(elasticSearchConfig.DefaultIndex);

config?.Invoke(settings);

var client = new ElasticClient(settings);
var client = new ElasticsearchClient(settings);

return services.AddSingleton<IElasticClient>(client);
return services.AddSingleton(client);
}
}
2 changes: 1 addition & 1 deletion Core.ElasticSearch/Core.ElasticSearch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


<ItemGroup>
<PackageReference Include="NEST" Version="7.17.5" />
<PackageReference Include="Elastic.Clients.Elasticsearch" Version="8.0.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.3" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
Expand Down
9 changes: 4 additions & 5 deletions Core.ElasticSearch/Projections/ElasticSearchProjection.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
using Core.ElasticSearch.Indices;
using Core.Events;
using Core.Projections;
using Elasticsearch.Net;
using Elastic.Clients.Elasticsearch;
using Microsoft.Extensions.DependencyInjection;
using Nest;

namespace Core.ElasticSearch.Projections;

public class ElasticSearchProjection<TEvent, TView> : IEventHandler<EventEnvelope<TEvent>>
where TView : class, IProjection
where TEvent : notnull
{
private readonly IElasticClient elasticClient;
private readonly ElasticsearchClient elasticClient;
private readonly Func<TEvent, string> getId;

public ElasticSearchProjection(
IElasticClient elasticClient,
ElasticsearchClient elasticClient,
Func<TEvent, string> getId
)
{
Expand Down Expand Up @@ -50,7 +49,7 @@ public static IServiceCollection Project<TEvent, TView>(this IServiceCollection
{
services.AddTransient<IEventHandler<EventEnvelope<TEvent>>>(sp =>
{
var session = sp.GetRequiredService<IElasticClient>();
var session = sp.GetRequiredService<ElasticsearchClient>();
return new ElasticSearchProjection<TEvent, TView>(session, getId);
});
Expand Down
44 changes: 22 additions & 22 deletions Core.ElasticSearch/Repository/ElasticSearchExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
using System.Collections.Concurrent;
using Nest;
using Elastic.Clients.Elasticsearch;

namespace Core.ElasticSearch.Repository;

public static class ElasticSearchRepository
{
public static async Task<T?> Find<T>(this ElasticsearchClient elasticClient, string id, CancellationToken ct)
where T : class =>
(await elasticClient.GetAsync<T>(id, cancellationToken: ct).ConfigureAwait(false))?.Source;

public static class ElasticSearchRepository
{
public static async Task<T?> Find<T>(this IElasticClient elasticClient, string id, CancellationToken ct)
where T: class =>
(await elasticClient.GetAsync<T>(id, ct: ct).ConfigureAwait(false))?.Source;

public static async Task Upsert<T>(this IElasticClient elasticClient, string id, T entity, CancellationToken ct)
where T: class =>
await elasticClient.UpdateAsync<T>(id,
u => u.Doc(entity).Upsert(entity).Index(ToIndexName<T>()),
ct
).ConfigureAwait(false);
public static async Task Upsert<T>(this ElasticsearchClient elasticClient, string id, T entity,
CancellationToken ct)
where T : class =>
await elasticClient.UpdateAsync<T, object>(ToIndexName<T>(), id,
u => u.Doc(entity).Upsert(entity),
ct
).ConfigureAwait(false);

private static readonly ConcurrentDictionary<Type, string> TypeNameMap = new();
private static readonly ConcurrentDictionary<Type, string> TypeNameMap = new();

private static string ToIndexName<TIndex>()
private static string ToIndexName<TIndex>()
{
var indexType = typeof(TIndex);
return TypeNameMap.GetOrAdd(indexType, _ =>
{
var indexType = typeof(TIndex);
return TypeNameMap.GetOrAdd(indexType, _ =>
{
var modulePrefix = indexType.Namespace!.Split(".").First();
return $"{modulePrefix}-{indexType.Name}".ToLower();
});
}
var modulePrefix = indexType.Namespace!.Split(".").First();
return $"{modulePrefix}-{indexType.Name}".ToLower();
});
}
}
12 changes: 6 additions & 6 deletions Core.ElasticSearch/Repository/ElasticSearchRepository.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Core.ElasticSearch.Indices;
using Nest;
using Elastic.Clients.Elasticsearch;
using IAggregate = Core.Aggregates.IAggregate;

namespace Core.ElasticSearch.Repository;
Expand All @@ -15,18 +15,18 @@ namespace Core.ElasticSearch.Repository;

public class ElasticSearchRepository<T>: IElasticSearchRepository<T> where T : class, IAggregate, new()
{
private readonly IElasticClient elasticClient;
private readonly ElasticsearchClient elasticClient;

public ElasticSearchRepository(
IElasticClient elasticClient
ElasticsearchClient elasticClient
)
{
this.elasticClient = elasticClient ?? throw new ArgumentNullException(nameof(elasticClient));
}

public async Task<T?> Find(Guid id, CancellationToken cancellationToken)
{
var response = await elasticClient.GetAsync<T>(id, ct: cancellationToken).ConfigureAwait(false);
var response = await elasticClient.GetAsync<T>(id, cancellationToken).ConfigureAwait(false);
return response?.Source;
}

Expand All @@ -37,11 +37,11 @@ public Task Add(T aggregate, CancellationToken cancellationToken)

public Task Update(T aggregate, CancellationToken cancellationToken)
{
return elasticClient.UpdateAsync<T>(aggregate.Id, i => i.Doc(aggregate).Index(IndexNameMapper.ToIndexName<T>()), cancellationToken);
return elasticClient.UpdateAsync<T, object>(IndexNameMapper.ToIndexName<T>(), aggregate.Id, i => i.Doc(aggregate), cancellationToken);
}

public Task Delete(T aggregate, CancellationToken cancellationToken)
{
return elasticClient.DeleteAsync<T>(aggregate.Id, ct: cancellationToken);
return elasticClient.DeleteAsync<T>(aggregate.Id, cancellationToken);
}
}
2 changes: 1 addition & 1 deletion Core.EventStoreDB.Tests/Core.EventStoreDB.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.10.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
Expand Down
2 changes: 1 addition & 1 deletion Core.Kafka.Tests/Core.Kafka.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.10.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
Expand Down
3 changes: 1 addition & 2 deletions Core.Marten/Core.Marten.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Marten" Version="6.0.0-alpha.4" />
<PackageReference Include="MediatR" Version="12.0.0" />
<PackageReference Include="Marten" Version="6.0.0-alpha.9" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.3" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
Expand Down
2 changes: 2 additions & 0 deletions Core.Marten/Subscriptions/MartenSubscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ CancellationToken ct
throw;
}
}

public bool EnableDocumentTrackingDuringRebuilds { get; set; }
}

public interface IMartenEventsConsumer
Expand Down
4 changes: 2 additions & 2 deletions Core.Tests/Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.10.0" />
<PackageReference Include="Marten" Version="6.0.0-alpha.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="Marten" Version="6.0.0-alpha.9" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
Expand Down
4 changes: 2 additions & 2 deletions Core/Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="Polly" Version="7.2.3" />
<PackageReference Include="RestSharp" Version="108.0.3" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.0.0-rc9.9" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.4.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc9.9" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.0.0-rc9.9" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.3.2" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.4.0" />
<PackageReference Include="Scrutor" Version="4.2.1" />
</ItemGroup>

Expand Down
3 changes: 2 additions & 1 deletion Core/OpenTelemetry/TelemetryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ OpenTelemetryOptions options
Activity.DefaultIdFormat = ActivityIdFormat.W3C;

services
.AddOpenTelemetryTracing(builder =>
.AddOpenTelemetry()
.WithTracing(builder =>
{
options.ConfigureTracerProvider(builder
.AddSource(ActivitySourceProvider.DefaultSourceName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.10.0" />
<PackageReference Include="Marten" Version="6.0.0-alpha.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="Marten" Version="6.0.0-alpha.9" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
Expand Down
2 changes: 1 addition & 1 deletion Marten.Integration.Tests/CompositeIds/CompositeIdsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ protected override IDocumentSession CreateSession(Action<StoreOptions>? setStore
options.Projections.SelfAggregate<Reservation>();
});

return store.OpenSession();
return store.LightweightSession();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected override IDocumentSession CreateSession(Action<StoreOptions>? setStore
options.Projections.SelfAggregate<IssuesList>();
});

return store.OpenSession();
return store.LightweightSession();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected override IDocumentSession CreateSession(Action<StoreOptions>? setStore
options.Projections.Add(new IssueDescriptionsProjection());
});

return store.OpenSession();
return store.LightweightSession();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void GivenExistingStreamWithOneEvent_WhenStreamIsLoadedByEventType_ThenIt

//Then
@event.Should().NotBeNull();
@event.Id.Should().Be(eventId);
@event!.Id.Should().Be(eventId);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void GivenOneEvent_WhenEventsArePublishedWithStreamId_ThenEventsAreSavedW
var streamState = EventStore.FetchStreamState(streamId);

streamState.Should().NotBeNull();
streamState.Version.Should().Be(1);
streamState!.Version.Should().Be(1);
}

[Fact]
Expand Down
10 changes: 4 additions & 6 deletions Marten.Integration.Tests/General/StoreInitializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void GivenProperConnectionString_WhenDocumentSessionIsCreatedAndTransacti
options.Events.DatabaseSchemaName = Settings.SchemaName;
});
using var session = store.OpenSession();
using var session = store.LightweightSession();
using var transaction = session.Connection!.BeginTransaction();
ConnectionShouldBeEstablished(store);
Expand All @@ -93,11 +93,9 @@ public void GivenProperConnectionString_WhenDocumentSessionIsCreatedAndTransacti

private static void ConnectionShouldBeEstablished(IDocumentStore store)
{
using (var session = store.OpenSession())
{
var result = session.Query<int>("SELECT 1");
using var session = store.LightweightSession();
var result = session.Query<int>("SELECT 1");

result.Should().NotBeNull();
}
result.Should().NotBeNull();
}
}
4 changes: 2 additions & 2 deletions Marten.Integration.Tests/Marten.Integration.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.10.0" />
<PackageReference Include="Marten" Version="6.0.0-alpha.4" />
<PackageReference Include="Marten" Version="6.0.0-alpha.9" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ public static IDocumentSession Get(string? schemaName = null, Action<StoreOption
{
var store = DocumentStoreProvider.Get(schemaName, setOptions);

return store.OpenSession();
return store.LightweightSession();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="NSubstitute" Version="5.0.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="7.0.3" />
Expand Down
2 changes: 1 addition & 1 deletion Sample/ECommerce/Carts/Carts.Api/Carts.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.3" />
<PackageReference Include="OpenTelemetry.Exporter.Jaeger" Version="1.4.0-beta.3" />
<PackageReference Include="OpenTelemetry.Exporter.Jaeger" Version="1.4.0" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Sample/ECommerce/Carts/Carts.Tests/Carts.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.10.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="NSubstitute" Version="5.0.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="NSubstitute" Version="5.0.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="7.0.3" />
Expand Down
2 changes: 1 addition & 1 deletion Sample/ECommerce/Orders/Orders.Api/Orders.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<ItemGroup>
<PackageReference Include="Npgsql.OpenTelemetry" Version="7.0.2" />
<PackageReference Include="OpenTelemetry.Exporter.Jaeger" Version="1.4.0-beta.3" />
<PackageReference Include="OpenTelemetry.Exporter.Jaeger" Version="1.4.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.3">
Expand Down
2 changes: 1 addition & 1 deletion Sample/ECommerce/Orders/Orders.Tests/Orders.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.10.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="NSubstitute" Version="5.0.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
Expand Down
Loading

0 comments on commit 2ee4a70

Please sign in to comment.