2017-10-12 111 views
1

我一直在嘗試自學Sitecore過去幾個星期。
目前我正在嘗試創建一個供用戶搜索的食譜列表。Sitecore Lucene搜索與GlassMapper無法正常工作

但是每個食譜都含有成分,Lucene將這些成分作爲包含商品ID的字符串返回。我想在我的代碼中添加成分列表,所以我給了GlassMapper一個鏡頭。

因此,我通過更改名稱來排除Lucene中我的代碼中的成分列表,所以Lucene找不到該字段。 然後我設置GlassMapper來填充成分列表。但列表保持空。

如何讓GlassMapper爲我填充此列表?

我的代碼:
食譜類

[SitecoreType(TemplateId= "{1CF86642-6EC5-4B26-B8A7-1B2EC41F7783}")] 
public class Recipe : SearchResultItem 
{ 
    [SitecoreId] 
    public Guid Id { get { return base.ItemId.Guid; } } 
    public virtual string RecipeName { get; set; } 
    public virtual string BookName { get; set; } 
    public virtual IEnumerable<Ingredient> _Ingredients { get; set; } 
    public virtual int AmountOfPeople { get; set; } 
} 

成份類

[SitecoreType(TemplateId = "{730A0D54-A697-4DAA-908A-279CD24A9F41}")] 
public class Ingredient : SearchResultItem 
{ 
    [SitecoreId] 
    Guid Id { get; } 
    [IndexField("Name")] 
    public virtual string IngredientName { get; set; } 
} 

GlassMapperScCustom類(我只修改此方法)

public static IConfigurationLoader[] GlassLoaders() 
    { 
     var attributes = new SitecoreAttributeConfigurationLoader("Receptenboek"); 

     var loader = new SitecoreFluentConfigurationLoader(); 
     var config = loader.Add<Recipe>(); 


     config.Id(x => x.ItemId); 
     config.Info(x => x.Language).InfoType(SitecoreInfoType.Language); 
     config.Info(x => x.Version).InfoType(SitecoreInfoType.Version); 

     config.Field(x => x._Ingredients); 
     config.Info(x => x.Uri).InfoType(SitecoreInfoType.Url); 
     return new IConfigurationLoader[] {attributes, loader }; 

    } 

規則控制器

[HttpGet] 
    public ActionResult Index() 
    { 
     List<Recipe> recipes; 
     IQueryable<Recipe> query; 

     string index = string.Format("sitecore_{0}_index", Sitecore.Context.Database.Name); 
     var sitecoreService = new SitecoreService(Sitecore.Context.Database.Name); 
     string search = WebUtil.GetQueryString("search"); 

     using (var context = ContentSearchManager.GetIndex(index).CreateSearchContext()) 
     { 
      if (!string.IsNullOrEmpty(search)) 
      { 
       query = context.GetQueryable<Recipe>().Where(p => p.Path.Contains("/sitecore/Content/Home/Recipes/")).Where(p => p.TemplateName == "Recipe").Where(p => p.RecipeName.Contains(search)); 
      } 
      else 
      { 
       search = ""; 
       query = context.GetQueryable<Recipe>().Where(p => p.Path.Contains("/sitecore/Content/Home/Recipes/")).Where(p => p.TemplateName == "Recipe"); 
      } 
      recipes = query.ToList(); 
      foreach(var r in recipes) 
      { 
       sitecoreService.Map(r); 
       Sitecore.Diagnostics.Log.Audit("SWELF" + r.RecipeName + "- " + r.BookName + " - " + r.AmountOfPeople + " - " + r.Name + "--" + r._Ingredients.Count(), this); 

      } 
     } 
     RecipesViewModel bvm = new RecipesViewModel() { Recipes = recipes, Search = search }; 
     return View(bvm); 
    } 
+0

可能值得通過https://sitecore.stackexchange.com/詢問? – DaveParsons

回答

0

玩弄了一會兒,我決定我的拆分搜索和地圖多一點之後。我在Lucene中使用了食譜模型,並創建了一個ViewModel來將這些字段映射到GlassMapper。

食譜類沒有改變。
成分類沒有變化。
GlassMapperScCustom類不是必需的,所以我恢復了它的默認值。

RecipeViewModel類
映射到這個類後配料表成分過但是它的所有領域,其中空的正確的金額。 環顧四周,在互聯網上多了一些我發現這個計算器貼:Why isn't my Enumerable getting populated by Glass.Mapper?
我決定給SitecoreFieldType一個去,它做了詭計!

[SitecoreType(TemplateId = "{1CF86642-6EC5-4B26-B8A7-1B2EC41F7783}", AutoMap = true)] 
public class RecipeViewModel : BaseFields 
{ 
    [SitecoreId] 
    public ID Id { get; set; } 
    public virtual string RecipeName { get; set; } 
    public virtual string BookName { get; set; } 
    [SitecoreField(FieldId = "{D1603482-7CBC-4E55-9CCB-E51DC0FC5A0B}", FieldType = SitecoreFieldType.Multilist)] 
    public virtual IEnumerable<IngredientViewModel> Ingredients { get; set; } 
    public virtual int AmountOfPeople { get; set; } 
} 

規則控制器
它竟然是映射錯誤的方式aswel。我發現了一個列表的例子被映射到使用SitecoreService.GetItem <另一個列表>()

[HttpGet] 
    public ActionResult Index() 
    { 
     List<RecipeViewModel> recipes; 
     List<Recipe> query; 

     string index = string.Format("sitecore_{0}_index", Sitecore.Context.Database.Name); 
     var sitecoreService = new SitecoreService(Sitecore.Context.Database.Name); 
     string search = WebUtil.GetQueryString("search"); 

     //Search with Lucene 
     using (var context = ContentSearchManager.GetIndex(index).CreateSearchContext()) 
     { 
      if (!string.IsNullOrEmpty(search)) 
      { 
       query = context.GetQueryable<Recipe>().Where(p => p.Path.Contains("/sitecore/Content/Home/Recipes/")).Where(p => p.TemplateName == "Recipe").Where(p => p.RecipeName.Contains(search)).ToList(); 
      } 
      else 
      { 
       search = ""; 
       query = context.GetQueryable<Recipe>().Where(p => p.Path.Contains("/sitecore/Content/Home/Recipes/")).Where(p => p.TemplateName == "Recipe").ToList(); 
      } 
     } 
     //Map to ViewModel 
     recipes = query.Select(x => sitecoreService.GetItem<RecipeViewModel>(x.ItemId.Guid)).ToList(); 

     RecipesViewModel bvm = new RecipesViewModel() { Recipes = recipes, Search = search }; 
     return View(bvm); 
    } 

還有一個問題
因爲我的ViewModel不從SearchResultItem繼承了很多地方丟失有用的領域映射。爲了保持SearchResultItem所需的字段,我爲ViewModel創建了一個BaseFields類來繼承。我現在只需要Url,但這可以通過更多字段輕鬆擴展。

public class BaseFields 
{ 
    public virtual string Url { get; set; } 
}