Copied to clipboard

Flag this post as spam?

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


  • Kasper Dyrvig 246 posts 379 karma points
    Jan 21, 2020 @ 15:01
    Kasper Dyrvig
    0

    Site search in site with language variations

    Hi all,

    So, I successfully migrated my site from Umbraco 7 to 8. Initially I also managed to make my search feature work again.

    Then I implemented using language variations. Now the search does not work.

    My goal is to search the site (ideally in just the "support" section) and show the results. But only per language.

    The current code follows:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using Umbraco.Examine
    @{
        var searchTerm = string.Empty;
        searchTerm = string.IsNullOrEmpty(Request["search"]) ? string.Empty : Request["search"];
    
        if (searchTerm != string.Empty)
        {
            if(ExamineManager.Instance.TryGetIndex("ExternalIndex", out var index))
            {
                var searcher = index.GetSearcher();
                var results = searcher.CreateQuery("content").NodeTypeAlias("SupportItem").And().Field("bodyText", searchTerm).Execute();
    
                if (results.Any())
                {
                    <h2 class="header-greenline">@Umbraco.GetDictionaryValue("Results for") "@searchTerm"</h2>
                    <ul>
                        @foreach (var result in results)
                        {
                            if (result.Id != null)
                            {
                                var node = Umbraco.Content(result.Id);
                                <li>
                                    <a href="@node.Url">@node.Value("bodyHeadline")</a>
                                </li>
                            }
                        }
                    </ul>
                }
                else
                {
                    <p>@Umbraco.GetDictionaryValue("No search results") @searchTerm</p>
                }
            }
            return;
        }
    }
    

    The above code consistenly returns zero results.

    I hope someone can help my in the right direction. Thank you!

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Jan 21, 2020 @ 19:03
    Nik
    100

    Hi Kasper,

    I believe that once you turn on multi-language features the indexes change and append the culture code to the end of their field name (only on the multi-lingual fields) ... so if your bodyText is now multi lingual and is available for both English(UK) and Welsh(UK), I believe 2 fields now exists in the index:

    bodyText_enGB and bodyText_cyGB

    If you update your search query to take this into account it should start working I think.

    Thanks,

    Nik

  • Kasper Dyrvig 246 posts 379 karma points
    Jan 23, 2020 @ 13:11
    Kasper Dyrvig
    0

    Hi Nik,

    Thank you very much for directing me here.

    I checked the index via backoffice and indeed the results are sorted in languages per fields. bodyText is now bodyText_da-dk for Danish edition.

    I'll continue work on this to accomodate the different languages.

  • Gurumurthy 54 posts 127 karma points
    Jul 26, 2021 @ 12:59
    Gurumurthy
    0

    Hi Nik,

    I was going through this approach, for creating an custom index for multi language, but in my case the index creates only for one default language not for any other variant enabled language.

    the index are not created by appending the lang code, as illustrated above. but i can see that its created for multiple lang for other type of indexs.

    Below is my code snippet:

         public void Initialize()
        {
            if (_examineManager.TryGetIndex("ExternalIndex", out IIndex externalIndex))
            {
                //initalizing the custom index for Article document type. Fileds Category, Tags and Author
                externalIndex.FieldDefinitionCollection.AddOrUpdate(
                    new FieldDefinition("searchableCategoryTagAuthor", FieldDefinitionTypes.FullText));
    
                ((BaseIndexProvider)externalIndex).TransformingIndexValues +=
                    IndexerComponent_TransformingIndexValues;
            }
        }
    

    setting values for custom index:

      if (tags != null && tags.Any())
      {
           e.ValueSet.Set("searchableCategoryTagAuthor", string.Join(" ", tags));
       }
    

    searchableCategoryTagAuthor my custom index name:

    enter image description here

    Any help would be greatly appreciated !

    Thanks, Gurumurthy J V

  • Andrew 23 posts 156 karma points
    Dec 08, 2022 @ 18:09
    Andrew
    0

    Hi,

    Did you ever figure this issue out? Currently trying to get past this myself with custom index fields and can't seem to reliably access the current culture of the node being indexed.

    Thanks Andrew

Please Sign in or register to post replies

Write your reply to:

Draft