Copied to clipboard

Flag this post as spam?

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


  • bh 226 posts 808 karma points
    3 days ago
    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 226 posts 808 karma points
    3 days ago
    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 1197 posts 4973 karma points MVP 2x c-trib
    3 days ago
    Nik
    1

    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

Please Sign in or register to post replies

Write your reply to:

Draft