在 Sitecore 中,如何在派生的 SearchResultItem 类的 [IndexField] 属性以外的属性上定义索引字段名称?
我正在尝试使用与 Glass.Mapper 模型定义相同的接口,并且它已经包含定义 Sitecore 字段名称(以及索引字段名称)的属性的 [SitecoreField] 属性。
谢谢!
在 Sitecore 中,如何在派生的 SearchResultItem 类的 [IndexField] 属性以外的属性上定义索引字段名称?
我正在尝试使用与 Glass.Mapper 模型定义相同的接口,并且它已经包含定义 Sitecore 字段名称(以及索引字段名称)的属性的 [SitecoreField] 属性。
谢谢!
我相信您可以更改 Sitecore Glass Mapper SitecoreFieldAttribute 来实现这一点。
IIndexFieldNameFormatterAttribute
您可以在 Glass Mapper 上实现接口SitecoreFieldAttribute
。
这个界面你会在 中找到,Sitecore.ContentSearch.Linq.dll
它看起来像这样:
namespace Sitecore.ContentSearch
{
public interface IIndexFieldNameFormatterAttribute : _Attribute
{
string GetIndexFieldName(string fieldName);
string GetTypeFieldName(string fieldName);
}
}
你的实现是这样的,我在这里只粘贴了接口方法。
namespace Glass.Sitecore.Mapper.Configuration.Attributes
{
/// <summary>
/// Used to populate the property with data from a Sitecore field
/// </summary>
public class SitecoreFieldAttribute: AbstractSitecorePropertyAttribute, IIndexFieldNameFormatterAttribute
{
public string GetIndexFieldName(string fieldName)
{
return this.FieldName;
}
public string GetTypeFieldName(string fieldName)
{
return fieldName;
}
我还没有测试它,但我可以看到 Sitecore Linq 依靠这个接口来查找字段名称。您可以自己进行调查,但这是让我推断出的一段代码:
var variable = (from p in (IEnumerable<PropertyInfo>)typeof(TItem).GetProperties()
select new { Property = p, Attribute = (IIndexFieldNameFormatterAttribute)p.GetCustomAttributes().FirstOrDefault<Attribute>((Attribute a) => a is IIndexFieldNameFormatterAttribute) }).FirstOrDefault((p) => p.Attribute != null);
if (variable != null && variable.Attribute.GetIndexFieldName(variable.Property.Name) == this.FieldName)
{
property = variable.Property;
}
希望能帮助到你..
我认为不可能,因为' IndexField '和' SitecoreField '代表不同的东西,' IndexField '属性设置索引字段的名称,例如' IndexField("_id") ',' IndexField("_language" ) ' 表示 lucene 文档中的 id 和语言字段名称。
现在,Sitecore 默认情况下,所有字段的索引名称都以它的名字存储在 Sitecore 中,例如,一个名为 ' Content ' 的字段将默认存储在 lucene 文档中作为 ' content '。如果需要,您可以更改为其他内容。
SitecoreField属性表示项目字段的实际名称,因此 glass 可以将该字段值映射到属性中。
最重要的是,您只需要在类中的每个属性上指定 IndexField 和 SitecoreField,因为每个属性的工作方式不同