2014-09-11 24 views
1

我已經在Umbraco 7中使用檢查和用戶控制執行搜索。搜索工作正常。現在我需要顯示列表中顯示的搜索結果的URL。有沒有像「BodyText」這樣的可以顯示URL的索引屬性,還是我必須在用戶控件中做一些事情?如何顯示搜索結果與他們的網址在Umbraco 7

用戶控件代碼:

public static class SiteSearchResultExtensions 
{ 
    public static string FullURL(this Examine.SearchResult sr) 
    { 
     return umbraco.library.NiceUrl(sr.Id); 
    } 
} 



public partial class SiteSearchResults : System.Web.UI.UserControl 
{ 
    #region Properties 

    private int _pageSize = 5; 
    public string PageSize 
    { 
     get { return _pageSize.ToString(); } 
     set 
     { 
      int pageSize; 
      if (int.TryParse(value, out pageSize)) 
      { 
       _pageSize = pageSize; 
      } 
      else 
      { 
       _pageSize = 10; 
      } 
     } 
    } 

    private string SearchTerm 
    { 
     get 
     { 
      object o = this.ViewState["SearchTerm"]; 
      if (o == null) 
       return ""; 
      else 
       return o.ToString(); 
     } 

     set 
     { 
      this.ViewState["SearchTerm"] = value; 
     } 
    } 

    protected IEnumerable<Examine.SearchResult> SearchResults 
    { 
     get; 
     private set; 
    } 

    #endregion 

    #region Events 

    protected override void OnLoad(EventArgs e) 
    { 
     try 
     { 
      CustomValidator.ErrorMessage = ""; 

      if (!Page.IsPostBack) 
      { 
       topDataPager.PageSize = _pageSize; 
       bottomDataPager.PageSize = _pageSize; 

       string terms = Request.QueryString["s"]; 
       if (!string.IsNullOrEmpty(terms)) 
       { 
        SearchTerm = terms; 
        PerformSearch(terms); 
       } 
      } 

      base.OnLoad(e); 
     } 
     catch (Exception ex) 
     { 
      CustomValidator.IsValid = false; 
      CustomValidator.ErrorMessage += Environment.NewLine + ex.Message; 
     } 
    } 

    protected void searchResultsListView_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e) 
    { 
     try 
     { 
      if (SearchTerm != "") 
      { 
       topDataPager.SetPageProperties(e.StartRowIndex, e.MaximumRows, false); 
       bottomDataPager.SetPageProperties(e.StartRowIndex, e.MaximumRows, false); 
       PerformSearch(SearchTerm); 
      } 
     } 
     catch (Exception ex) 
     { 
      CustomValidator.IsValid = false; 
      CustomValidator.ErrorMessage += Environment.NewLine + ex.Message; 
     } 
    } 

    #endregion 

    #region Methods 

    private void PerformSearch(string searchTerm) 
    { 
     if (string.IsNullOrEmpty(searchTerm)) return; 

     var criteria = ExamineManager.Instance 
      .SearchProviderCollection["ExternalSearcher"] 
      .CreateSearchCriteria(UmbracoExamine.IndexTypes.Content); 

     // Find pages that contain our search text in either their nodeName or bodyText fields... 
     // but exclude any pages that have been hidden. 
     //var filter = criteria 
     // .GroupedOr(new string[] { "nodeName", "bodyText" }, searchTerm) 
     // .Not() 
     // .Field("umbracoNaviHide", "1") 
     // .Compile(); 

     Examine.SearchCriteria.IBooleanOperation filter = null; 
     var searchKeywords = searchTerm.Split(' '); 
     int i = 0; 
     for (i = 0; i < searchKeywords.Length; i++) 
     { 
      if (filter == null) 
      { 
       filter = criteria.GroupedOr(new string[] { "nodeName", "bodyText", "browserTitle", "tags", "mediaTags" }, searchKeywords[i]); 
      } 
      else 
      { 
       filter = filter.Or().GroupedOr(new string[] { "nodeName", "bodyText", "browserTitle", "tags", "mediaTags" }, searchKeywords[i]); 
      } 
     } 

     //SearchResults = ExamineManager.Instance 
     // .SearchProviderCollection["ExternalSearcher"] 
     // .Search(filter); 

     SearchResults = ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"].Search(filter.Compile()); 

     if (SearchResults.Count() > 0) 
     { 
      searchResultsListView.DataSource = SearchResults.ToArray(); 
      searchResultsListView.DataBind(); 
      searchResultsListView.Visible = true; 
      bottomDataPager.Visible = topDataPager.Visible = (SearchResults.Count() > _pageSize); 
     } 

     summaryLiteral.Text = "<p>Your search for <b>" + searchTerm + "</b> returned <b>" + SearchResults.Count().ToString() + "</b> result(s)</p>"; 

     // Output the query which an be useful for debugging. 
     queryLiteral.Text = criteria.ToString(); 
    } 

    #endregion 
    } 
} 

我已經做了我檢查的設置是這樣的:

ExamineIndex.Config

<IndexSet SetName="ExternalndexSet" IndexPath="~/App_Data/TEMP/ExamineIndexes/ExternalIndex/"> 
     <IndexAttributeFields> 
      <add Name="id"/> 
      <add Name="nodeName"/> 
      <add Name="nodeTypeAlias"/> 
      <add Name="parentID" /> 
     </IndexAttributeFields> 
     <IndexUserFields> 
      <add Name="bodyText"/> 
      <add Name="umbracoNaviHide"/> 
    </IndexUserFields> 
    <IncludeNodeTypes/> 
    <ExcludeNodeTypes/> 
    </IndexSet> 

ExamineSettings.Config ExamineIndexProviders

 <add name="ExternalIndexer" type="UmbracoExamine.UmbracoContentIndexer, UmbracoExamine" 
     runAsync="true" 
     supportUnpublished="true" 
     supportProtected="true" 
     interval="10" 
     analyzer="Lucene.Net.Analysis.WhitespaceAnalyzer, Lucene.Net" 
     indexSet="DemoIndexSet" 
     /> 

ExamineSearchProvide

 <add name="ExternalSearcher" type="UmbracoExamine.UmbracoExamineSearcher, UmbracoExamine" 
    analyzer="Lucene.Net.Analysis.WhitespaceAnalyzer, Lucene.Net" 
     indexSet="DemoIndexSet" 
     /> 

回答

0

這是很簡單的,我所做的用戶控件(SiteSearchresult.ascx)一些編輯 這樣

<li> 
       <a href="<%# ((Examine.SearchResult)Container.DataItem).FullURL() %>"> 
        <%# ((Examine.SearchResult)Container.DataItem).Fields["nodeName"] %> 

       <br > 
      addingvalue.webdevstaging.co.uk<%# ((Examine.SearchResult)Container.DataItem).FullURL() %> 

       </a> 

       <p><%# ((Examine.SearchResult)Container.DataItem).Fields.ContainsKey("bodyText") == true ? ((Examine.SearchResult)Container.DataItem).Fields["bodyText"] : ""%></p> 
      </li>