7 Replies Latest reply on Feb 16, 2018 4:06 PM by Rick Davin

    Event Fame search using AFSearch for a template over a subset of elements

    Lonnie Bowling

      Hi Everyone,

       

      I’m hitting a snag with searching for event frames (using AFSearch). I have a  template setup for my event frame and it is used on many elements. I need to do a search where I get only the events associated with a subset of the elements.

       

      It is easy to get all of them by using a template search, and I can get one by using a name search with a wild card (the element path is part of the event name). But I’m pulling my hair out with a good way to search for events associated with several elements. I know that I cannot use an OR operation, which would make life wonderful.

       

      Also, note that I’m doing a search with index/paging, from most recent to oldest, so that needs to work. Doing a search for each element, one at a time, is really not a good option, as I would have to pull in a ton of events then sort, then return only x number that were requested.

       

      I’m thinking that I pull in some events than I need using a template search, then filter out the elements that I don’t need, check to make sure I have enough events to return, if not then do another search to get more events, until I get the number of events I need. Paging will be a mess, but it seems like the only way.

       

      Any ideas?

       

      Thanks,

       

      Lonnie

        • Re: Event Fame search using AFSearch for a template over a subset of elements
          Rick Davin

          Hi Lonnie,

           

          Glad to see you back in the forums.  I'm in meetings most of this week, but wanted to start a dialog with you about this topic.  What version of AF SDK are you using?  When you say elements, let's clarify that you mean referenced elements on an event frame, right?  Are the elements based on a template as well?

           

          In AF SDK 2.9.5 released in December 2017, there is new support for nested queries, so you can specify a template (or other filters) for a nested element query, and then a template yet again (plus other filters) on AFEventFrameSearch query.

          1 of 1 people found this helpful
            • Re: Event Fame search using AFSearch for a template over a subset of elements
              Lonnie Bowling

              Hi Rick,

               

              Thanks for the response. I'm using 1 version back from that; 2.9.2. I'll load up 2.9.5 and check it out, that would be so nice if it works for me. Yes, by elements, I mean referenced elements, and everything is based on templates. I will post again after I look at the new SDK. Don't know how I missed that release, guess I have been hiding under a rock for too long!

               

              Thanks,

               

              Lonnie

                • Re: Event Fame search using AFSearch for a template over a subset of elements
                  Rick Davin

                  Hi Lonnie,

                   

                  I don't have an example to match exactly what you want to do, but I do have an example illustrating nested tokens using the new AFAttributeSearch.  I am sure you will be able to apply to your situation.  Instead of searching on 2 templates (1 for EF and 1 for elements), I will be searching on 2 names (1 for attribute and 1 for element).  The main thing is notice the syntax.  All 3 examples do the same thing but do it differently.  The first uses AFSearchToken, the second uses a plain query string, and the third uses an Interpolated string.

                   

                  // Goal is to find attributes named "Feed Rate" on any elements named "Boiler*"
                  
                  // Finds 1 attribute per element, using nested query tokens.
                  // New to AF 2.9.5: AFSearchToken.Tokens property.
                  public static void FindAttributeDemo21(AFElement root)
                  {
                      // Set up the nested element search tokens
                      var nestedTokens = new List<AFSearchToken>();
                      nestedTokens.Add(new AFSearchToken(AFSearchFilter.Name, "Boiler*"));
                  
                  
                      // Set up the outer attribute search tokens, which use the nested tokens
                      var tokens = new List<AFSearchToken>();
                      // The Element uses the nested token(s)
                      tokens.Add(new AFSearchToken(AFSearchFilter.Element, AFSearchOperator.Equal, null, nestedTokens));
                      // The Attribute uses a simple Name search
                      // NOTE 'Feed Rate' is the attribute name, whereas '|Feed Rate' is the path (relative to the element).
                      tokens.Add(new AFSearchToken(AFSearchFilter.Name, "Feed Rate"));
                  
                      using (var search = new AFAttributeSearch(root.Database, "attr demo 2.1", tokens))
                      {
                          search.CacheTimeout = TimeSpan.FromMinutes(10);
                          foreach (var item in search.FindAttributes())
                          {
                              // do something
                          }
                      }
                  }
                  
                  // Same as above but using nested { } in query strings.
                  public static void FindAttributeDemo22(AFElement root)
                  {
                      // NOTE how element has nested { }. 
                      var query = "Element:{Name:'Boiler*'} Name:'Feed Rate'";
                  
                      using (var search = new AFAttributeSearch(root.Database, "attr demo 2.2", query))
                      {
                          search.CacheTimeout = TimeSpan.FromMinutes(10);
                          foreach (var item in search.FindAttributes())
                          {
                              // do something
                          }
                      }
                  }
                  
                  // Same as above but using nested { } in Interpolated query strings.
                  public static void FindAttributeDemo23(AFElement root)
                  {
                      string elemName = "Boiler*";
                      string attrName = "Feed Rate";
                  
                      // This gives a compile error with an Interpolated string
                      //var query = $"Element:{Name:'{elemName}'} Name:'{attrName}'";
                  
                      // Escape the { and } around literal braces with {{ and }} 
                      var query = $"Element:{{Name:'{elemName}'}} Name:'{attrName}'";
                  
                      using (var search = new AFAttributeSearch(root.Database, "attr demo 2.3", query))
                      {
                          search.CacheTimeout = TimeSpan.FromMinutes(10);
                          foreach (var item in search.FindAttributes())
                          {
                              // do something
                          }
                      }
                  }
                  

                   

                  To help round out the discussion, here are some links:

                   

                  What's New AF 2017 R2 (AF SDK 2.9.5)

                   

                  Search Overview - strongly suggest you BOOKMARK this link

                   

                  AFSearchToken constructor with nested tokens

                  2 of 2 people found this helpful
                  • Re: Event Fame search using AFSearch for a template over a subset of elements
                    Rick Davin

                    It was easy to miss since it was released on December 27 during busy holiday season.

                • Re: Event Fame search using AFSearch for a template over a subset of elements
                  Rick Davin

                  Hi Lonnie,

                   

                  One very important detail about nested queries that I left out earlier, you will need to upgrade both the AF Client and AF Server.  Specifically, your AF Server must support the PISystemFeatures.QuerySearchAttribute feature.  That particular enumeration indicates that your AF Server supports new search features added to AF 2.9.5:

                   

                  • AFAttributeSearch
                  • AFNotificationContactTemplateSearch
                  • Nested queries
                  • New search tokens for EventFrame, IsInternal, Parent, PlugIn, and PlugInName.

                   

                  I know in a private conversation that you are interested in the Parent filter.

                  1 of 1 people found this helpful