Search code examples
searchrazorumbracoexamine

Umbraco 7+ Search


I wonder if someone could help me... I am trying to get the Examine search working in Umbraco 7.3 but am struggling with the documentation.

https://our.umbraco.org/documentation/Reference/Searching/Examine/quick-start

when I look at the following files ExamineSettings.config and ExamineIndex.config they seem to already have the ExternalIndexer, ExternalSearcher and ExternalIndexSet already specified. I guess this is an enhancement from 4.11 which the document seems to have been written for.

But it is the section after where I seem to get lost... Is the Razor section for a Partial View? And what does the full code need to be?

I would assume if it is a partial view I would create a page to include the partial view @(Html.Partial("searcher"))

and if I viewed the page and appended a query string ~/?query=keyword this would work?

Any help would be great.

Thanks in advance


Solution

  • In Config/ExamineIndex.config, you create a new IndexSet with all the properties you want users to be able to search for.

    The Umbraco.TypedSearch(Request.QueryString["query"]); will search for anything throughout the page, but if you set up UmbracoExamine correctly, you can choose what Document Types you want people to be able to search for, as well as you can choose what kind of properties you want people to be able to index the content based on.

    For example:

    <IndexSet SetName="ExternalTopLevelSearchSet" IndexPath="~/App_Data/TEMP/ExamineIndexes/{machinename}/TopLevelSearch/">
        <IndexAttributeFields>
          <add Name="Name"/>
          <add Name="bodyText"/>
          <add Name="tags"/>
          <add Name="themes"/>
          <add Name="parentID"/>
        </IndexAttributeFields>
        <IncludeNodeTypes>
          <add Name="ArticlePage" />
        </IncludeNodeTypes>
      </IndexSet>
    

    Then you have to create an Indexer in Config/ExamineSettings.config

      <add name="ExternalTopLevelIndexer" type="UmbracoExamine.UmbracoContentIndexer, UmbracoExamine"
            supportUnpublished="false"
            supportProtected="false"
            interval="10"
            analyzer="Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net"
            indexSet="ExternalTopLevelSearchSet"/>
    

    Now, you create a Search Provider, also in Config/ExamineSettings.config

    <add name="ExternalTopLevelSearcher" type="UmbracoExamine.UmbracoExamineSearcher, UmbracoExamine"
               analyzer="Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net" indexSet="ExternalTopLevelSearchSet" enableLeadingWildcards="true"/>
    

    Here's my C# code using the "ExternalTopLevel"-searcher shown above.

    public List<SearchResult> SearchResults
            {
                get
                {
                    if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["q"])) {
                        var searcher = ExamineManager.Instance.SearchProviderCollection["ExternalTopLevelSearcher"];
                        // Search criteria.
                        var searchCriteria = searcher.CreateSearchCriteria(BooleanOperation.Or);
                        var q = HttpContext.Current.Request.QueryString["q"].ToLower().Trim().Split(' ');
                        var contentType = HelperClass.GetContentTypeNodes().FirstOrDefault(item => q.Contains(item.Name.ToLower()));
                        q = q.Where(i => i.Length > 3).ToArray();
                        var query = searchCriteria
                            .GroupedOr(new[] { "nodeName" }, q.Select(x => x.Boost(150)).ToArray())
                            .Or()
                            .GroupedOr(new[] { "grid" }, q.Select(x => x.Boost(80)).ToArray())
                            .Or()
                            .GroupedOr(new[] { "tags", "themes", "institutions" }, q.Select(x => x.Boost(110)).ToArray());
                        // Search results
                        var searchResults = searcher.Search(query.Compile()).OrderByDescending(x => x.Score);
                        return searchResults.ToList();
                    }
                    return new List<SearchResult>();
                }
            }
    

    Hope this helps you out!