2014-12-05 64 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");