Skip to content

Commit af102bf

Browse files
committed
Implement Algolia converter collection builder.
1 parent 9c9925b commit af102bf

17 files changed

+234
-148
lines changed

src/Umbraco.Cms.Integrations.Search.Algolia/AlgoliaComposer.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Umbraco.Cms.Core.Notifications;
88
using Umbraco.Cms.Integrations.Search.Algolia;
99
using Umbraco.Cms.Integrations.Search.Algolia.Configuration;
10+
using Umbraco.Cms.Integrations.Search.Algolia.Extensions;
1011
using Umbraco.Cms.Integrations.Search.Algolia.Handlers;
1112
using Umbraco.Cms.Integrations.Search.Algolia.Migrations;
1213
using Umbraco.Cms.Integrations.Search.Algolia.Models;
@@ -21,8 +22,8 @@ public void Compose(IUmbracoBuilder builder)
2122
builder.AddNotificationHandler<UmbracoApplicationStartingNotification, RunAlgoliaIndicesMigration>();
2223

2324
builder.AddNotificationAsyncHandler<ContentCacheRefresherNotification, AlgoliaContentCacheRefresherHandler>();
24-
25-
var options = builder.Services.AddOptions<AlgoliaSettings>()
25+
26+
_ = builder.Services.AddOptions<AlgoliaSettings>()
2627
.Bind(builder.Config.GetSection(Constants.SettingsPath));
2728

2829
builder.Services.AddSingleton<IAlgoliaIndexService, AlgoliaIndexService>();
@@ -32,6 +33,8 @@ public void Compose(IUmbracoBuilder builder)
3233
builder.Services.AddScoped<IAlgoliaIndexDefinitionStorage<AlgoliaIndex>, AlgoliaIndexDefinitionStorage>();
3334

3435
builder.Services.AddScoped<IAlgoliaSearchPropertyIndexValueFactory, AlgoliaSearchPropertyIndexValueFactory>();
36+
37+
builder.AddAlgoliaConverters();
3538
}
3639

3740
}

src/Umbraco.Cms.Integrations.Search.Algolia/Builders/ContentRecordBuilder.cs

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -26,56 +26,7 @@ public ContentRecordBuilder(IUserService userService, IPublishedUrlProvider urlP
2626

2727
_algoliaSearchPropertyIndexValueFactory = algoliaSearchPropertyIndexValueFactory;
2828
}
29-
public ContentRecordBuilder BuildFromContent(IPublishedContent content, Func<IPublishedProperty, bool> filter = null)
30-
{
31-
_record.ObjectID = content.Key.ToString();
32-
33-
_record.Id = content.Id;
34-
_record.Name = content.Name;
35-
36-
_record.CreateDate = content.CreateDate.ToString();
37-
_record.CreatorName = content.CreatorName();
38-
_record.UpdateDate = content.UpdateDate.ToString();
39-
_record.WriterName = content.WriterName();
40-
41-
_record.TemplateId = content.TemplateId.HasValue ? content.TemplateId.Value : -1;
42-
_record.Level = content.Level;
43-
_record.Path = content.Path;
44-
_record.ContentTypeAlias = content.ContentType.Alias;
45-
_record.Url = _urlProvider.GetUrl(content.Id);
46-
47-
if (content.Cultures.Any())
48-
{
49-
foreach (var culture in content.Cultures)
50-
{
51-
_record.Data.Add($"name{(!string.IsNullOrEmpty(culture.Value.Culture) ? "-" + culture.Value.Culture : "")}", culture.Value.Name);
52-
_record.Data.Add($"url{(!string.IsNullOrEmpty(culture.Value.Culture) ? "-" + culture.Value.Culture : "")}", content.Url(culture.Value.Culture));
53-
}
54-
}
5529

56-
foreach (var property in content.Properties.Where(filter ?? (p => true)))
57-
{
58-
if (!_record.Data.ContainsKey(property.Alias))
59-
{
60-
if (property.PropertyType.VariesByCulture())
61-
{
62-
foreach (var culture in content.Cultures)
63-
{
64-
var indexValue = _algoliaSearchPropertyIndexValueFactory.GetValue(property, culture.Value.Culture);
65-
_record.Data.Add($"{indexValue.Key}-{culture.Value.Culture}", indexValue.Value);
66-
}
67-
}
68-
else
69-
{
70-
var indexValue = _algoliaSearchPropertyIndexValueFactory.GetValue(property, string.Empty);
71-
_record.Data.Add(indexValue.Key, indexValue.Value);
72-
}
73-
74-
}
75-
}
76-
77-
return this;
78-
}
7930
public ContentRecordBuilder BuildFromContent(IContent content, Func<IProperty, bool> filter = null)
8031
{
8132
_record.ObjectID = content.Key.ToString();

src/Umbraco.Cms.Integrations.Search.Algolia/Controllers/SearchController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public async Task<IActionResult> BuildIndex([FromBody] IndexConfiguration indexC
130130
{
131131
using var ctx = _umbracoContextFactory.EnsureUmbracoContext();
132132
var contentType = ctx.UmbracoContext.Content.GetContentType(contentDataItem.ContentType.Alias);
133-
var contentItems = ctx.UmbracoContext.Content.GetByContentType(contentType);
133+
var contentItems = _contentService.GetPagedOfType(contentType.Id, 0, int.MaxValue, out _, null);
134134

135135
_logger.LogInformation("Building index for {ContentType} with {Count} items", contentDataItem.ContentType.Alias, contentItems.Count());
136136

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace Umbraco.Cms.Integrations.Search.Algolia.Converters
2+
{
3+
/// <summary>
4+
/// Defines a custom index converter.
5+
/// </summary>
6+
public interface IConverter
7+
{
8+
/// <summary>
9+
/// Gets the name of the converter.
10+
/// </summary>
11+
string Name { get; }
12+
13+
/// <summary>
14+
/// Parses the index value.
15+
/// </summary>
16+
object ParseIndexValue(KeyValuePair<string, IEnumerable<object>> indexValue);
17+
}
18+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace Umbraco.Cms.Integrations.Search.Algolia.Converters
2+
{
3+
public class UmbracoBooleanConverter : IConverter
4+
{
5+
public string Name => Core.Constants.PropertyEditors.Aliases.Boolean;
6+
7+
public object ParseIndexValue(KeyValuePair<string, IEnumerable<object>> indexValue)
8+
{
9+
if (indexValue.Value != null && indexValue.Value.Any())
10+
{
11+
var value = indexValue.Value.FirstOrDefault();
12+
13+
return value != null
14+
? value.Equals(1)
15+
: default;
16+
}
17+
18+
return default(bool);
19+
}
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace Umbraco.Cms.Integrations.Search.Algolia.Converters
2+
{
3+
public class UmbracoDecimalConverter : IConverter
4+
{
5+
public string Name => Core.Constants.PropertyEditors.Aliases.Decimal;
6+
7+
public object ParseIndexValue(KeyValuePair<string, IEnumerable<object>> indexValue)
8+
{
9+
if (indexValue.Value != null && indexValue.Value.Any())
10+
{
11+
var value = indexValue.Value.FirstOrDefault();
12+
13+
return value != null
14+
? decimal.Parse(value.ToString())
15+
: default;
16+
}
17+
18+
return default(decimal);
19+
}
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Windows.Markup;
2+
3+
namespace Umbraco.Cms.Integrations.Search.Algolia.Converters
4+
{
5+
public class UmbracoIntegerConverter : IConverter
6+
{
7+
public string Name => Core.Constants.PropertyEditors.Aliases.Integer;
8+
9+
public object ParseIndexValue(KeyValuePair<string, IEnumerable<object>> indexValue)
10+
{
11+
if (indexValue.Value != null && indexValue.Value.Any())
12+
{
13+
var value = indexValue.Value.FirstOrDefault();
14+
15+
return value ?? default(int);
16+
}
17+
18+
return default(int);
19+
}
20+
}
21+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System.Text.Json;
2+
using Umbraco.Cms.Core.Services;
3+
4+
namespace Umbraco.Cms.Integrations.Search.Algolia.Converters
5+
{
6+
public class UmbracoMediaPickerConverter : IConverter
7+
{
8+
private readonly IMediaService _mediaService;
9+
10+
public UmbracoMediaPickerConverter(IMediaService mediaService) => _mediaService = mediaService;
11+
12+
public string Name => Core.Constants.PropertyEditors.Aliases.MediaPicker3;
13+
14+
public object ParseIndexValue(KeyValuePair<string, IEnumerable<object>> indexValue)
15+
{
16+
var list = new List<string>();
17+
18+
var parsedIndexValue = ParseIndexValue(indexValue.Value);
19+
20+
if (string.IsNullOrEmpty(parsedIndexValue)) return list;
21+
22+
var inputMedia = JsonSerializer.Deserialize<IEnumerable<MediaItem>>(parsedIndexValue);
23+
24+
if (inputMedia == null) return string.Empty;
25+
26+
foreach (var item in inputMedia)
27+
{
28+
if (item == null) continue;
29+
30+
var mediaItem = _mediaService.GetById(Guid.Parse(item.MediaKey));
31+
32+
if (mediaItem == null) continue;
33+
34+
list.Add(mediaItem.GetValue("umbracoFile")?.ToString() ?? string.Empty);
35+
}
36+
37+
return list;
38+
}
39+
40+
private string ParseIndexValue(IEnumerable<object> values)
41+
{
42+
if (values != null && values.Any())
43+
{
44+
var value = values.FirstOrDefault();
45+
46+
if (value == null) return string.Empty;
47+
48+
return value.ToString();
49+
}
50+
51+
return string.Empty;
52+
}
53+
}
54+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Text.Json;
2+
3+
namespace Umbraco.Cms.Integrations.Search.Algolia.Converters
4+
{
5+
public class UmbracoTagsConverter : IConverter
6+
{
7+
public string Name => Core.Constants.PropertyEditors.Aliases.Tags;
8+
9+
public object ParseIndexValue(KeyValuePair<string, IEnumerable<object>> indexValue)
10+
{
11+
if (indexValue.Value != null && indexValue.Value.Any())
12+
{
13+
return indexValue.Value;
14+
}
15+
16+
return Enumerable.Empty<string>();
17+
}
18+
}
19+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Umbraco.Cms.Core.DependencyInjection;
2+
using Umbraco.Cms.Integrations.Search.Algolia.Converters;
3+
using Umbraco.Cms.Integrations.Search.Algolia.Providers;
4+
5+
namespace Umbraco.Cms.Integrations.Search.Algolia.Extensions
6+
{
7+
public static class UmbracoBuilderExtensions
8+
{
9+
public static IUmbracoBuilder AddAlgoliaConverters(this IUmbracoBuilder builder)
10+
{
11+
builder.AlgoliaConverters()
12+
.Append<UmbracoMediaPickerConverter>()
13+
.Append<UmbracoDecimalConverter>()
14+
.Append<UmbracoIntegerConverter>()
15+
.Append<UmbracoBooleanConverter>()
16+
.Append<UmbracoTagsConverter>();
17+
18+
return builder;
19+
}
20+
21+
public static ConverterCollectionBuilder AlgoliaConverters(this IUmbracoBuilder builder)
22+
=> builder.WithCollectionBuilder<ConverterCollectionBuilder>();
23+
}
24+
}

0 commit comments

Comments
 (0)