Skip to content

Instantly share code, notes, and snippets.

@ealsur
Last active February 16, 2018 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ealsur/11fc26ce47a558b2fa85b5b64f1f76c3 to your computer and use it in GitHub Desktop.
Save ealsur/11fc26ce47a558b2fa85b5b64f1f76c3 to your computer and use it in GitHub Desktop.
Azure Cosmos DB + Functions Cookbook - search indexing run.csx
#r "Microsoft.Azure.Documents.Client"
using System;
using System.Configuration;
using System.Collections.Generic;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Search;
private static string searchServiceName = ConfigurationManager.AppSettings["SearchServiceName"];
private static string searchServiceKey = ConfigurationManager.AppSettings["SearchServiceKey"];
private static SearchServiceClient serviceClient = new SearchServiceClient(searchServiceName, new SearchCredentials(searchServiceKey));
private static ISearchIndexClient indexClient = serviceClient.Indexes.GetClient(ConfigurationManager.AppSettings["SearchServiceIndexName"]);
public class IndexItem {
public string id {get;set;}
public string name {get;set;}
public int age {get;set;}
}
public static async Task Run(IReadOnlyList<Document> documents, TraceWriter log)
{
log.Verbose("Documents modified " + documents.Count);
if (documents != null && documents.Count > 0)
{
var batch = Microsoft.Azure.Search.Models.IndexBatch.MergeOrUpload(documents.Select(
// Do any transformation needed
doc => new IndexItem(){
id = doc.GetPropertyValue<string>("id"),
name = doc.GetPropertyValue<string>("name"),
age = CalculateAge(doc.GetPropertyValue<string>("born"))
}
));
try
{
await indexClient.Documents.IndexAsync(batch);
}
catch (IndexBatchException e)
{
// Sometimes when your Search service is under load, indexing will fail for some of the documents in
// the batch. Depending on your application, you can take compensating actions like delaying and
// retrying. For this simple demo, we just log the failed document keys and continue.
log.Error(
string.Format("Failed to index some of the documents: {0}",
String.Join(", ", e.IndexingResults.Where(r => !r.Succeeded).Select(r => r.Key))));
log.Error(e.Message);
}
}
}
private static int CalculateAge(string born){
DateTime bday = DateTime.ParseExact(born,"yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
DateTime now = DateTime.Today;
var age = now.Year - bday.Year;
if (bday > now.AddYears(-age)) age--;
return age;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment