Advanced Database Crawler and Dynamic Fields

The latest version of the Advanced Database Crawler project, now called SitecoreSearchContrib, https://github.com/sitecorian/SitecoreSearchContrib, comes with useful features such as sorting built-in.  Sorting is one feature that was not present in the original version of the ADC.  For some of the projects I maintain I often implement dynamic fields to be able to manipulate my SkinnyItem list of search results without having to get the actual Sitecore Item for each SkinnyItem.  If you have a large list of SkinnyItems it can take a while to get all of the Sitecore Items for each.  If you’ve been converting your lists of SkinnyItem into Sitecore Items for the purposes of filtering and sorting keep reading, this is not necessary!

Let’s say you have a list of SkinnyItems that represent attorneys.  And let’s say you need to sort this list by last name before displaying to the user.  One way would be to get the Sitecore Item for each SkinnyItem, access the Fields property for each Item and OrderBy that the Last Name field.  Another way is to use a dynamic field and never have to convert the SkinnyItems to Sitecore Items.

First, define the dynamic field in your Sitecore.SharedSource.Search.config file adding a dynamicField node in the dynamicFields section:

<dynamicField type="MyProject.Search.DynamicFields.StartDate, MyProject" name="_lastname"
storageType="YES" indexType="UN_TOKENIZED" vectorType="NO" boost="1f" />

Next, create the C# Class referenced above.  You can place this file anywhere you want in the solution.  I like to follow the general structure the ADC project follows.  Your class needs to do only two things:

  1. Inherit the Sitecore.SharedSource.Search.DynamicFields.BaseDynamicField class
  2. Override the ResolveValue method.

Here’s how you override the ResolveValue method, it’s really simple:

public override string ResolveValue(Item item) {
    Assert.ArgumentNotNull(item, "item");
    return item["Last Name"];
}

“Last Name” is the name of the field you are creating a dynamic field for.

Once you got all of this setup rebuild your solution and rebuild your indexes.

Now, to use this dynamic field to manipulate your list of SkinnyItems, you can do something like this:

var searchParam = new SearchParam {
    Language = language,
    TemplateIds = templateFilter,
    LocationIds = locationFilter,
    FullTextQuery = fullTextQuery,
    ShowAllVersions = false
};

var sortedSkinnyItems = null;

using (var searcher = new Searcher(indexName)) {
    sortedSkinnyItems = searcher.GetItems(searchParam).OrderBy(si => si.Fields["_lastname"]);
}

If you’re wondering how this works, I’ll be honest, I’m not completely clear on that either.  My understanding is that the values for every field are in the Lucene index already.  When you setup a dynamic field you are simply setting up a key with which you’ll be able to access the data in the index for each SkinnyItem.  In my use of dynamic fields I have not noticed significant increases in the index size.

One thought on “Advanced Database Crawler and Dynamic Fields

Leave a Reply