Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade elastic search dependencies #470

Merged
Merged
3 changes: 1 addition & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageVersion>
<PackageVersion Include="Elasticsearch.Net" Version="7.17.5" />
<PackageVersion Include="Elastic.Clients.Elasticsearch" Version="8.14.6" />
<PackageVersion Include="ElmahCore" Version="2.1.2" />
<PackageVersion Include="FluentAssertions" Version="6.12.0" />
<PackageVersion Include="JunitXml.TestLogger" Version="3.1.12" />
<PackageVersion Include="log4net" Version="2.0.17" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageVersion Include="NEST" Version="7.17.5" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="RabbitMQ.Client" Version="6.8.1" />
<PackageVersion Include="SonarAnalyzer.CSharp" Version="9.29.0.95321" />
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ Refer to [CONTRIBUTING.md](CONTRIBUTING.md) to learn how to contribute to this p
<table>
<tbody>
<tr>
<td align="center">
<a href="https://github.com/Mohammad-Haris">
<img src="https://avatars.githubusercontent.com/u/34305911?v=4" width="100;" alt="Mohammad-Haris"/>
<br />
<sub><b>Mohammad Haris</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/guibranco">
<img src="https://avatars.githubusercontent.com/u/3362854?v=4" width="100;" alt="guibranco"/>
Expand All @@ -139,13 +146,6 @@ Refer to [CONTRIBUTING.md](CONTRIBUTING.md) to learn how to contribute to this p
<sub><b>João Vítor Valadares</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/Mohammad-Haris">
<img src="https://avatars.githubusercontent.com/u/34305911?v=4" width="100;" alt="Mohammad-Haris"/>
<br />
<sub><b>Mohammad Haris</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/SimranGil">
<img src="https://avatars.githubusercontent.com/u/111714647?v=4" width="100;" alt="SimranGil"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\CrispyWaffle.Configuration\CrispyWaffle.Configuration.csproj" />
<ProjectReference Include="..\CrispyWaffle\CrispyWaffle.csproj" />
<PackageReference Include="Elastic.Clients.Elasticsearch" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Elasticsearch.Net" />
<PackageReference Include="NEST" />
<ProjectReference Include="..\CrispyWaffle.Configuration\CrispyWaffle.Configuration.csproj" />
<ProjectReference Include="..\CrispyWaffle\CrispyWaffle.csproj" />
</ItemGroup>

</Project>
64 changes: 26 additions & 38 deletions Src/CrispyWaffle.ElasticSearch/Helpers/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CrispyWaffle.Composition;
using CrispyWaffle.ElasticSearch.Utils.Communications;
using Nest;
using Elastic.Clients.Elasticsearch;
using Elastic.Clients.Elasticsearch.IndexManagement;

namespace CrispyWaffle.ElasticSearch.Helpers
{
Expand Down Expand Up @@ -31,7 +33,7 @@ public static string GetIndexName<T>()
is IndexNameAttribute[] attributes
&& attributes.Length == 1
? attributes[0].IndexName
: type.Name.ToLower().Replace(@" ", @"-");
: type.Name.ToLowerInvariant().Replace(@" ", @"-");
}

/// <summary>
Expand All @@ -42,84 +44,70 @@ is IndexNameAttribute[] attributes
/// <param name="alias">The alias.</param>
/// <param name="indexName">Name of the index.</param>
/// <returns>T.</returns>
public static T Alias<T>(this T index, string alias, string indexName = null)
public static async Task<T> AliasAsync<T>(this T index, string alias, string indexName = null)
where T : class, IIndexable, new()
{
if (string.IsNullOrWhiteSpace(indexName))
{
indexName = GetIndexName<T>();
}

_connector.Client.Indices.BulkAlias(a =>
a.Add(add => add.Index(indexName).Alias(alias))
await _connector.Client.Indices.UpdateAliasesAsync(aliases =>
aliases.Actions(
actions => actions.Add(
new AddAction() { Index = indexName, Alias = alias }
)
)
);
return index;
}

/// <summary>
/// Deletes the specified index.
/// </summary>
/// <typeparam name="T">The type to be indexed.</typeparam>
/// <param name="index">The index.</param>
/// <returns>T.</returns>
public static T Delete<T>(this T index)
where T : class, IIndexable, new()
{
var indexName = GetIndexName<T>();
if (_connector.Client.Indices.Exists(indexName).Exists)
{
_connector.Client.Indices.Delete(indexName);
}

return index;
}

/// <summary>
/// Automatics the map.
/// Deletes the specified index.
/// </summary>
/// <typeparam name="T">The type to be indexed.</typeparam>
/// <param name="index">The index.</param>
/// <returns>T.</returns>
public static T AutoMap<T>(this T index)
public static async Task<T> DeleteAsync<T>(this T index)
where T : class, IIndexable, new()
{
var indexName = GetIndexName<T>();
if (!_connector.Client.Indices.Exists(indexName).Exists)
if ((await _connector.Client.Indices.ExistsAsync(indexName)).Exists)
{
_connector.Client.Indices.Create(
indexName,
descriptor => descriptor.Map(ms => ms.AutoMap<T>())
);
await _connector.Client.Indices.DeleteAsync(indexName);
}

return index;
}

/// <summary>
/// Deletes the by query.
/// Deletes the document by query.
/// </summary>
/// <typeparam name="T">The type to be indexed.</typeparam>
/// <param name="index">The index.</param>
/// <param name="field">The field.</param>
/// <param name="indexPattern">The index pattern.</param>
/// <param name="daysBefore">The days before.</param>
/// <returns>System.Int64.</returns>
public static long DeleteByQuery<T>(
/// <returns>A task to be awaited on.</returns>
public static async Task<long?> DeleteByQueryAsync<T>(
this T index,
Expression<Func<T, object>> field,
string indexPattern,
int daysBefore
)
where T : class, new()
{
var result = _connector.Client.DeleteByQuery<T>(d =>
d.Index(indexPattern)
.Query(q =>
q.DateRange(g =>
g.Field(field).LessThan(DateMath.Now.Subtract($@"{daysBefore}d"))
var result = await _connector.Client.DeleteByQueryAsync<T>(
indexPattern,
d => d.Query(
q => q.Range(
r => r.DateRange(
dr => dr.Field(field).Lt(DateMath.Now.Subtract($@"{daysBefore}d"))
)
)
)
);
);

return result.Deleted;
}
Expand Down
74 changes: 27 additions & 47 deletions Src/CrispyWaffle.ElasticSearch/Log/ElasticSearchLogProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using CrispyWaffle.Log;
using CrispyWaffle.Log.Providers;
using CrispyWaffle.Serialization;
using Nest;
using Elastic.Clients.Elasticsearch;
using LogLevel = CrispyWaffle.Log.LogLevel;

namespace CrispyWaffle.ElasticSearch.Log
Expand All @@ -26,7 +26,7 @@ public class ElasticSearchLogProvider : ILogProvider, IDisposable
/// <summary>
/// The client.
/// </summary>
private readonly ElasticClient _client;
private readonly ElasticsearchClient _client;

/// <summary>
/// The index name.
Expand Down Expand Up @@ -86,20 +86,23 @@ private Task GarbageCollector()
{
if (_tokenSource.Token.IsCancellationRequested)
{
return null;
return Task.FromCanceled(_tokenSource.Token);
}

var retentionDays = _logRetentionDays;
_client.DeleteByQuery<LogMessage>(d =>
d.Index(_indexName)
.Query(q =>
q.DateRange(g =>
g.Field(f => f.Date)
.LessThan(DateMath.Now.Subtract($@"{retentionDays}d"))

_client.DeleteByQueryAsync<LogMessage>(
_indexName,
d => d.Query(
q => q.Range(
r => r.DateRange(
dr => dr.Field(f => f.Date).Lt(DateMath.Now.Subtract($@"{retentionDays}d"))
)
)
)
);
return null;

return Task.CompletedTask;
}

/// <summary>
Expand Down Expand Up @@ -166,9 +169,7 @@ public void Fatal(string category, string message)
return;
}

Task.Factory.StartNew(
() => _client.IndexDocument(Serialize(LogLevel.Fatal, category, message))
);
_client.IndexAsync(Serialize(LogLevel.Fatal, category, message));
}

/// <summary>
Expand All @@ -183,9 +184,7 @@ public void Error(string category, string message)
return;
}

Task.Factory.StartNew(
() => _client.IndexDocument(Serialize(LogLevel.Error, category, message))
);
_client.IndexAsync(Serialize(LogLevel.Error, category, message));
}

/// <summary>
Expand All @@ -200,9 +199,7 @@ public void Warning(string category, string message)
return;
}

Task.Factory.StartNew(
() => _client.IndexDocument(Serialize(LogLevel.Warning, category, message))
);
_client.IndexAsync(Serialize(LogLevel.Warning, category, message));
}

/// <summary>
Expand All @@ -217,9 +214,7 @@ public void Info(string category, string message)
return;
}

Task.Factory.StartNew(
() => _client.IndexDocument(Serialize(LogLevel.Info, category, message))
);
_client.IndexAsync(Serialize(LogLevel.Info, category, message));
}

/// <summary>
Expand All @@ -234,9 +229,7 @@ public void Trace(string category, string message)
return;
}

Task.Factory.StartNew(
() => _client.IndexDocument(Serialize(LogLevel.Trace, category, message))
);
_client.IndexAsync(Serialize(LogLevel.Trace, category, message));
}

/// <summary>
Expand All @@ -252,9 +245,8 @@ public void Trace(string category, string message, Exception exception)
return;
}

Task.Factory.StartNew(
() => _client.IndexDocument(Serialize(LogLevel.Trace, category, message))
);
_client.IndexAsync(Serialize(LogLevel.Trace, category, message));

Trace(category, exception);
}

Expand All @@ -263,6 +255,7 @@ public void Trace(string category, string message, Exception exception)
/// </summary>
/// <param name="category">The category.</param>
/// <param name="exception">The exception.</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1500:Braces for multi-line statements should not share line", Justification = "Enhances readability.")]
public void Trace(string category, Exception exception)
{
if (!_level.HasFlag(LogLevel.Trace))
Expand All @@ -273,12 +266,8 @@ public void Trace(string category, Exception exception)
do
{
var ex = exception;
Task.Factory.StartNew(() =>
{
_client.IndexDocument(Serialize(LogLevel.Trace, category, ex.Message));
_client.IndexDocument(Serialize(LogLevel.Trace, category, ex.StackTrace));
});

_client.IndexAsync(Serialize(LogLevel.Trace, category, ex.Message));
_client.IndexAsync(Serialize(LogLevel.Trace, category, ex.StackTrace));
exception = exception.InnerException;
} while (exception != null);
}
Expand All @@ -295,9 +284,7 @@ public void Debug(string category, string message)
return;
}

Task.Factory.StartNew(
() => _client.IndexDocument(Serialize(LogLevel.Debug, category, message))
);
_client.IndexAsync(Serialize(LogLevel.Debug, category, message));
}

/// <summary>
Expand All @@ -313,10 +300,7 @@ public void Debug(string category, string content, string identifier)
return;
}

Task.Factory.StartNew(
() =>
_client.IndexDocument(Serialize(LogLevel.Debug, category, content, identifier))
);
_client.IndexAsync(Serialize(LogLevel.Debug, category, content, identifier));
}

/// <summary>
Expand All @@ -327,6 +311,7 @@ public void Debug(string category, string content, string identifier)
/// <param name="content">The object to be serialized.</param>
/// <param name="identifier">The filename/attachment identifier (file name or key).</param>
/// <param name="customFormat">(Optional) the custom serializer format.</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Critical Code Smell", "S1006:Method overrides should not change parameter defaults", Justification = "Keeping it for now.")]
public void Debug<T>(
string category,
T content,
Expand All @@ -351,12 +336,7 @@ public void Debug<T>(
serialized = (string)content.GetCustomSerializer(customFormat);
}

Task.Factory.StartNew(
() =>
_client.IndexDocument(
Serialize(LogLevel.Debug, category, serialized, identifier)
)
);
_client.IndexAsync(Serialize(LogLevel.Debug, category, serialized, identifier));
}
}
}
Loading