Azure Cosmos DB + Functions Cookbook — search indexing

Matías Quaranta
Microsoft Azure
Published in
3 min readFeb 16, 2018

For this next Azure Cosmos DB + Azure Functions Cookbook recipe, we’ll be adding a new ingredient to the mix, Azure Search, Azure’s Search-as-a-Service offering.

Scenario

You are using (or want to provide your application) with Azure Search’s great search capabilities and want to push any change in your database as soon as it happens to a Search Index.

While Azure Search does have an Indexer for Azure Cosmos DB, this recipe will allow you to customize and pre-process the data before it reaches Azure Search and also change the model from pull to push.

Ingredients

Like in our previous post, we’ll be using the Cosmos DB Trigger to kickoff our Function execution.

In addition, we’ll be using the Azure Search SDK to handle all the index operations and for performance reasons (as we saw in the first post of this series) we’ll be maintaining it as a static resource.

Recipe

Since Azure Search SDK does not come as part of the Azure Functions core packages, we need to add it manually. In order to do that, we create a file named project.json in the Function:

adding a project.json file

Within, we can define Nuget packages like so:

Then, we can configure our Trigger in the function.json file to use our connection string, database and collection name to listen for changes (remember to read the previous post for more information on each attribute):

Now, we’ll work with a sample document that’s going to have an id , a name and a born attribute that marks the date of birth:

Our Azure Search Index has similar attributes but instead of the date of birth, it contains just the age:

Azure Search Index definition

So now, our Azure Function’s code will read any new or updated document in our collection, calculate the age and send it to Azure Search for indexing. This is the run.csx file:

Notice how we are maintaining the ISearchIndexClient as a static to be shared by all executions. All credentials and configurations for the Azure Search service are being pulled from the Configuration Settings by ConfigurationManager . We are using MergeOrUpload to either upload or update the documents based on the matching id property.

Error handling is important, since the operation can have partial successes, so the catch block will help you identify failing documents to analyze.

After this, any change made to the documents will be processed, transformed and pushed to Azure Search to maintain your index updated!

--

--

Matías Quaranta
Microsoft Azure

Software Engineer @ Microsoft Azure Cosmos DB. Your knowledge is as valuable as your ability to share it.