azure-search-.net-sdk - Azure 搜索 CreateIndexAsync 失败并出现 CamelCase 字段名称 FieldBuilder
问题描述
Azure Search V11
我无法让它工作。但是使用标准 FieldBuilder 会创建索引。
private static async Task CreateIndexAsync(SearchIndexClient indexClient, string indexName, Type type)
{
var builder = new FieldBuilder
{
Serializer = new JsonObjectSerializer(new JsonSerializerOptions {PropertyNamingPolicy = new CamelCaseNamingPolicy()})
};
var searchFields = builder.Build(type).ToArray();
var definition = new SearchIndex(indexName, searchFields);
await indexClient.CreateIndexAsync(definition);
}
`
public class CamelCaseNamingPolicy : JsonNamingPolicy
{
public override string ConvertName(string name)
{
return char.ToLower(name[0]) + name.Substring(1);
}
}
解决方案
请参阅我们的示例。FieldBuilder
基本上,您必须为FieldBuilder
和使用命名策略SearchClient
:
var clientOptions = new SearchClientOptions
{
Serializer = new JsonObjectSerializer(
new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
}),
};
var builder = new FieldBuilder
{
Serializer = clientOptions.Serializer,
};
var index = new SearchIndex("name")
{
Fields = builder.Build(type),
};
var indexClient = new SearchIndexClient(uri, clientOptions);
await indexClient.CreateIndexAsync(index);
await Task.DelayAsync(5000); // can take a little while
var searchClient = new SearchClient(uri, clientOptions);
var response = await searchClient.SearchAsync("whatever");
虽然此示例有效(我们的示例代码来自经常执行的测试),但如果您还有其他问题,请务必发布您收到的确切异常消息。
推荐阅读
- asp.net-mvc - 如何从客户模型中选择所有下订单的客户orderby 购买数量
- replace - 使用带有特殊字符的 JREPL
- ubuntu-18.04 - ROS2 (ros-eloquent-desktop) - Ubuntu 18.04 - 无法纠正问题,你持有损坏的包
- user-interface - 将 FXML 中的扩展自定义类与自定义 ControllerFactory 一起使用
- javascript - 根据html5中相同形式的另一个字段中的数据设置输入的最大长度的方法?
- css - 使用 WayPoint 滚动时如何显示导航栏
- python - Django - 停止自动填充表单文本区域
- python - python while循环在满足设定条件时不会停止
- c# - C#中的多个条件
- node.js - MongoDB:替换数组中的对象