ADAM Search - Use the force

ADAM's powerful search capabilities allow you to express advanced or complex searches in an elegant and succinct way. This high level of expressiveness yields code that is not only more readable but often also more efficient when executed. Let us take a look at a small real-world example to make this point clear.

Suppose you are interested in classifications that are linked to records referencing a certain product name. Your initial approach might be to use a basic search expression to search for those records and then loop through the classifation data:

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
RecordCollection recordCollection = new RecordCollection(application);
recordCollection.Load(new SearchExpression("ProductName"));

List<Guid> classificationIds = new List<Guid>();

foreach (Record record in recordCollection)
{
    IEnumerable<RecordClassification> classifications = record.Classifications;
    classificationIds.AddRange(classifications.Select(c => c.ClassificationId));
}

ClassificationCollection classificationCollection = new ClassificationCollection(application);
classificationCollection.Load(classificationIds.Distinct().ToList());
The problem is of course that this code only works when the number of records matching the search expression is relatively small. And even when the number of records is small, a lot of redundant record data is still being loaded which is not very efficient.

A better approach would be to use a slightly more complex search expression that specifies the objects you are searching for in a more direct way. In fact, you are not searching for records but rather for classifications that are linked to those records. In ADAM 4.5.1, you can use the Record keyword when searching for classifications. The following code obtains the same set of classifications as above but is much more efficient because it avoids loading redundant record data:
C#
1
2
ClassificationCollection classificationCollection = new ClassificationCollection(application);
classificationCollection.Load(new SearchExpression("Record[ProductName]");
Of course more advanced search expressions are also possible. For instance, you might want to obtain not only the classifications directly linked to certain records but also their ancestors:
C#
1
2
ClassificationCollection classificationCollection = new ClassificationCollection(application);
classificationCollection.Load(new SearchExpression("DescendantOrSelf.Record[ProductName]");
Or you might be interested in only those classifications that are child of a certain parent classification:
C#
1
2
ClassificationCollection classificationCollection = new ClassificationCollection(application);
classificationCollection.Load(new SearchExpression("Record[ProductName] Parent = 84bf0913-c13c-4832-b61b-c9e5a3373870");
Bottomline: getting to know ADAM's more advanced search features usually pays off quickly, because you will soon be writing more elegant and efficient code.

Comments

Leave a comment
You must be logged in to post comments.
Sign in now
 
 
Technical
Business
rss feed