How to search for multiple element names within AFElementSearch's query string?

I keep receiving an error when calling AFElementSearch.FindObjects that involves the passed query string. The query string and error message provided below.

 

Query string:

"Element:IN('45'; '46') Template:'My Template Name' Root:'MyAFServer\MyAFPath'"

 

Error message:

'The search criteria 'Element:IN(45;46)' is not valid for Element search.'

 

I've confirmed the error was not present before the Element-based filter was appended to the beginning of the query string. I had also originally tried filtering on "Name" rather than "Element", but that did not work either. I switched the filter to "Element" after I found an example on the PI AF SDK Search Overview page that used the "Element:IN" condition, but no examples that used "Name:IN".

 

AF SDK Search Overview:

https://docs.aveva.com/bundle/af-sdk/page/html/search-overview.htm

 

  • According to the help link you posted, the Element filter is not supported in an AFElementSearch query:

    Click to enlarge:


    AFElementSearch Filter.png
     

    The Name filter is allowed but not ElementName. However, the Name filter is a strict equals, and cannot use an IN( ) clause. See Search Query Syntax from your own help link:


    2023-11-17 08_22_46.png
     

    That said, you could consider using an OR condition in conjuction with the Name filter, but OR's perform more sluggish than if not using an OR. What I would suggest is first performing a skinny search by using FindObjectFields to return the guid ID and string Name. You can then filter on name for just the few elements that you desire.

     

    Once you have a small set of guid ID's, you can then use another AFElementSearch to FindObjects passing in an ID filter that does allow for IN( ). I would recommend this if you were still expecting a large number of elements matching just the 2 names, and you want to process them in pages.

     

    If you really are just expecting a reasonably small number of elements to be found, then I would grab the guid IDs from the skinny search, and then use the AFElement.FindElements(PISystem, Guid[]) method.

     

  • I have used something like this to find Element Templates:

    Switch the strQuery to the mask you want to use or run several queries if needed.

    string strQuery = "*45*";
    AFNamedCollectionList<AFElementTemplate> elementTemplates = AFElementTemplate.FindElementTemplates(AFDB, strQuery, AFSearchField.Name, AFSortField.Name, AFSortOrder.Ascending, 1000);