Sort the search results on the member search by a custom field

Former Member
Former Member

Can the search results on the member search page be sorted by an ExtendedAttributes or ProfileFields field?

Parents Reply Children
  • Former Member
    0 Former Member in reply to Patrick M.

    How often does the BeforeBulkIndex event get fired in a user's lifecycle? Is it only once? My custom sort field can change quite often, and it might not have a value before the event gets fired. Will that be a problem?

  • That event gets fired before altered users get changed in the index, which varies by how often the job runs and how many changes need to be processed.  You still will have to store this value somehow and read it when the event is fired

  • Former Member
    0 Former Member in reply to Patrick M.

    I've attempted to write some code for this. Would this work as a widget extension?

    public class MyUsers
        {
            public Telligent.Evolution.Extensibility.Api.Entities.Version1.SearchResults GetUsers(int pageIndex, int pageSize, string filters, string query)
            {
                ISearchResults search = Apis.Get<ISearchResults>();
                var searchOptions = new SearchResultsListOptions
                {
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    Query = query,
                    Filters = filters
                };
                searchOptions.Sort = "UserSortOrder";
                var results = search.List(searchOptions);
                return results;           //Apis.Get<Users>().List(options);
    
            }
    
            public void BeforeSearchBulkIndexingEventHandler(BeforeBulkIndexingEventArgs e)
            {
                SearchIndexDocument d = new SearchIndexDocument();
                d.AddField("UserSortOrder", GetSortOrder(d.ContentId));
                e.Documents.Append(d);
            }
    
            private string GetSortOrder(Guid contentId)
            {
                SqlDataAccess sd = new SqlDataAccess();
                using (SqlCommand cmd = sd.GetCommand("ibby_UserSortOrder_Get"))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@ContentId", contentId);
                    DataTable dt = sd.Execute(cmd);
                    if (dt.Rows.Count > 0)
                        return dt.Rows[0]["UserSortOrder"].ToString();
                    else
                        return string.Empty;
                }
            }
        }

  • You are not longer writing a widget extension.  You are writing a plugin and possibly modifying widget code.  

  • Former Member
    0 Former Member in reply to Patrick M.

    To which of the SearchIndexDocument Documents should I add the custom sort field? The first, the last, a specific one, or all of them?

  • You would evaluate them to be of the users content type 

  • Former Member
    0 Former Member in reply to Patrick M.

    Here's the code for the Plugin that I've written to handle the BeforeBulkIndex event for users. I've copied the DLL to the website's bin folder, and the JobServer folder. Does it look okay? How would I get the event to fire for all the users so that it would update all the users?

    using Telligent.Evolution.Extensibility;
    using Telligent.Evolution.Extensibility.Api.Entities.Version1;
    using Telligent.Evolution.Extensibility.Api.Version1;
    
    namespace UserData
    {
        public class UserDataPlugin : Telligent.Evolution.Extensibility.Version1.IPlugin
        {
            public string Name
            {
                get { return "UserDataPlugin"; }
            }
    
            public string Description
            {
                get { return "User Data Plugin"; }
            }
    
    
            public void Initialize()
            {
                Apis.Get<ISearchIndexing>().Events.BeforeBulkIndex += new BeforeSearchBulkIndexingEventHandler(UpdateSortOrder_BeforeBulkIndex);
            }
    
            private void UpdateSortOrder_BeforeBulkIndex(BeforeBulkIndexingEventArgs e)
            {
                UsersGetOptions o = new UsersGetOptions();
                var i = Apis.Get<IUsers>();
                foreach (SearchIndexDocument d in e.Documents)
                {
                    o.ContentId = d.ContentId;
                    var u = i.Get(o);
                    if (u != null)
                    {
                        d.AddField("UserSortOrder", u.ExtendedAttributes["SortOrder"].Value);
                    }
                }
            }
        }
    }

    Here's where I change the sort order in search.vm. Is this correct?

    #set ($searchListOptions = "%{ Query = $searchQuery, Sort = 'UserSortOrder', PageIndex = $pageIndex, PageSize = $pageSize }")

  • In 11.1+, you can go to Administration > Site > Search, view the Include tab, uncheck "Users", Save, recheck "Users", and Save again. This will reindex users.

  • Former Member
    0 Former Member in reply to Ben Tiedt

    Thanks ALL: This is working now. However, I'd like to take it one step further -

    How can I sort the user search results by two fields?

    Also, how can I exclude some users from being included in the index?

  • How can I sort the user search results by two fields?

    The sort can be specified as "FIELD DIRECTION,FIELD DIRECTION,...' where FIELD is the name of the field to sort on and DIRECTION is 'asc' or 'desc'

    Also, how can I exclude some users from being included in the index?

    Users can be excluded by disabling the user's "include in search" option in their settings.