1

Azure Search V11
I can't get this to work. But with the standard FieldBuilder the index is created.

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);
     }
}
CRG
  • 677
  • 1
  • 7
  • 16

1 Answers1

2

See our sample for FieldBuilder. Basically, you must use a naming policy for both FieldBuilder and the 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");

While this sample works (our sample code comes from oft-executed tests), if you have further troubles, please be sure to post the exact exception message you are getting.

Heath
  • 2,986
  • 18
  • 21