Skip to content

Commit

Permalink
Replace Nest package with Elastic.Clients.Elasticsearch
Browse files Browse the repository at this point in the history
Fix #17021
  • Loading branch information
MikeAlhayek committed Nov 15, 2024
1 parent 05bd65a commit 80ed984
Show file tree
Hide file tree
Showing 20 changed files with 350 additions and 515 deletions.
9 changes: 1 addition & 8 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<PackageVersion Include="BenchmarkDotNet" Version="0.14.0" />
<PackageVersion Include="Castle.Core" Version="5.1.1" />
<PackageVersion Include="DocumentFormat.OpenXml" Version="3.1.0" />
<PackageVersion Include="Elastic.Clients.Elasticsearch" Version="8.15.10" />
<PackageVersion Include="Fluid.Core" Version="2.12.0" />
<PackageVersion Include="GraphQL" Version="7.9.0" />
<PackageVersion Include="GraphQL.DataLoader" Version="7.9.0" />
Expand Down Expand Up @@ -54,7 +55,6 @@
<PackageVersion Include="MiniProfiler.AspNetCore.Mvc" Version="4.3.8" />
<PackageVersion Include="Moq" Version="4.20.72" />
<PackageVersion Include="ncrontab" Version="3.3.3" />
<PackageVersion Include="NEST" Version="7.17.5" />
<PackageVersion Include="NJsonSchema" Version="11.0.2" />
<PackageVersion Include="NLog.Web.AspNetCore" Version="5.3.14" />
<PackageVersion Include="NodaTime" Version="3.2.0" />
Expand Down Expand Up @@ -84,15 +84,13 @@
<PackageVersion Include="YesSql.Filters.Abstractions" Version="5.1.1" />
<PackageVersion Include="YesSql.Filters.Query" Version="5.1.1" />
<PackageVersion Include="ZString" Version="2.6.0" />

<!-- dotnet/extensions -->
<PackageVersion Include="Microsoft.Extensions.Http.Resilience" Version="8.10.0" />
</ItemGroup>
<ItemGroup>
<GlobalPackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<GlobalPackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeStyle" Version="4.11.0" />
</ItemGroup>

<!-- These versions are used for tansitive dependency forced upgrades only. E.g. when a package references a vulnerable version. -->
<!-- To list all vulnerable direct references run 'dotnet list package -vulnerable' (use double dash, just XML comments can't contain it) -->
<!-- To list all vulnerable transitive references run 'dotnet list package -vulnerable -include-transitive' (use double dash, just XML comments can't contain it) -->
Expand Down Expand Up @@ -123,24 +121,19 @@
└─ System.IO.Packaging (v8.0.0)
-->
<PackageVersion Include="System.IO.Packaging" Version="8.0.1" />

<!--
YesSql (v5.1.1)
└─ YesSql.Provider.SqlServer (v5.1.1)
└─ Microsoft.Data.SqlClient (v5.2.1)
└─ System.Runtime.Caching (v8.0.0)
-->
<PackageVersion Include="System.Runtime.Caching" Version="8.0.1" />

<PackageVersion Include="System.Private.Uri" Version="4.3.2" />

<!-- Several transitive dependencies on 8.0.4 and lower which has known vulnerabilities -->
<PackageVersion Include="System.Text.Json" Version="8.0.5" />

<!-- Several transitive dependencies on 8.0.0 which has known vulnerabilities -->
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="8.0.1" />
</ItemGroup>

<!-- These versions are used for the NuGet packages that are dependent on the current TFM -->
<!-- Versions are preset for the default TFM (there may be no TFM in an evaluation phase) -->
<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using Elastic.Clients.Elasticsearch;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Options;
using Nest;
using OrchardCore.DisplayManagement.Entities;
using OrchardCore.DisplayManagement.Handlers;
using OrchardCore.DisplayManagement.Views;
Expand All @@ -25,7 +25,7 @@ public sealed class ElasticSettingsDisplayDriver : SiteDisplayDriver<ElasticSett
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IAuthorizationService _authorizationService;
private readonly ElasticConnectionOptions _elasticConnectionOptions;
private readonly IElasticClient _elasticClient;
private readonly ElasticsearchClient _elasticClient;

internal readonly IStringLocalizer S;

Expand All @@ -37,7 +37,7 @@ public ElasticSettingsDisplayDriver(
IHttpContextAccessor httpContextAccessor,
IAuthorizationService authorizationService,
IOptions<ElasticConnectionOptions> elasticConnectionOptions,
IElasticClient elasticClient,
ElasticsearchClient elasticClient,
IStringLocalizer<ElasticSettingsDisplayDriver> stringLocalizer
)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Elastic.Clients.Elasticsearch" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\OrchardCore\OrchardCore.Admin.Abstractions\OrchardCore.Admin.Abstractions.csproj" />
<ProjectReference Include="..\..\OrchardCore\OrchardCore.Module.Targets\OrchardCore.Module.Targets.csproj" />
Expand All @@ -26,8 +30,4 @@
<ProjectReference Include="..\..\OrchardCore\OrchardCore.Search.Elasticsearch.Core\OrchardCore.Search.Elasticsearch.Core.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="NEST" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System.Text;
using System.Text.Encodings.Web;
using Elastic.Clients.Elasticsearch;
using Elastic.Clients.Elasticsearch.QueryDsl;
using Fluid.Values;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Nest;
using OrchardCore.Liquid;
using OrchardCore.Search.Abstractions;
using OrchardCore.Search.Elasticsearch.Core.Models;
Expand All @@ -19,8 +20,8 @@ public class ElasticsearchService : ISearchService
private readonly ISiteService _siteService;
private readonly ElasticIndexManager _elasticIndexManager;
private readonly ElasticIndexSettingsService _elasticIndexSettingsService;
private readonly IElasticSearchQueryService _elasticsearchQueryService;
private readonly IElasticClient _elasticClient;
private readonly ElasticSearchQueryService _elasticsearchQueryService;
private readonly ElasticsearchClient _elasticClient;
private readonly JavaScriptEncoder _javaScriptEncoder;
private readonly ElasticConnectionOptions _elasticConnectionOptions;
private readonly ILiquidTemplateManager _liquidTemplateManager;
Expand All @@ -30,8 +31,8 @@ public ElasticsearchService(
ISiteService siteService,
ElasticIndexManager elasticIndexManager,
ElasticIndexSettingsService elasticIndexSettingsService,
IElasticSearchQueryService elasticsearchQueryService,
IElasticClient elasticClient,
ElasticSearchQueryService elasticsearchQueryService,
ElasticsearchClient elasticClient,
JavaScriptEncoder javaScriptEncoder,
IOptions<ElasticConnectionOptions> elasticConnectionOptions,
ILiquidTemplateManager liquidTemplateManager,
Expand Down Expand Up @@ -86,7 +87,7 @@ public async Task<SearchResult> SearchAsync(string indexName, string term, int s
try
{
var searchType = searchSettings.GetSearchType();
QueryContainer query = null;
Query query = null;

if (searchType == ElasticSettings.CustomSearchType && !string.IsNullOrWhiteSpace(searchSettings.DefaultQuery))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Nest;
using OrchardCore.BackgroundTasks;
using OrchardCore.ContentManagement;
using OrchardCore.ContentTypes.Editors;
Expand Down Expand Up @@ -40,11 +39,11 @@ public override void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IConfigureOptions<ElasticConnectionOptions>, ElasticConnectionOptionsConfigurations>();

services.AddSingleton<IElasticClient>((sp) =>
services.AddSingleton((sp) =>
{
var options = sp.GetRequiredService<IOptions<ElasticConnectionOptions>>().Value;
return new ElasticClient(options.GetConnectionSettings() ?? new ConnectionSettings());
return ElasticSearchClientFactory.Create(options);
});

services.Configure<ElasticsearchOptions>(options =>
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="NEST" />
<PackageReference Include="Elastic.Clients.Elasticsearch" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
using Nest;

namespace OrchardCore.Search.Elasticsearch.Core.Mappings;

internal sealed class ContainedPartModel
{
[Keyword(Name = "Ids")]
public string Ids { get; set; }

[Number(Name = "Order")]
public string Order { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
using Nest;

namespace OrchardCore.Search.Elasticsearch.Core.Mappings;

internal sealed class DisplayTextModel
{
[Text(Name = "Analyzed")]
public string Analyzed { get; set; }

[Keyword(Name = "Normalized")]
public string Normalized { get; set; }

[Keyword(Name = "keyword")]
public string Keyword { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Nest;

namespace OrchardCore.Search.Elasticsearch.Core.Models;

public class ElasticConnectionOptions
Expand Down Expand Up @@ -39,27 +37,14 @@ public class ElasticConnectionOptions
/// </summary>
public string CertificateFingerprint { get; set; }

/// <summary>
/// Enables compatibility mode for Elasticsearch 8.x.
/// </summary>
public bool EnableApiVersioningHeader { get; set; }

/// <summary>
/// Whether the configuration section exists.
/// </summary>
private bool _fileConfigurationExists { get; set; }

private IConnectionSettingsValues _conntectionSettings;

public void SetFileConfigurationExists(bool fileConfigurationExists)
=> _fileConfigurationExists = fileConfigurationExists;

public bool FileConfigurationExists()
=> _fileConfigurationExists;

public void SetConnectionSettings(IConnectionSettingsValues settings)
=> _conntectionSettings = settings;

public IConnectionSettingsValues GetConnectionSettings()
=> _conntectionSettings;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="NEST" />
<PackageReference Include="Elastic.Clients.Elasticsearch" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Elastic.Clients.Elasticsearch;
using Elastic.Clients.Elasticsearch.QueryDsl;
using Microsoft.Extensions.Options;
using Nest;
using OrchardCore.ContentManagement;
using OrchardCore.Environment.Shell;
using OrchardCore.Search.Elasticsearch.Core.Models;
Expand All @@ -10,15 +11,12 @@ namespace OrchardCore.Search.Elasticsearch.Core.Providers;
public class ElasticContentPickerResultProvider : IContentPickerResultProvider
{
private readonly ElasticIndexManager _elasticIndexManager;
private readonly string _indexPrefix;
private readonly ElasticConnectionOptions _elasticConnectionOptions;

public ElasticContentPickerResultProvider(
ShellSettings shellSettings,
IOptions<ElasticConnectionOptions> elasticConnectionOptions,
ElasticIndexManager elasticIndexManager)
{
_indexPrefix = shellSettings.Name.ToLowerInvariant() + "_";
_elasticConnectionOptions = elasticConnectionOptions.Value;
_elasticIndexManager = elasticIndexManager;
}
Expand Down Expand Up @@ -50,19 +48,19 @@ public async Task<IEnumerable<ContentPickerResult>> Search(ContentPickerSearchCo

await _elasticIndexManager.SearchAsync(indexName, async elasticClient =>
{
ISearchResponse<Dictionary<string, object>> searchResponse = null;
SearchResponse<Dictionary<string, object>> searchResponse = null;
var elasticTopDocs = new ElasticTopDocs();
if (string.IsNullOrWhiteSpace(searchContext.Query))
{
searchResponse = await elasticClient.SearchAsync<Dictionary<string, object>>(s => s
.Index(_indexPrefix + indexName)
.Index(_elasticIndexManager.GetFullIndexName(indexName))
.Query(q => q
.Bool(b => b
.Filter(f => f
.Terms(t => t
.Field("Content.ContentItem.ContentType")
.Terms(searchContext.ContentTypes.ToArray())
.Term(new TermsQueryField(searchContext.ContentTypes.Select(contentType => FieldValue.String(contentType)).ToArray()))
)
)
)
Expand All @@ -72,13 +70,13 @@ await _elasticIndexManager.SearchAsync(indexName, async elasticClient =>
else
{
searchResponse = await elasticClient.SearchAsync<Dictionary<string, object>>(s => s
.Index(_indexPrefix + indexName)
.Index(_elasticIndexManager.GetFullIndexName(indexName))
.Query(q => q
.Bool(b => b
.Filter(f => f
.Terms(t => t
.Field("Content.ContentItem.ContentType")
.Terms(searchContext.ContentTypes.ToArray())
.Term(new TermsQueryField(searchContext.ContentTypes.Select(contentType => FieldValue.String(contentType)).ToArray()))
)
)
.Should(s => s
Expand All @@ -92,7 +90,7 @@ await _elasticIndexManager.SearchAsync(indexName, async elasticClient =>
);
}
if (searchResponse.IsValid)
if (searchResponse.IsValidResponse)
{
elasticTopDocs.TopDocs = searchResponse.Documents.ToList();
}
Expand All @@ -105,7 +103,7 @@ await _elasticIndexManager.SearchAsync(indexName, async elasticClient =>
{
ContentItemId = doc["ContentItemId"].ToString(),
DisplayText = doc["Content.ContentItem.DisplayText.keyword"].ToString(),
HasPublished = doc["Content.ContentItem.Published"].ToString().ToLowerInvariant().Equals("true", StringComparison.Ordinal)
HasPublished = doc["Content.ContentItem.Published"].ToString().Equals("true", StringComparison.OrdinalIgnoreCase)
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static IServiceCollection AddElasticServices(this IServiceCollection serv
services.AddSingleton<ElasticIndexManager>();
services.AddScoped<ElasticIndexingService>();
services.AddScoped<IModularTenantEvents, ElasticIndexInitializerService>();
services.AddScoped<IElasticSearchQueryService, ElasticSearchQueryService>();
services.AddScoped<ElasticSearchQueryService>();
services.AddScoped<IElasticQueryService, ElasticQueryService>();
services.AddScoped<IContentHandler, ElasticIndexingContentHandler>();

Expand Down

This file was deleted.

Loading

0 comments on commit 80ed984

Please sign in to comment.