0

I've created a custom index to store users in based on the code found at https://gist.github.com/jorgelusar/150a607dcf96acb54b60, but because there's no Sitecore item to trigger the usual indexing strategies on, I'm not seeing a built-in way to update the index when a record is added or edited. Originally I thought of scheduling a nightly refresh agent, but I'd like something more real-time if possible.

I found this command that I thought might do the trick: ContentSearchManager.GetIndex("users_index").Refresh(new IndexableUser(indexedUser)); and it does indeed add the new record, but it also proceeds to duplicate the records that are already in the index. It looks like this is because when the Refresh command is called, it runs the GetItemsToIndex function (pulled from the gist below for reference):

        protected override IEnumerable<IndexableUser> GetItemsToIndex()
        {
            var list = new List<IndexableUser>();
            var membershipUsers = Membership.GetAllUsers();
            foreach (MembershipUser membershipUser in membershipUsers)
            {
                var user = User.FromName(membershipUser.UserName, false);
                var indexedUser = new IndexedUser
                {
                    Id = membershipUser.ProviderUserKey is Guid ? (Guid) membershipUser.ProviderUserKey : Guid.Empty,
                    UserName = membershipUser.UserName,
                    FullName = user.Profile.FullName,
                    Email = user.Profile.Email,
                    Comment = user.Profile.Comment,
                    Roles = user.Roles.Any() ? string.Join(",", user.Roles.Select(x => x.Name)) : string.Empty,
                    Domain = user.Domain.Name
                };

                list.Add(new IndexableUser(indexedUser));
            }

            return list;
        }

This functionality uses the FlatDataCrawler as the base type. What I'd like to do is have some way to register adds/updates and only process those changes if possible. I thought this could work:

        ISearchIndex index = ContentSearchManager.GetIndex("users_index");

        using (IProviderSearchContext context = index.CreateSearchContext()) {
            context.Index.Operations.Add(indexUser, index.CreateUpdateContext(), index.Configuration);
            context.Index.Refresh(indexUser);
        }

This seems to register the user as a "add" but then the Refresh command once again would essentially call a full rebuild given its current setup. Is there a way to check a queue or the like for records to be added/updated/deleted and process just those as incremental changes?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.