Copied to clipboard

Flag this post as spam?

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


  • BEWD 71 posts 233 karma points
    Sep 29, 2019 @ 09:01
    BEWD
    0

    Search cant handle spaces

    Hi

    I have manged to get the following basic search working on a site and it in returning both content and media items. However, It cannot handle a search phrase.

    For example, If I search for "debit" it will return both media and content items which is perfect, but if I search for "direct debit" it doesn't return anything which it should.

    <div class="container">
        @Html.Partial("breadcrumb")
    </div>
    <div class="container">
        <main class="main-wrapper">
            <div class="container">
                <main class="main-wrapper">
                    <h2 class="heading-m-home">Search Results for</h2>
                    <form action="@Model.Url" method="GET">
                        <div class="searchPage">
                            <input type="text" class="searchTerm" placeholder="Search" id="query" name="query" value="@searchQuery" />
                            <button class="searchButton"><span class="fa fa-search">Search</span></button>
                        </div>
                        <div class="grid-row">
                            @{
                                var searchTerm = string.Empty;
                                searchTerm = string.IsNullOrEmpty(Request["query"])
                                    ? string.Empty
                                    : Request["query"];
                                if (searchTerm == string.Empty)
                                {
                                    <p>Enter search term</p>
                                }
                                else
                                {
                                    //perform the search
                                    //first we try to get the index, it is the ExternalIndex as we don't want to return unpublished things
                                    //it returns the index in the var index
                                    if (ExamineManager.Instance.TryGetIndex("ExternalIndex", out var index))
                                    {
                                        var searcher = index.GetSearcher();
                                        var textFields = new[] { "bodyText", "pageTitle", "pageTitle", "pageName", "pageDescription" };
                                        var results = searcher.CreateQuery("content").GroupedOr(textFields, searchTerm.Fuzzy()).Execute();
                                        var mediaFields = new[] { "altText", "umbracoFile" };
                                        var mediaResults = searcher.CreateQuery("media").GroupedOr(mediaFields, searchTerm.Fuzzy()).Execute();
    
                                        if (results.Any())
                                        {
                                            <div class="grid-column-full searchResults">
                                                @foreach (var result in results)
    
                                                {
                                                    if (result.Id != null)
                                                    {
                                                        var node = Umbraco.Content(result.Id);
    
                                                        <div class="post-preview">
                                                            <a href="@node.Url">
                                                                <h2 class="class=" post-title">
                                                                    @node.Name
                                                                </h2>
                                                                <p class="post-subtitle">
                                                                    @node.Value("pageDescription")
                                                                </p>
                                                            </a>
                                                        </div>
                                                    }
                                                }
                                            </div>
                                        }
                                        if (mediaResults.Any())
                                        {
                                            <div class="grid-column-full searchResults">
                                                @foreach (var mediaResult in mediaResults)
    
                                                {
                                                    if (mediaResult.Id != null)
                                                    {
                                                        var mediaNode = Umbraco.Media(mediaResult.Id);
    
                                                        <div class="post-preview">
                                                            <a href="@mediaNode.Url">
                                                                <h2 class="class=" post-title">
                                                                    @mediaNode.Value("altText")
                                                                </h2>
                                                            </a>
                                                        </div>
                                                    }
                                                }
                                            </div>
                                        }
                                    }
                                }
                            }
                        </div>
                    </form>
                </main>
            </div>
        </main>
    </div>
    

    I am sure that there is a simple solution to this but is anyone able to point me in the right direction as I have been banging my head on a brick wall for the last day or so trying to get this to work.

    Thanks in advance

    Ben

  • Shaishav Karnani from digitallymedia.com 330 posts 1537 karma points
    Sep 30, 2019 @ 06:27
    Shaishav Karnani from digitallymedia.com
    0

    Hi Ben,

    Have you tried Examine Management Section in the back-office? It does work with Search. I think you can try it and see if it is different to your results.

    enter image description here

    Hope that helps to solve your issue.

    Cheers,

    Shaishav

  • BEWD 71 posts 233 karma points
    Sep 30, 2019 @ 07:20
    BEWD
    0

    Hi Shaishav

    Thanks for your response. I have checked the Examin Management section and yes, it does return what I am expecting it to return

    Search Results

    I wouldn't have thought that to return a string in a search would be so tricky? I'm sure I am missing something simple?

    Ben

  • BEWD 71 posts 233 karma points
    Sep 30, 2019 @ 12:33
    BEWD
    0

    Ok, I think I have cracked the spaces with the folowing

      var textFields = new[] { "bodyText", "pageTitle", "pageName", "pageDescription" };
      var results = searcher.CreateQuery("content").GroupedOr(textFields, searchTerm.ToString().Fuzzy(0.35f)).Execute();
    

    Now to try to figure out how to boost certain fields

    Ben

Please Sign in or register to post replies

Write your reply to:

Draft