Skip to content

Commit 1eeff02

Browse files
authored
Merge pull request #225 from umbraco/bugfix/v14/fix-index-properties-selection
Fix property selection on persisting index data
2 parents 0a1636d + 6420e1b commit 1eeff02

File tree

3 files changed

+64
-14
lines changed

3 files changed

+64
-14
lines changed

src/Umbraco.Cms.Integrations.Search.Algolia/Api/Management/Controllers/SaveIndexController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.AspNetCore.Http;
33
using Microsoft.AspNetCore.Mvc;
44
using System.Text.Json;
5+
using Umbraco.Cms.Integrations.Search.Algolia.Extensions;
56
using Umbraco.Cms.Integrations.Search.Algolia.Migrations;
67
using Umbraco.Cms.Integrations.Search.Algolia.Models;
78
using Umbraco.Cms.Integrations.Search.Algolia.Services;
@@ -31,8 +32,7 @@ public async Task<IActionResult> SaveIndex([FromBody] IndexConfiguration index)
3132
{
3233
Id = index.Id,
3334
Name = index.Name,
34-
SerializedData = JsonSerializer.Serialize(index.ContentData
35-
.Where(p => p.Selected && p.Properties.Any(q => q.Selected))),
35+
SerializedData = JsonSerializer.Serialize(index.ContentData.FilterByPropertySelected()),
3636
Date = DateTime.Now
3737
});
3838

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Umbraco.Cms.Integrations.Search.Algolia.Models.ContentTypeDtos;
2+
3+
namespace Umbraco.Cms.Integrations.Search.Algolia.Extensions
4+
{
5+
public static class ContentTypeExtensions
6+
{
7+
public static IEnumerable<ContentTypeDto> FilterByPropertySelected(this IEnumerable<ContentTypeDto> source)
8+
{
9+
foreach (var item in source)
10+
{
11+
if (!item.Selected) continue;
12+
13+
var contentType = item;
14+
15+
contentType.Properties = item.Properties.Where(x => x.Selected);
16+
17+
if (!contentType.Properties.Any()) continue;
18+
19+
yield return contentType;
20+
}
21+
}
22+
}
23+
}
Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
using Umbraco.Cms.Core.Models;
2-
using Umbraco.Cms.Core.Models.PublishedContent;
1+
using Microsoft.Extensions.Logging;
2+
using Umbraco.Cms.Core.Models;
33
using Umbraco.Cms.Core.PropertyEditors;
4+
using Umbraco.Cms.Core.Services;
45
using Umbraco.Cms.Integrations.Search.Algolia.Providers;
56

67
namespace Umbraco.Cms.Integrations.Search.Algolia.Services
@@ -11,13 +12,23 @@ public class AlgoliaSearchPropertyIndexValueFactory : IAlgoliaSearchPropertyInde
1112

1213
private readonly ConverterCollection _converterCollection;
1314

15+
private readonly IContentTypeService _contentTypeService;
16+
17+
private readonly ILogger<AlgoliaSearchPropertyIndexValueFactory> _logger;
18+
1419
public AlgoliaSearchPropertyIndexValueFactory(
1520
PropertyEditorCollection propertyEditorCollection,
16-
ConverterCollection converterCollection)
21+
ConverterCollection converterCollection,
22+
IContentTypeService contentTypeService,
23+
ILogger<AlgoliaSearchPropertyIndexValueFactory> logger)
1724
{
1825
_propertyEditorsCollection = propertyEditorCollection;
1926

2027
_converterCollection = converterCollection;
28+
29+
_contentTypeService = contentTypeService;
30+
31+
_logger = logger;
2132
}
2233

2334
public virtual KeyValuePair<string, object> GetValue(IProperty property, string culture)
@@ -28,20 +39,36 @@ public virtual KeyValuePair<string, object> GetValue(IProperty property, string
2839
return default;
2940
}
3041

31-
var indexValues = propertyEditor.PropertyIndexValueFactory.GetIndexValues(property, culture, null, true, Enumerable.Empty<string>(), null);
42+
Dictionary<Guid, IContentType> contentTypeDictionary = _contentTypeService.GetAll().ToDictionary(x => x.Key);
43+
44+
try
45+
{
46+
var indexValues = propertyEditor.PropertyIndexValueFactory.GetIndexValues(
47+
property,
48+
culture,
49+
null,
50+
true,
51+
Enumerable.Empty<string>(),
52+
contentTypeDictionary);
53+
54+
if (indexValues == null || !indexValues.Any()) return new KeyValuePair<string, object>(property.Alias, string.Empty);
3255

33-
if (indexValues == null || !indexValues.Any()) return new KeyValuePair<string, object>(property.Alias, string.Empty);
56+
var indexValue = indexValues.First();
3457

35-
var indexValue = indexValues.First();
58+
var converter = _converterCollection.FirstOrDefault(p => p.Name == property.PropertyType.PropertyEditorAlias);
59+
if (converter != null)
60+
{
61+
var result = converter.ParseIndexValues(property, indexValue.Value);
62+
return new KeyValuePair<string, object>(property.Alias, result);
63+
}
3664

37-
var converter = _converterCollection.FirstOrDefault(p => p.Name == property.PropertyType.PropertyEditorAlias);
38-
if (converter != null)
65+
return new KeyValuePair<string, object>(property.Alias, indexValue.Value);
66+
}
67+
catch (Exception ex)
3968
{
40-
var result = converter.ParseIndexValues(property, indexValue.Value);
41-
return new KeyValuePair<string, object>(property.Alias, result);
69+
_logger.LogError($"Failed to get values for {property.Alias}: {ex.Message}");
70+
return default;
4271
}
43-
44-
return new KeyValuePair<string, object>(property.Alias, indexValue.Value);
4572
}
4673
}
4774
}

0 commit comments

Comments
 (0)