0 Replies Latest reply on May 9, 2016 4:49 PM by Dan Fishman

    OSIsoft.AF.Search Example

    Dan Fishman

      A number of new AF Search classes have been added.  I took some time this morning to play around with the namespace and wanted to share some sample code to help everyone get started.  I liked that I didn't have to create an AFElemetTemplate object each time I wanted to search using a template; I could just write a query string such as "Template:myTemplate".  The more flexible queries allowed me to complete new searches such as where my Event Frames were in category A and category B. Overall, this new search class provides much more flexibility and with AF 2.8 my searches will be even more complex with the introduction of severity, acknowledgement, and annotations.

       

      Thanks Jerome Lefebvre のブログ for giving me the idea to test this after seeing your recent code.

       

       

                   //Query string, logical AND applied.
                  string query = "Start:>=*-35d InProgress:=False Template:Downtime Category:Temperature";            
                             
                  //use simple search, although searchMode is very useful using other overloads
                  AFEventFrameSearch search = new AFEventFrameSearch(myDB, "search", query);
      
                  //use new mehotd to GetTotalCount() without bringing back all EF headers. Fast and lower memory footprint
                  Console.WriteLine("Total EF match critiera: {0} ", search.GetTotalCount());
      
                  //now actually bring back EF and full load- much more expensive call.
                  IEnumerable<AFEventFrame>  eventFameResults = search.FindEventFrames(0, true, int.MaxValue);
      
      
                  //do work with results...            
      
      
                  //build up a new search using Searchtokens, attribute named "mode" = ramp, and duration greater than 60 minutes                   
                  AFSearchToken token1 = new AFSearchToken(AFSearchFilter.Value, AFSearchOperator.Equal, "Ramp", "|Mode");
                  AFSearchToken token2 = new AFSearchToken(AFSearchFilter.Duration, AFSearchOperator.GreaterThan, "60M");
                  List<AFSearchToken> tokens = new List<AFSearchToken>() { token2, token1 };
                  AFEventFrameSearch search2 = new AFEventFrameSearch(myDB, "search2", tokens);
      
      
                  //take EF found above, and see if they match search2 search query using IsMatch method
                  IEnumerable<AFEventFrame> matchEF = eventFameResults.Where(e => search2.IsMatch(e));