我想让 Azure-Search-Index 与存储在 Azure-DocumentDB 中的文档保持同步。索引器可以完成这项工作,但这不适合我,因为需要更新的不仅仅是搜索索引,而且从 DocumentDB 结构到索引方案的映射非常复杂。
我可以使用 DocumentDB 触发器来执行此操作吗?
(执行 http 调用 - 可能是对 Azure 函数 - 获取更改后的 DocumentDB 文档并调用 Azure 搜索服务来更新搜索索引)
最佳答案
您可以挂接 Cosmos DB Trigger要监听集合中的更改,请在 Azure 函数中处理这些更改并将它们发送到 Azure 搜索。
例如,下一个函数监听文档,对年龄进行一些简单的计算并将批处理发送到搜索:
示例文档:
{
"name": "john",
"born": "1983-05-07",
"id": "some-id"
}
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 void 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
{
indexClient.Documents.Index(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;
}
您需要添加 Azure Search Nuget包,为此,将 project.json
文件添加到您的 Azure Function:
{
"frameworks": {
"net46":{
"dependencies": {
"Microsoft.Azure.Search": "3.0.5"
}
}
}
}
关于azure - 更新 documentDb 更改的搜索索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48829509/