Linq lucene的d映射器,该映射器将从索引中的Guid转换为玻璃模型。我破解了这个,但还没有测试: public class IndexFieldDateTimeValueConverter : TypeConverter { public Type RequestedType { get; set; } public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { var config = Glass.Mapper.Context.Default.GetTypeConfiguration<SitecoreTypeConfiguration>(sourceType, true); if (config != null) { RequestedType = sourceType; return true; } else return base.CanConvertFrom(context, sourceType); } public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if (destinationType == typeof(string)) return true; else return base.CanConvertTo(context, destinationType); } public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { var scContext = new SitecoreContext(); return scContext.CreateType(RequestedType, scContext.Database.GetItem(value.ToString()),true, false, new Dictionary<string, object>()); } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { var config = Glass.Mapper.Context.Default.GetTypeConfiguration<SitecoreTypeConfiguration>(value.GetType(), true); ID id =config.GetId(value); return id.ToShortID().ToString().ToLowerInvariant(); } 公共类IndexFieldDateTimeValueConverter:TypeConverter { 公共类型RequestedType{get;set;} 公共覆盖布尔CanConvertFrom(ITypeScriptorContext上下文,类型sourceType) { var config=Glass.Mapper.Context.Default.GetTypeConfiguration(sourceType,true); 如果(配置!=null) { RequestedType=sourceType; 返回true; } 其他的 返回base.CanConvertFrom(上下文,sourceType); } 公共覆盖布尔CanConvertTo(ITypeDescriptorContext上下文,类型destinationType) { if(destinationType==typeof(string)) 返回true; 其他的 返回base.CanConvertTo(上下文,destinationType); } 公共重写对象ConvertFrom(ITypeDescriptorContext上下文、CultureInfo区域性、对象值) { var scContext=new SitecoreContext(); 返回scContext.CreateType(RequestedType,scContext.Database.GetItem(value.ToString()),true,false,new Dictionary()); } 公共重写对象转换为(ITypeDescriptorContext上下文、CultureInfo区域性、对象值、类型destinationType) { var config=Glass.Mapper.Context.Default.GetTypeConfiguration(value.GetType(),true); ID=config.GetId(值); 返回id.ToSortId().ToString().ToLowerInvariant(); }
我关心的是ConvertFrom方法没有传递所请求的类型,因此我们必须将其作为属性存储在类上,以便将其从CanConvertFrom方法传递到ConvertFrom方法。这使得该类不是线程安全的Linq lucene的d映射器,该映射器将从索引中的Guid转换为玻璃模型。我破解了这个,但还没有测试: public class IndexFieldDateTimeValueConverter : TypeConverter { public Type RequestedType { get; set; } public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { var config = Glass.Mapper.Context.Default.GetTypeConfiguration<SitecoreTypeConfiguration>(sourceType, true); if (config != null) { RequestedType = sourceType; return true; } else return base.CanConvertFrom(context, sourceType); } public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if (destinationType == typeof(string)) return true; else return base.CanConvertTo(context, destinationType); } public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { var scContext = new SitecoreContext(); return scContext.CreateType(RequestedType, scContext.Database.GetItem(value.ToString()),true, false, new Dictionary<string, object>()); } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { var config = Glass.Mapper.Context.Default.GetTypeConfiguration<SitecoreTypeConfiguration>(value.GetType(), true); ID id =config.GetId(value); return id.ToShortID().ToString().ToLowerInvariant(); } 公共类IndexFieldDateTimeValueConverter:TypeConverter { 公共类型RequestedType{get;set;} 公共覆盖布尔CanConvertFrom(ITypeScriptorContext上下文,类型sourceType) { var config=Glass.Mapper.Context.Default.GetTypeConfiguration(sourceType,true); 如果(配置!=null) { RequestedType=sourceType; 返回true; } 其他的 返回base.CanConvertFrom(上下文,sourceType); } 公共覆盖布尔CanConvertTo(ITypeDescriptorContext上下文,类型destinationType) { if(destinationType==typeof(string)) 返回true; 其他的 返回base.CanConvertTo(上下文,destinationType); } 公共重写对象ConvertFrom(ITypeDescriptorContext上下文、CultureInfo区域性、对象值) { var scContext=new SitecoreContext(); 返回scContext.CreateType(RequestedType,scContext.Database.GetItem(value.ToString()),true,false,new Dictionary()); } 公共重写对象转换为(ITypeDescriptorContext上下文、CultureInfo区域性、对象值、类型destinationType) { var config=Glass.Mapper.Context.Default.GetTypeConfiguration(value.GetType(),true); ID=config.GetId(值); 返回id.ToSortId().ToString().ToLowerInvariant(); },linq,sitecore,sitecore7,linq-query-syntax,glass-mapper,Linq,Sitecore,Sitecore7,Linq Query Syntax,Glass Mapper,我关心的是ConvertFrom方法没有传递所请求的类型,因此我们必须将其作为属性存储在类上,以便将其从CanConvertFrom方法传递到ConvertFrom方法。这使得该类不是线程安全的 将其添加到sitecore配置的indexFieldStorageValueFormatter部分。尝试将ContentSearch.EnableSearchDebug设置设置为true,然后更改logger“name=”sitecore.Diagnostics.Search"需要调试的级别,这将记录您
将其添加到sitecore配置的indexFieldStorageValueFormatter部分。尝试将ContentSearch.EnableSearchDebug设置设置为true,然后更改logger“name=”sitecore.Diagnostics.Search"需要调试的级别,这将记录您翻译的lucene查询,您可以检查ITI@AhmedOkour的错误,感谢您的提示。这有助于进一步挖掘。请参阅我编辑的部分。我遇到了完全相同的问题-没有找到任何其他人尝试这样做的参考。目前我正在使用自定义TypeConverter,它可以填充属性,但没有.Where筛选语法。@tomunderhill在其中一个答案中看到了我的工作。我没有将其标记为“正确答案”因为我想先试用Michael Edward的解决方案,在这种情况下,我仍然面临一些关于contentsearchindex(web)中填充索引值的问题基本上,我现在面临的是在我的真实代码中的where子句中添加两个条件。其中一个字段已填充,但另一个字段不是,即使它们都是droplink字段类型(我猜这与索引本身有关。有什么想法吗?)。我将很快在这里发布我的发现。我尝试使用“MyMappedSearchClass:SearchResultItem”和类似c=>c[“field1”]==“{GUID}”的查询。并且遇到了我在编辑的部分中提到的相同问题,例如字段值为空。我无法编译您的方法。它在“.Select”(I=>I.GlassCast())中出错"行。在这一行中,您获取每个
Sitecore.Context.Item
并将其强制转换为MyMappedClass
。执行强制转换的任何方法都应该在这里完成。嗨,Mike,我遇到了同样的问题,并尝试使用自定义类型转换器沿着这条路线前进。我的版本用于填充s上的复杂类型属性earch,但不适用于过滤这些属性上的搜索。有什么想法吗?我想这是因为这里提到的Linq解析功能:我已经尝试自己实现了这一点,您是否能够提供如何将其用作s过滤器的示例。我想这将需要深入理解Linq解析器。我们可能需要o与Pope先生谈谈这件事。
var results = context.GetQueryable<SearchResultItem>()
.Where(c => ((string)c["field1"]) == "{GUID}").GetResults();
List<MyMappedClass> results = context.GetQueryable<MyMappedClass>()
.Where(c => c.field1.MyValue == "{GUID}").ToList();
public string this[string key]
{
get
{
return key.ToLowerInvariant();
}
set { }
}
results2 = context.GetQueryable<MyMappedClass>().Where(c => c["filed1"]== "{GUID}").ToList();
<fieldType fieldTypeName="droplink" storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" type="System.String" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />
Exception: System.InvalidCastException
Message: Invalid cast from 'System.String' to 'MyLib.MyKeyValue'.
List<MyMappedClass> results = context.GetQueryable<SearchResultItem>()
.Select(sri => sri.GetItem())
.Where(i => i != null)
.Select(i => i.GlassCast<MyMappedClass>())
.Where(c => c.field1.MyValue == "{GUID}").ToList();
public abstract class SharedFieldClass
{
[SitecoreId]
[IndexField("_id")]
[TypeConverter(typeof(IndexFieldIDValueConverter))]
public virtual ID Id { get; set; }
[SitecoreInfo(SitecoreInfoType.Language)]
[IndexField("_language")]
public virtual string Language { get; set; }
[SitecoreInfo(SitecoreInfoType.Version)]
public virtual int Version
{
get
{
return Uri == null ? 0 : Uri.Version.Number;
}
}
[TypeConverter(typeof(IndexFieldItemUriValueConverter))]
[XmlIgnore]
[IndexField("_uniqueid")]
public virtual ItemUri Uri { get; set; }
}
var sitecoreService = new SitecoreService("web");
foreach (var r in results)
{
sitecoreService.Map(r);
}
[SitecoreId]
[IndexField("_id")]
[TypeConverter(typeof(IndexFieldIDValueConverter))]
public virtual ID Id { get; set; }
[SitecoreId]
[IndexField("_group")]
[TypeConverter(typeof(IndexFieldIDValueConverter))]
public virtual ID Id { get; set; }
[SitecoreType(TemplateId = "{TEMPLATE_GIUD}")]
public class MyMappedClass : SharedFieldClass
{
[SitecoreField(FieldName = "mylist")]
public virtual IEnumerable<SharedFieldClass> MyMultilistField { get; set; }
[SitecoreField(FieldName = "field1")]
[IndexField("field1")]
public virtual MyKeyValue field1 { get; set; }
// Will be set with key and value for each field in the index document
public string this[string key]
{
get
{
return key.ToLowerInvariant();
}
set { }
}
}
[SitecoreType]
public class MyKeyValue
{
public virtual Sitecore.Data.ID Id {get;set;}
public virtual string MyValue{get;set;}
}
List<MyMappedClass> results = context.GetQueryable<MyMappedClass>()
.Where(c => c["field1"] == "{GUID}").ToList();
public class IndexFieldKeyValueModelConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
var config = Glass.Mapper.Context.Default.GetTypeConfiguration<SitecoreTypeConfiguration>(sourceType, true);
if (config != null && sourceType == typeof(MyLib.IKeyValue))
{
return true;
}
else
return base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
return true;
else
return base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
var scContext = new SitecoreContext();
Guid x = Guid.Empty;
if(value is string)
{
x = new Guid((string)value);
}
var item = scContext.Database.GetItem(x.ToString());
if (item == null)
return null;
return scContext.CreateType(typeof(MyLib.IKeyValue), item, true, false, new Dictionary<string, object>());
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
var config = Glass.Mapper.Context.Default.GetTypeConfiguration<SitecoreTypeConfiguration>(value.GetType(), true);
ID id = config.GetId(value);
return id.ToShortID().ToString().ToLowerInvariant();
}
}
results = context.GetQueryable<MyMappedGlassClass>().Where(c => c["field1"] == field1value && c["field2"] == field2value && c["_template"] == templateId).Filter(selector => selector["_group"] != currentId).ToList();
public class IndexFieldDateTimeValueConverter : TypeConverter
{
public Type RequestedType { get; set; }
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
var config = Glass.Mapper.Context.Default.GetTypeConfiguration<SitecoreTypeConfiguration>(sourceType, true);
if (config != null)
{
RequestedType = sourceType;
return true;
}
else
return base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
return true;
else
return base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
var scContext = new SitecoreContext();
return scContext.CreateType(RequestedType, scContext.Database.GetItem(value.ToString()),true, false, new Dictionary<string, object>());
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
var config = Glass.Mapper.Context.Default.GetTypeConfiguration<SitecoreTypeConfiguration>(value.GetType(), true);
ID id =config.GetId(value);
return id.ToShortID().ToString().ToLowerInvariant();
}