I want to filter some event nodes based on region via Delivery API.
We have done similar features for categories on nodes using MTNP.
However in this specific use-case an event has a location picked using MTNP and region itself is parent node of this.
I noticed the nodes in DeliveryContentIndex already as parentId
and ancestorIds
fields, which could be used.
I wonder what best a best practise to handle this?
We could lookup of content from location
property on event node and index a field region
on event.
E.g. the filter may look something like this:
public class RegionFilter : IFilterHandler //, IContentIndexHandler
{
private const string FilterOptionSpecifier = "region:";
private const string IndexFieldName = "parentId";
private const string ContentFieldName = nameof(Event.Location);
// Querying
public bool CanHandle(string query)
=> query.StartsWith(FilterOptionSpecifier, StringComparison.OrdinalIgnoreCase);
public FilterOption BuildFilterOption(string filter)
{
var fieldValue = filter.Substring(FilterOptionSpecifier.Length);
var values = fieldValue.Split(',');
return new FilterOption
{
FieldName = IndexFieldName,
Values = values,
// use exact match
Operator = FilterOperation.Is
};
}
// Indexing
/*public IEnumerable<IndexFieldValue> GetFieldValues(IContent content, string? culture)
{
var fieldValue = content.GetValue<string?>(ContentFieldName);
if (fieldValue is null)
{
return Array.Empty<IndexFieldValue>();
}
var keys = fieldValue.Split(',').Select(udi => new GuidUdi(new Uri(udi)).Guid).ToArray();
return new[]
{
new IndexFieldValue
{
FieldName = IndexFieldName,
// index multiple values instead of one string value
Values = keys.OfType<object>()
}
};
}*/
public IEnumerable<IndexField> GetFields() => new[]
{
new IndexField
{
FieldName = IndexFieldName,
FieldType = FieldType.StringRaw,
VariesByCulture = false
}
};
}
But since the parentId
(region id), we want to search/filter is not directly on event, I guess best practise would be to index a new field on event node for picked location.
Regarding indexing I guess we can just use GetFieldValues()
here and doesn’t need to use TransformingIndexValues
on DeliveryApiContentIndex
like this:
public void Initialize()
{
if (!_examineManager.TryGetIndex(UmbracoIndexes.DeliveryApiContentIndexName, out IIndex index))
{
throw new InvalidOperationException($"No index found by name {UmbracoIndexes.DeliveryApiContentIndexName}");
}
index.TransformingIndexValues += UmbracoContextIndex_TransformingIndexValues;
}