Copied to clipboard

Flag this post as spam?

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


  • Karlo Medallo 14 posts 36 karma points
    Mar 07, 2014 @ 00:37
    Karlo Medallo
    1

    How to Add Member Group in Examine Index

    Hi,

    I'm currently using the Internal Member Index but I need to have the member group in the index as well. Any suggestions?

    Thanks,

    Karlo

  • Mark 122 posts 255 karma points
    Feb 19, 2015 @ 15:29
    Mark
    1

    Out of curiosity, did you ever find a way to do this?

    I'm thinking of writing a custom GatheringNodeData event to handle it as it doesn't seem obvious to do it any other way. It's quite an overhead hitting the db each time I want to query if a member is in a particular group..

    Thanks,

    Mark

  • Josh Olson 79 posts 207 karma points
    Apr 01, 2015 @ 12:15
    Josh Olson
    0

    Did either of you guys solve this issue? I am now stuck trying to figure the same thing out!

    Cheers.

  • Mark 122 posts 255 karma points
    Apr 01, 2015 @ 12:33
    Mark
    4

    Hi Josh,

    In the end I went with the GatheringNodeData solution to add to an index:

    ExamineManager.Instance.IndexProviderCollection["InternalMemberIndexer"].GatheringNodeData += ExamineMemberEventHandler_GatheringNodeData;
    void ExamineMemberEventHandler_GatheringNodeData(object sender, IndexingNodeDataEventArgs e)
            {
                foreach (KeyValuePair<string, string> keyValuePair in new Dictionary<string, string>(e.Fields))
                {
                    string key = keyValuePair.Key;
                    string value = keyValuePair.Value;
                    if (key != "id") return;
    
                    var memberGroupNameList = new HashSet<string>();
                    var thisMember = new Member(Convert.ToInt32(value));
                    var thisMemberGroups = thisMember.Groups.Values;
                    foreach (MemberGroup group in thisMemberGroups)
                    {
                        memberGroupNameList.Add("\"" + group.Text + "\"");
                    }
                    e.Fields.Add("_memberGroups", string.Join(",", memberGroupNameList));
                }
            }

     

    I have in my mind that it was necessary to change the analyzer, but my code appears to be using the StandardAnalyzer, so maybe it wasn't an issue in the end. It had something to do with spaces between the comma's, so make sure there aren't any when putting the groups into Examine.You can then query the index as normal. I wrote a couple of helper methods to return a members groups and to return all members within groups etc.

    Hope that helps!

    Mark

  • Josh Olson 79 posts 207 karma points
    Apr 01, 2015 @ 13:53
    Josh Olson
    1

    Mark, that looks like exactly what I need. I don't suppose you could share those helper methods too? Pretty please?

    Cheers!

  • Mark 122 posts 255 karma points
    Apr 01, 2015 @ 14:50
    Mark
    3

    Haha..

    well, I can share what I used, though you will want to adapt them to your needs:

    /// Return a list of member groups for a given Member Id
    public
    static List<string>MemberGroups(int memberId) { var searcher = ExamineManager.Instance.SearchProviderCollection["InternalMemberSearcher"]; var searchCriteria = searcher.CreateSearchCriteria(); var query = searchCriteria.Field("id", memberId.ToString()); var compiledQuery = query.Compile(); var results = (Examine.LuceneEngine.SearchResults)searcher.Search(compiledQuery); var returnList = new List<string>(); foreach(var result in results) { if (result.Fields.Keys.Contains("_memberGroups")) { return result.Fields["_memberGroups"].ToString().Split(',').ToList(); } } return returnList; }  
              /// Returns a list of MyCustomMemberClass for any given member group names (
    public static List<MyCustomMemberClass> GetCustomerMembersByGroups(IEnumerable<string> groupNames) { var searcher = ExamineManager.Instance.SearchProviderCollection["InternalMemberSearcher"]; var searchCriteria = searcher.CreateSearchCriteria(); var query = searchCriteria.GroupedOr(new string[] { "_memberGroups" }, groupNames.ToArray()).And().Field("nodeTypeAlias", "customer"); var compiledQuery = query.Compile(); var results = (Examine.LuceneEngine.SearchResults)searcher.Search(compiledQuery); var memberList = new List<MyCustomMemberClass>();
    if (results.Count() > 0) { foreach (var result in results) { if (result.Fields.Keys.Contains("_memberGroups")) { memberList.Add(new MyCustomMemberClass { Id = Convert.ToInt32(result.Fields["id"].ToString()), LoginName = result.Fields["loginName"].ToString() }); } } } return memberList; }
    public class MyCustomMemberClass
    { public int Id { get; set; } public string LoginName { get; set; } }

    Don't for get to include the usings:

    using Examine.SearchCriteria;
    using Examine.Providers;
    using Examine.Config;
    using UmbracoExamine;
    using Examine;


    Hopefully you'll find these useful.

    :-)

    Mark

  • Josh Olson 79 posts 207 karma points
    Apr 01, 2015 @ 14:59
    Josh Olson
    0

    Thanks Mark! This looks great. It will definitely get me 99% of the way there. Just need to adapt it a bit and it will be perfect.

    Cheers!

    **First couple round of the beer of your choice are on me if you are ever passing through Slovenia!

  • Josh Olson 79 posts 207 karma points
    Apr 02, 2015 @ 08:30
    Josh Olson
    1

    Hey Mark! I got everything going thanks to your great help with the code sample! Wish I could mark your answer as a solution, but I didn't originate the post so can't help there.

    Cheers!

  • lucuma 261 posts 563 karma points
    Jan 12, 2016 @ 15:01
    lucuma
    1

    Thank you Mark. Not only did it help with the exceedingly slow member queries I'm doing in a business layer, but getting the groups was a bonus. Besides adapting it for my situation, I am rebuilding the index on app start (seemed to be an issue with existing data not being indexed).

    ExamineManager.Instance.IndexProviderCollection["InternalMemberIndexer"].GatheringNodeData += ExamineMemberEventHandler_GatheringNodeData;
    ExamineManager.Instance.IndexProviderCollection["InternalMemberIndexer"].RebuildIndex();
    
Please Sign in or register to post replies

Write your reply to:

Draft