Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bora B 32 posts 124 karma points
    Mar 18, 2015 @ 12:13
    Bora B
    1

    filtering examine search results in current culture only

    Hi,

    I have a simple search in razor and a multilanguage website tree like below

    CONTENT - TR --search - EN --search

    all the content are in their own language tree and the culture is set accordingly. I want the sarch to show results only in the same language. How should I modify the below code?

    var query = Request.QueryString["q"];
    IEnumerable<IPublishedContent> results = Umbraco.TypedSearch(query, true, "ExternalSearcher").Where(c => (c.TemplateId ==  1097 
                                                                                                               || c.TemplateId ==  1061 
                                                                                                               || c.TemplateId ==  1059 
                                                                                                               || c.TemplateId ==  1052 
                                                                                                               || c.TemplateId ==  1056 
                                                                                                               || c.TemplateId ==  1086) 
                                                                                                     && c.ItemType == PublishedItemType.Content);
    

    Thanks in advance :)

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Mar 18, 2015 @ 12:29
    Dave Woestenborghs
    0

    I think the best way is to store your culture in the index. So you can use it in your search clause.

    You can do this by hooking in to the GatheringNodeData event of examine.

    A google on that will give a you a lot of examples.

    Dave

     

  • Bora B 32 posts 124 karma points
    Mar 18, 2015 @ 21:15
    Bora B
    100

    Hi Dave,

    thanks for the answer but I found an easier way to do it..

    var parent = CurrentPage.AncestorOrSelf(1);
        var query = Request.QueryString["q"];
        IEnumerable<IPublishedContent> results = Umbraco.TypedSearch(query, true, "ExternalSearcher").Where(c => (c.TemplateId ==  1097 
                                                                                                                   || c.TemplateId ==  1061 
                                                                                                                   || c.TemplateId ==  1059 
                                                                                                                   || c.TemplateId ==  1052 
                                                                                                                   || c.TemplateId ==  1056 
                                                                                                                   || c.TemplateId ==  1086) 
                                                                                                         && c.ItemType == PublishedItemType.Content);
    
    foreach (var result in results)
            {
                if(result.Path.StartsWith(parent.Path))
                {
                <a href="@result.Url"><p class="lead">@result.Name</p></a>
    
                }
            }
    
  • Kyle Weems 42 posts 296 karma points MVP 7x c-trib
    Mar 18, 2015 @ 23:11
    Kyle Weems
    3

    Hi Bora,

    Looks like you have a solution that works for you, but here's an alternate approach I took on a recent project that might be worth considering in the future:

    In ExamineIndex.config:

    <ExamineLuceneIndexSets>
        <!-- per culture -->
        <IndexSet SetName="en-US-ExternalIndexSet" IndexPath="~/App_Data/TEMP/ExamineIndexes/External-en-US" IndexParentId="1057" />
    </ExamineLuceneIndexSets>
    

    With the IndexParentId matching the root node of the culture.

    In ExamineSettings.config:

    <Examine>
      <ExamineIndexProviders>
        <providers>
          <!-- per culture -->
          <add name="en-US-ExternalIndexer" type="UmbracoExamine.UmbracoContentIndexer, UmbracoExamine" supportUnpublished="false" indexSet="en-US-ExternalIndexSet" />
        </providers>
      </ExamineIndexProviders>
      <ExamineSearchProviders defaultProvider="ExternalSearcher">
        <providers>
          <!-- per culture -->
          <add name="en-US-Searcher" type="UmbracoExamine.UmbracoExamineSearcher, UmbracoExamine" analyzer="Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net" indexSet="en-US-ExternalIndexSet" enableLeadingWildcards="true"/>
        </providers>
      </ExamineSearchProviders>
    </Examine>
    

    And then something like this example function in an API controller (modify as needed to your use case):

        public IEnumerable<Foo> Search(string culture, string searchTerm)
        {
            var searcher = ExamineManager.Instance.SearchProviderCollection[culture + "-Searcher"];
            var criteria = searcher.CreateSearchCriteria();
            var result = Umbraco.TypedSearch(criteria, searcher).ToArray();
            return result.Select(obj => new Foo() { Name = obj.Name, Widget = obj.Widget });
        }
    

    It has the benefit of letting you not having to modify the C# code after launch if you add more cultures, instead only modifying the Examine configs.

    Hope it helps :)

Please Sign in or register to post replies

Write your reply to:

Draft