Copied to clipboard

Flag this post as spam?

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


  • Steven Applegate 18 posts 178 karma points
    Dec 19, 2017 @ 23:03
    Steven Applegate
    0

    How can I convert Examine search results into a IPublishedContent collection

    I'm trying to create a simple search results page for my site. I have some test code to get some search only the node title:

    var searcher = ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"];
    var searchCriteria = Searcher.CreateSearchCriteria(BooleanOperation.Or);
    var query = searchCriteria.Field("nodeName", "blog").Compile();
    var searchResults = Searcher.Search(query);
    

    This works, but it's returning an Examine data type (ISearchResults)

    I'd like to convert it to IPublishedContent so that I can work with it Umbraco objects, instead of just the list of fields returned in ISearchResults.

    Is that possible?

  • Marcio Goularte 388 posts 1360 karma points
    Dec 20, 2017 @ 02:05
    Marcio Goularte
    101

    try this:

    List<IPublishedContent> contents = new List<IPublishedContent>();
    var searcher = ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"];
    var searchCriteria = Searcher.CreateSearchCriteria(BooleanOperation.Or);
    var query = searchCriteria.Field("nodeName", "blog").Compile();
    var searchResults = Searcher.Search(query);
    
    
    foreach (var item in searchResults)
     {
          contents .Add(Umbraco.TypedContent(item.Id));
     }
    

    I recommend the Paul Seal tutorial, it's very good

    http://www.codeshare.co.uk/blog/how-to-search-by-document-type-and-property-in-umbraco/

  • Murray Roke 503 posts 966 karma points c-trib
    Feb 14, 2018 @ 02:32
    Murray Roke
    0

    If you're dealing with media, something like this:

        private IPiblishedContent ConvertToViewModel(SearchResult x)
        {
            switch (x.Fields["__IndexType"])
            {
                case "content":
                    return Umbraco.TypedContent(x.Id);
                case "media":
                    return Umbraco.TypedMedia(x.Id);
                default:
                    throw new ArgumentException($"Index Type of '{x.Fields["__IndexType"]}' not recognised");
            }
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft