2014-09-11 1 views
1

J'ai implémenté la recherche dans Umbraco 7 en utilisant l'examen et le contrôle utilisateur. La recherche fonctionne bien. Maintenant, je dois montrer l'URL des résultats de recherche affichés sur les listes. Y a-t-il une propriété d'index comme "BodyText" qui peut montrer l'URL ou dois-je faire des choses dans le contrôle de l'utilisateur?Comment afficher les résultats de recherche avec leurs URLs dans Umbraco 7

Code de contrôle de l'utilisateur:

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 
    } 
} 

Je l'ai fait mes paramètres comme ceci Examiner:

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" 
     /> 

Répondre

0

Il était assez simple et je fait des modifications dans le Usercontrol (SiteSearchresult.ascx) comme celui-ci

<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> 
Questions connexes