Copied to clipboard

Flag this post as spam?

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


  • Hein 29 posts 158 karma points
    Sep 27, 2017 @ 11:47
    Hein
    0

    Get typed results for examine indexer using Umbraco

    Continueing on this question on this forum Get typed document from an index using Umbraco 7.7, I've found a working solution.

    The case

    The case I'll create is that I've got a lot of persons and a knows person get 0, 1 or multiple languages. The language can get 0, 1 or multiple MVPs. Inside the back office of Umbraco I've created two document types:

    • Person: with the first name, last name, photo and a (Obsolete) Multinode Treepicker for the known programming languages of that person.
    • Language: With the name and a piece of code.

    The code

    On the template of the language, I'll create a table with the language, the code sample and the MVPs.

    For this I've create this code.

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ProgrammingLanguages>
    @using EventCalendar.Website.Models;
    
    @{
        Layout = "Master.cshtml";
    
        var searcher = ExamineManager.Instance.SearchProviderCollection["PersonsSearcher"];
        var searchCriteria = searcher.CreateSearchCriteria();
        var query = searchCriteria.Field("nodeTypeAlias", "person").Compile();
        var persons = searcher.Search(query);
    }
    
    @Html.Partial("~/Views/Partials/SectionHeader.cshtml")
    
    <table class="table">
        <tr>
            <th>Name</th>
            <th>Output <samp>Hello World!</samp></th>
            <th>MVP</th>
        </tr>
    
        @foreach (ProgrammingLanguage lang in Model.Content.Children)
        {
            var res = persons.Where(x => x.Fields["knowledge"] != null && x.Fields["knowledge"].Contains(lang.Id.ToString())); @* <-- instead of this *@
    
            <tr>
                <td>@lang.PageTitle</td>
                <td><pre>@lang.HelloWorld</pre></td>
                <td>
                    <ul>
                        @foreach (var r in res)
                        {
                            <li>@r.Fields["firstName"] @r.Fields["lastName"]</li>
                        }
                    </ul>
                </td>
            </tr>
        }
    </table>
    

    This outputs this:

    Output

    Good to know

    I've created an indexer and a searcher for the persons.

    Examine Manager of Umbraco

    After building:

    Build of PersonsIndexer

    My questions

    1. Is this code performance enough?
    2. Can I use typed persons? I'll like to use this code below instead of the marked line.

      var res = persons.Where(x => x.Knowledge != null && x.Knowledge.Contains(lang)));
      
Please Sign in or register to post replies

Write your reply to:

Draft