I'm experimenting with adding support for conditionally returning
SearchId for Azure search traffic analytics purposes (https://docs.microsoft.com/en-us/azure/search/search-traffic-analytics) as part of Azure search queries made by the Sitecore content search API. Doing so involves passing a custom header to the Azure search service along with the query.
Ideally, this would be something that is conditionally available, say, on the search context object and, if set, the
SearchId property would be hydrated on each search result object. Something like this:
... ctx.IncludeSearchId = true; ... var results = ctx.GetQueryable<FooResult>(); ... var searchId = oneFooResult.SearchId; // e.g. "9acc737d-cf2e-4b4f-8b4f-d70288ce4e77"
I'm only about a third of the way there. I've implemented a custom search service client inheriting from
Sitecore.ContentSearch.Azure.Http.SearchServiceClient and am overriding the
Search(SearchRequest searchRequest) method to always pass the custom
"x-ms-azs-return-searchid" header. The custom search service client is patched in by switching the
"type" attribute on the
<searchServiceClient /> node from
Sitecore.ContentSearch.Azure.Http.SearchServiceClient, Sitecore.ContentSearch.Azure to my custom type. I get a response header containing the
SearchId, as expected.
I'm still missing two key things:
- The ability to set a flag on the search context to conditionally pass the
"x-ms-azs-return-searchid"header with the query (likely by inheriting from
CloudSearchSearchContextand adding a flag property and eventually adding a custom flag property to the
Sitecore.ContentSearch.Azure.Http.Model.SearchRequestobject around which to build an
- Mapping the
SearchIdvalue to a property on each result object to allow the caller to use the property downstream (to push analytics data into Application Insights when results are "clicked").
I don't see an elegant way of adding support Azure search traffic analytics that doesn't involve replacing/copying large swaths of the Sitecore search API.
What is the "correct" way to do something like this? Perhaps it's better to open a Sitecore support ticket and let them tell me?