Posted on 12/09/2019 11:30:51
Hi Florin,
I've looked into your issue, and there is definitely something amiss here.
The major thing preventing you from using Dynamicweb Metatags in your queries and facets is that fields are serialized into one array field. This is not solvable without custom code. The issue is that you get one field where each metatag becomes one value in the format "<tag label> <tag value>". This means that you cannot make proper queries using this field and you cannot create facets for these tags either.
I'll add a task to our backlog to get this fixed, but until then you can create a custom project with a new SchemaExtender and IndexBuilderExtender to add the metadata in a format that can be used for querying and facetting. Maybe something like this:
using Dynamicweb.Content.Files;
using Dynamicweb.Content.Files.Metadata;
using Dynamicweb.Core;
using Dynamicweb.Indexing;
using Dynamicweb.Indexing.Schemas;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace ExtendedFilesIndexer
{
public class ExtendedFilesIndexBuilderExtender : IIndexBuilderExtender
{
public void ExtendDocument(IndexDocument indexDocument)
{
var fullPath = indexDocument?["DirectoryFullPath"]?.ToString();
var fields = Helper.GetMetaFields(fullPath);
if (!fields.Any())
return;
var fileFullPath = indexDocument?["FileFullName"]?.ToString();
var metadata = EditorFactory.GetMetadataForFile(fileFullPath);
if (metadata == null)
return;
foreach (var metafield in fields)
{
indexDocument[$"MyMetaTags|{metafield.Id}"] = metadata.GetValue(metafield.Id);
}
}
}
public class ExtendedFilesSchemaExtender : FileIndexSchemaExtender, IIndexSchemaExtender
{
public new IEnumerable GetFields()
{
var baseFields = new List(base.GetFields());
var fields = Helper.GetMetaFields(SystemInformation.MapPath("/Files"));
foreach (var field in fields)
{
baseFields.Add(new FieldDefinition
{
Name = field.Label,
SystemName = $"MyMetaTags|{field.Id}",
Source = $"MyMetaTags|{field.Id}",
Indexed = true,
Stored = true,
Analyzed = false, // This can be changed according the needs
TypeName = "System.String",
Group = "Metadata Tags",
IsFieldFromSchemaExtender = true
});
}
return baseFields;
}
}
internal static class Helper
{
public static List GetMetaFields(string path)
{
var fileDirectory = new DirectoryInfo(path);
var subdirectoriesAndSelf = GetSubDirectoriesAndSelf(fileDirectory);
var fields = new List();
foreach (var dir in subdirectoriesAndSelf)
{
var relativePath = dir.FullName.Replace(SystemInformation.MapPath("/Files"), "Files");
var metaFields = EditorFactory.LoadConfig(relativePath);
if (metaFields != null)
{
fields.AddRange(metaFields);
}
}
return fields;
}
private static List GetSubDirectoriesAndSelf(DirectoryInfo dir)
{
var list = new List();
FillSubDirectoriesAndSelf(dir, list);
return list;
}
private static void FillSubDirectoriesAndSelf(DirectoryInfo dir, List list)
{
list.Add(dir);
foreach (var subdir in dir.GetDirectories())
{
FillSubDirectoriesAndSelf(subdir, list);
}
}
}
}
I hope this helps you proceed.
- Jeppe