2014-12-05 60 views
1

我使用Sitecore 7.2与MVC和页面构建的组件方法。这意味着页面大部分是空的,内容来自页面上的各种渲染。但是,我希望搜索结果返回主页面,而不是单个内容片段。如何用Lucene索引Sitecore中的子内容?

下面是基本的代码,我到目前为止有:

public IEnumerable<Item> GetItemsByKeywords(string[] keywords) 
{ 
    var index = ContentSearchManager.GetIndex("sitecore_master_index"); 
    var allowedTemplates = new List<ID>(); 
    IEnumerable<Item> items; 

    // Only Page templates should be returned 
    allowedTemplates.Add(new Sitecore.Data.ID("{842FAE42-802A-41F5-96DA-82FD038A9EB0}")); 

    using (var context = index.CreateSearchContext(SearchSecurityOptions.EnableSecurityCheck)) 
    { 
     var keywordsPredicate = PredicateBuilder.True<SearchResultItem>(); 
     var templatePredicate = PredicateBuilder.True<SearchResultItem>(); 
     SearchResults<SearchResultItem> results; 

     // Only return results from allowed templates 
     templatePredicate = allowedTemplates.Aggregate(templatePredicate, (current, t) => current.Or(p => p.TemplateId == t)); 

     // Add keywords to predicate 
     foreach (string keyword in keywords) 
     { 
      keywordsPredicate = keywordsPredicate.And(p => p.Content.Contains(keyword)); 
     } 

     results = context.GetQueryable<SearchResultItem>().Where(keywordsPredicate).Filter(templatePredicate).GetResults(); 
     items = results.Hits.Select(hit => hit.Document.GetItem()); 
    } 

    return items; 
} 

回答

1

你可以在其中查看页面上的效果图,并解决每个渲染的数据源项的索引创建一个计算字段。一旦你有这些项目中的每一个,你可以索引他们的领域,并连接所有这些数据。

一种选择是使用原生“内容”计算字段进行此操作,该字段本来就是全文搜索使用的字段。

0

另一种解决方案是使HttpRequest返回到您发布的网站,并基本上刮掉HTML。这确保所有的渲染都包含在索引中。

您可能不想索引通用部分,如菜单和页脚,因此请使用HTMLAgilityPackFizzlerEx仅返回特定父容器的内容。你可以更聪明地去除内部容器,这是你需要的。只记得去掉html标签:)

using HtmlAgilityPack; 
using Fizzler.Systems.HtmlAgilityPack; 

//get the page 
var web = new HtmlWeb(); 
var document = web.Load("http://localsite/url-to-page"); 
var page = document.DocumentNode; 

var content = page.QuerySelector("div.main-content");