Copied to clipboard

Flag this post as spam?

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


  • bh 291 posts 976 karma points
    Feb 13, 2020 @ 21:00
    bh
    0

    v8 Search Exclude MediaItem

    I'm using this for my searcher:

    if (ExamineManager.Instance.TryGetIndex("ExternalIndex", out var index))
            {
                var searcher = index.GetSearcher();
                //var results = searcher.CreateQuery("content").NodeTypeAlias("home").And().Field("nodeName", searchTerm).Execute();
                //var results = searcher.CreateQuery().NativeQuery("+__IndexType:content +nodeName:" + searchTerm).Execute();
                //var results = searcher.CreateQuery().NativeQuery("+__IndexType:content " + searchTerm).Execute();
                var results = searcher.CreateQuery("content").ManagedQuery(searchTerm).Execute();
                if (results.Any())
                {
                    <ul>
                        @foreach (var result in results)
                        {
                            if (result.Id != null)
                            {
                                var node = Umbraco.Content(result.Id);
                                <li>
                                    <a href="@node.Url">@node.Name</a>
                                </li>
                            }
                        }
                    </ul>
                }
                else
                {
                    <p>No results found for query @searchTerm</p>
                }
            }
    

    The results correctly include a page that contains my search term. But, it's also returning an image with my search term in its file name. How do I exclude media items from my searcher?

  • bh 291 posts 976 karma points
    Feb 13, 2020 @ 21:22
    bh
    0

    This works...

    @if (!string.IsNullOrWhiteSpace(searchTerm)) {
            var results = Umbraco.ContentQuery.Search(searchTerm, string.Empty);
            long resultCount = results != null && results.Any() ? results.Count() : 0;
            if (resultCount > 0)
            {
                <ol>
                @foreach (var result in results)
                {
                    <li><strong><a href="@result.Content.Url">@result.Content.Name</a></strong></li>
                }
                </ol>
    
            }
            else
            {
                <p>No search results were found for <strong>@searchTerm</strong></p>
            }
    
        }
    

    Credit: https://our.umbraco.com/forum/umbraco-8/96004-simple-search-in-v8#comment-305886

  • Nik 1413 posts 6212 karma points MVP 3x c-trib
    Feb 13, 2020 @ 22:51
    Nik
    100

    Hi BH,

    Using examine, it is possible to exclude media from your search.

    To do this use the fluent API to add the following to your query:

    .And().Field("__IndexType", "content")
    

    This will ensure results given back are only for content.

    I believe that this is what searcher.CreateQuery("content") is meant to do but there is a bug in it. You could try upgrading Examine and it might fix that issue, although I can't recall if it does or not.

    Nik

  • bh 291 posts 976 karma points
    Feb 21, 2020 @ 14:23
    bh
    0

    I ended up using @Nik's code.

    @if (!string.IsNullOrWhiteSpace(searchTerm)) {
        if (ExamineManager.Instance.TryGetIndex("ExternalIndex", out var index)){
                var searcher = index.GetSearcher();
                var results = searcher.CreateQuery("content").ManagedQuery(searchTerm).And().GroupedOr(new[] { "__NodeTypeAlias" }, new[] { "page", "home", "communities", "communityDetail", "connect", "leadership", "news", "newsItem" }).And().Field("__IndexType", "content").Execute();
                if (results.Any())
                {
                    <ol class="scrollbar">
                        @foreach (var result in results)
                        {
                            if (result.Id != null)
                            {
                                var searchResult = Umbraco.Content(result.Id);
                                var searchText = (searchResult.Value("metaDescription") != null ? searchResult.Value("metaDescription") : (@searchResult.Value("CommunityDetialDescription") != null ? @searchResult.Value("CommunityDetialDescription") : string.Empty));
                                <li>                                
                                    <a href="@searchResult.Url"><b>@searchResult.Name</b><br />@Helper.Trunc(searchText.ToString(), 200)</a>
                                </li>
                            }
                        }
                    </ol>
                }
                else
                {
                    <p>No results found for query @searchTerm</p>
                }
            }
            return;
        }
    }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies