Copied to clipboard

Flag this post as spam?

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


  • Mus'ab 157 posts 385 karma points notactivated
    Sep 16, 2019 @ 15:46
    Mus'ab
    0

    How to customize back office search in umbraco 8

    Hello ! i am trying to customize the back office search in umbraco 8 to return nodes based on specific property value i found this code in umbraco documentation https://our.umbraco.com/documentation/extending/section-trees/Searchable-Trees/

    enter code herepublic class FavouriteThingsSearchableTree : ISearchableTree
    {
        public string TreeAlias => "favouriteThingsAlias";
    
        public IEnumerable<SearchResultEntity> Search(string query, int pageSize, long pageIndex, out long totalFound, string searchFrom = null)
        {
            // your custom search implementation starts here
            Dictionary<int, string> favouriteThings = new Dictionary<int, string>();
            favouriteThings.Add(1, "Raindrops on Roses");
            favouriteThings.Add(2, "Whiskers on Kittens");
            favouriteThings.Add(3, "Skys full of Stars");
            favouriteThings.Add(4, "Warm Woolen Mittens");
            favouriteThings.Add(5, "Cream coloured Unicorns");
            favouriteThings.Add(6, "Schnitzel with Noodles");
    
            var searchResults = new List<SearchResultEntity>();
    
            var matchingItems = favouriteThings.Where(f => f.Value.StartsWith(query, true, System.Globalization.CultureInfo.CurrentCulture));
            foreach (var matchingItem in matchingItems)
            {
                // making up the Id/Udi for this example! - these would normally be different for each search result.
                searchResults.Add(new SearchResultEntity() { Id = 12345, Alias = "favouriteThingItem", Icon = "icon-favorite", Key = new Guid("325746a0-ec1e-44e8-8f7b-6e7c4aab36d1"), Name = matchingItem.Value, ParentId = -1, Path = "-1,123456", Score = 1.0F, Trashed = false, Udi = Udi.Create("document", new Guid("325746a0-ec1e-44e8-8f7b-6e7c4aab36d1")) });
            }
            // set number of search results found
            totalFound = matchingItems.Count();
            // return your IEnumerable of SearchResultEntitys
            return searchResults;
        }
    }
    

    but i cant understand what should i fill in this line :searchResults.Add(new SearchResultEntity() { Id = 12345, Alias = "favouriteThingItem", Icon = "icon-favorite", Key = new Guid("325746a0-ec1e-44e8-8f7b-6e7c4aab36d1"), Name = matchingItem.Value, ParentId = -1, Path = "-1,123456", Score = 1.0F, Trashed = false, Udi = Udi.Create("document", new Guid("325746a0-ec1e-44e8-8f7b-6e7c4aab36d1")) });

    ?! any body can help ?

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Sep 17, 2019 @ 09:01
    Marc Goodson
    0

    Hi Mus'ab

    In the example in the docs that line is showing you how to populate an individual search result, into the collection of search results that Umbraco will display for the search term...

    The search mechnism is used across all Umbraco sections and trees, and so there is a common format for how to return the Search Result, eg in the form of a SearchResultEntity

    Normally you will be returning items that are in Umbraco and they will have an Id, an alias, a ParentId , Path etc...

    ... but if your tree does not, and your search result items come from a database, then you'll need to fake these values, in order for the search to work...

    in the example it is just a list of favourite things in the tree.

    they are at root level, so we know immediately the ParentId should be -1, and the items 'dont' have' a unique Id, but if they came from a database they might do, and you could construct therefore Id, and then the Path would be "-1, theid".

    The core searches all use Examine, and this returns a float value determining how well the search results matches the search term... again depending on your search mechnism, database, Examine, Azure Search, you might have a way for measuring that too and supplying a Float value as the Score property... this controls the ordering of the search results....

    Trashed indicates whether the item has been deleted, eg content in a recycle bin - your tree might not support such a concept! so Trashed will always be false... finally the SearchResult expect a unique Umbraco identifier Udi, and the above shows how one can be created from a Guid, this would be different for each search result and again depends on what and how you are searching as to the best way to determine it!

    anyway hope that demystifies it a bit.

    regards

    Marc

  • Mus'ab 157 posts 385 karma points notactivated
    Sep 17, 2019 @ 09:46
    Mus'ab
    0

    Hi Marc Goodson

    thank you for your help i think that you put me on the right way but there is some things i didn't understand it

    if i want to search in specific node descendants based on property value using examine what i have to fill in the searchResults.Add(new SearchResultEntity() { Id = 99579, Alias = etc ...

    and what i should do with public SearchResultEntity();

    its generate error i thin that i must to convert it to method with IEnumerable

    regards

    Mus'ab

Please Sign in or register to post replies

Write your reply to:

Draft