2013-11-04 155 views
2

我正在使用Lucene.Net和Sitecore.Search.Crawlers.DatabaseCrawler。目前,這項搜索適用於所有領域,我想將其改爲僅在幾個領域進行搜索。 我有自定義爬蟲:Sitecore使用Lucene.Net搜索:在特定字段中搜索

public class CustomCrawler : Sitecore.Search.Crawlers.DatabaseCrawler 
{ 
    protected override void AddAllFields(Lucene.Net.Documents.Document document, Sitecore.Data.Items.Item item, bool versionSpecific) 
    { 
     base.AddAllFields(document, item, versionSpecific); 
     document.Add(new Field("title", item["TitleField"], Field.Store.YES, Field.Index.TOKENIZED)); 
     document.Add(new Field("image", imageUrl, Field.Store.YES, Field.Index.TOKENIZED));  
    } 

    protected override bool IsMatch(Item item) 
    { 
     if (!item.TemplateName.Contains("txttmpl")) return false; 

     return base.IsMatch(item); 
    } 
} 

我使用的標題和圖像領域的搜索結果,並在網頁上顯示它們:在web.config文件

var list = new List<SearchResult>(); 
foreach (var result in results) 
{ 
    list.Add(new SearchResult() 
    { 
     Title = result.Document.GetField("title").StringValue(), 
     Image = result.Document.GetField("image").StringValue() 
    }); 
} 

var jss = new JavaScriptSerializer(); 
httpContext.Response.ContentType = "application/json"; 
httpContext.Response.Write(jss.Serialize(list)); 
httpContext.Response.Flush(); 

和:

<index id="myindex" type="Sitecore.Search.Index, Sitecore.Kernel"> 
    <param desc="name">$(id)</param> 
    <param desc="folder">Myfolder</param> 
    <Analyzer ref="search/analyzer" /> 
    <locations hint="list:AddCrawler"> 
     <web type="Search.CustomCrawler, Search"> 
      <Database>web</Database> 
      <Tags>web content</Tags> 
      <Root>/sitecore/content/Site</Root>        
      <Boost>2.0</Boost> 
     </web> 
    </locations> 
</index> 

上面的解決方案在所有領域搜索。我怎樣才能讓它只在
某些字段中搜索?我嘗試過document.RemoveField(「SomeFieldName」),但它不起作用。如何刪除或添加一些字段?提前致謝。

回答

4

您可以搜索通過使用下面的搜索結構中的特定字段:

 SearchManager.GetIndex("my_index").Rebuild(); 

     using (IndexSearchContext indexSearchContext = SearchManager.GetIndex("my_index").CreateSearchContext()) 
     { 
      // Field to be searched followed by search term 
      Term term = new Term("location", "Ottawa"); 
      Query query = new TermQuery(term); 

      SearchHits hits = indexSearchContext.Search(query, int.MaxValue); 
      // Get Sitecore items from the results of the query 
      List<Item> items = hits.FetchResults(0, int.MaxValue).Select(result => result.GetObject<Item>()).Where(item => item != null).ToList(); 
     } 

的指數可以建立索引的所有領域,這將繼續努力:

 <index id="my_index" type="Sitecore.Search.Index, Sitecore.Kernel"> 
     <param desc="name">$(id)</param> 
     <param desc="folder">dance_map_locations_index</param> 
     <Analyzer ref="search/analyzer" /> 
     <locations hint="list:AddCrawler"> 
      <core type="Sitecore.Search.Crawlers.DatabaseCrawler,Sitecore.Kernel"> 
      <Database>web</Database> 
      <Root>/sitecore/content/my first item/second item/parent item to be indexed</Root> 
      <templates hint="list:IncludeTemplate"> 
       <template>{AD7E2747-695A-4AC8-A6AB-C7C6111AF9A7}</template> 
      </templates> 
      </core> 
     </locations> 
     </index> 
0

你不應該」你需要一個自定義的抓取工具來完成你想要實現的大部分功能。您可以添加<IndexAllFields>false</IndexAllFields><web>節點內,從添加的所有領域保持,然後添加這樣一段:

<fields hint="raw:AddCustomField"> 
    <field luceneName="title" storageType="no" indexType="tokenized">TitleField</field> 
    <field luceneName="image" storageType="yes" indexType="untokenized">imageUrl</field> 
</fields> 

但是,因爲它看起來像你想補充一點,圖像src和不完整圖像字段的XML,您可能需要使用高級數據庫爬網程序並創建一個dynamicField。 http://sitecorian.github.io/SitecoreSearchContrib/

或者,如果您有升級到Sitecore 7的選項,您可以創建一個計算字段。有關動態字段和計算字段的更多詳細信息,請參見this question