Copied to clipboard

Flag this post as spam?

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


  • SlothMonkey 17 posts 56 karma points
    Feb 25, 2015 @ 15:08
    SlothMonkey
    0

    Search returns items in media folder

    I've wrote my search code for my site and it returns entries and pages as I'd expect. However, it is all return items found in my media section (images, pdfs etc) that when clicked on, return an error as there is no page to display.

    Is there anyway I can add to my search code so that it doesn't display any results found within my media folder or for it to ignore images etc?

    My current search code is this:

    @{
        Layout = "Master.cshtml";
        @*search variables*@
        var searchTerm = Request["term"];
    
        if (String.IsNullOrEmpty(searchTerm))
        {
            <p>Please use the search box</p>
            return;
        }
    
    
        @*the searcher instance to use - Umbraco has 3 by default - InternalSearcher, ExternalSearcher and InternalMemberSearcher *@
        var searcher = ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"];
        var searchCriteria = searcher.CreateSearchCriteria(BooleanOperation.Or);
        @* define node properties to be searched below *@
        var query = searchCriteria.GroupedOr(new string[] { "pageTitle", "metaDescription", "nodeName", "heading", "bodyText", "summary" }, searchTerm.Fuzzy(0.5f)).Compile();
    
        var searchResults = searcher.Search(query);
        var resultPage = searchResults.Skip(currentPage * itemsPerPage).Take(itemsPerPage);
        var noResults = searchResults.Count();
        var numberOfPages = Math.Ceiling((decimal)((noResults - 1) / itemsPerPage)) + 1;
        var pages = Enumerable.Range(1, (int)numberOfPages);
    }
    
    @Html.Partial("Breadcrumb")
    
    <div class="container content">
        <h3>You searched for <b>@searchTerm</b>, we found @noResults results</h3>
    
        <ul class="row clearfix list-unstyled newslist">
            @foreach (var result in resultPage)
            {
                <li class="col-md-12 column">
                    <div class="row clearfix">
                        <div class="col-md-12 column">
    
                            <h2>
                                @if (result.Fields.ContainsKey("pageTitle"))
                                {
                                    <a href="@umbraco.library.NiceUrl(result.Id)">@result.Fields["pageTitle"]</a>
                                }
                                else
                                {
                                    <a href="@umbraco.library.NiceUrl(result.Id)">@result.Fields["nodeName"]</a>
                                }
    
                            </h2>
    
                            @if (result.Fields.ContainsKey("metaDescription"))
                            {
                                <p>
                                    @result.Fields["metaDescription"]
                                </p>
                            }
    
                            @if (result.Fields.ContainsKey("summary"))
                            {
    
                                <p>
                                    @result.Fields["summary"]
    
                                </p>
                            }
    
                            @*@foreach(var fieldItem in result.Fields)
                                {
                                    <p>@fieldItem.Key <b>: </b> @fieldItem.Value</p>
                                }*@
    
                            <p class="pull-left">
                            </p><div class="addthis_sharing_toolbox"></div>
                            <p></p>
    
                            <p class="pull-right">
                                <a class="btn btn-green" href="@umbraco.library.NiceUrl(result.Id)">View details ยป</a>
                            </p>
                        </div>
                    </div>
                </li>
            }
        </ul>
    </div>
    
Please Sign in or register to post replies

Write your reply to:

Draft