Azure Cosmos DB + Functions Cookbook — search indexing
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:
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:
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!
Stay tuned for more recipes!
Other posts in this series:
- Azure Cosmos DB + Functions Cookbook — static client
- Azure Cosmos DB + Functions Cookbook — HTTP querying
- Azure Cosmos DB + Functions Cookbook — output collector
- Azure Cosmos DB + Functions Cookbook — live migration
- Azure Cosmos DB + Functions Cookbook — secure client
- Azure Cosmos DB + Functions Cookbook — multi trigger
- Azure Cosmos DB + Functions Cookbook — Connection modes
- Azure Cosmos DB + Functions Cookbook — Multi master & preferred region
- Azure Cosmos DB + Functions Cookbook — Shared throughput and new health logs
- Azure Cosmos DB + Functions Cookbook — monitoring Trigger pending work