Azure search and multilist guid fields

I recently went down the path of learning about Azure Search.  One of the requirements of this project is to be able to search on categories the item might have.  I had originally tackled this problem years ago with Lucene and SOLR inside XBlog.  When dealing with a multilist field of guids you search predicate simply needs to be:   (item => item[“some field”].Contains(myCategoryItem.ID.tostring()) .

But to my surprise, this doesn’t work with azure search.  To add further to the gloom and doom, I found in official Sitecore documentation that this is not suppose to work at all: https://doc.sitecore.net/sitecore_experience_platform/setting_up_and_maintaining/search_and_indexing/support_reference_for_azure_search?roles=developer  .

Specifically this information here:

“Important

You must not apply queries such as StartsWith, Contains to fields with of type EDM.String that contain paths (such as /sitecore/content/home) or collections of GUIDs. This is because Azure Search matches regular expression searches to single words.”

 

Well being the stubborn old programmer that I am, this did not sit well.  I don’t like being told I can’t do something, so I dug deeper to find a solution around this.  First I dug into the Azure search api on the portal site to see exactly how values of my multilist field were being stored, and this what I found:

 

“category_sm”: [

“efc89afd55e04000bbbad30960d4ecb3”,

“ace388700f904d529268219dfa35b55d”

],

 

Turns out when Azure stores a mutilist field of guids, they are stored as separate values and all the special characters are removed from the guid.

So the next step was to find a way to actually search on this.  So I started down the path of some rather rough code.  Just removing all the extra stuff that could cause problems.  To my surprise, the answer was very simple:

 

string id = categoryIncludeItem.ID.ToString();

id = id.Replace(“{“, “”);

id = id.Replace(“}”, “”);

id = id.Replace(“-“, “”);

categoryIncludePredicate = categoryIncludePredicate.And(item => item[“category_sm”].Contains(id.ToLower()));

 

 

There seems to be a few unsolved questions in the community around this, so I hope this helps someone out there also struggling with this concept.  I have yet to vet this more, with more complex search queries, but it appears that basic queries at least work.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s