如何在Umbraco 7中显示其网址的搜索结果

时间:2014-09-11 13:32:28

标签: search umbraco7 examine

我使用检查和用户控制在Umbraco 7中实现了搜索。搜索工作正常。现在我需要显示列表中显示的搜索结果的URL。是否有任何索引属性,如" BodyText"哪个可以显示URL,还是我必须在用户控件中做一些事情?

用户控制代码:

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

1 个答案:

答案 0 :(得分:0)

这很简单,我在Usercontrol(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>