Home > Software design >  Microsoft.Azure.Search SearchServiceClient.Indexes.Create throws an exception "The request is i
Microsoft.Azure.Search SearchServiceClient.Indexes.Create throws an exception "The request is i

Time:10-05

When calling Create on Microsoft Azure Search, Search Service Client Indexes an exception is thrown. The exception is: "The request is Invalid". This is a console app in C# using .Net Core 3.1. There isn't much discussion on this on the web. What am I doing wrong?

class Program
{
    private static readonly string searchServiceName = "mysearchservice";
    private static readonly string adminApiKey = "<<admin key here>>";
    static void Main(string[] args)
    {
        var serviceClient = CreateSearchServiceClient();
        CreateIndex(serviceClient);
    }

    private static void CreateIndex(SearchServiceClient ssc)
    {
        var definition = new Microsoft.Azure.Search.Models.Index()
        {
            Name = "hotels",
            Fields = FieldBuilder.BuildForType<Hotel>()
        };
        ssc.Indexes.Create(definition); // Throws
    }

    private static SearchServiceClient CreateSearchServiceClient()
    {
        SearchServiceClient serviceClient = 
           new SearchServiceClient(searchServiceName, 
             new SearchCredentials(adminApiKey));
        return serviceClient;
    }
}
 
public class Hotel 
{
    [System.ComponentModel.DataAnnotations.Key]
    [IsFilterable]
    public string HotelId { get; set; }

    [IsSearchable, IsSortable]
    public string HotelName { get; set; }

    [IsSearchable]
    [Analyzer(AnalyzerName.AsString.EnLucene)]
    public string Description { get; set; }

   
    [IsSearchable, IsFilterable, IsSortable, IsFacetable]
    public string Category { get; set; }

    [IsSearchable, IsFilterable, IsFacetable]
    public string[] Tags { get; set; }

    [IsSearchable, IsSortable, IsFacetable]
    public bool? ParkingIncluded { get; set; }


    public DateTimeOffset? LastRenovationDate { get; set; }

    [IsFilterable,IsSortable,IsFacetable]
    public double? Rating { get; set; }

    [IsFilterable, IsSortable]
    public GeographyPoint Location { get; set; }
}

CodePudding user response:

I've faced this problem in the past, it's related to the Json serializer and Geographypoint. The following code should work:

    Uri serviceEndpoint = new Uri($"https://{searchServiceName}.search.windows.net/");

    var adminClient = new SearchIndexClient(serviceEndpoint, new AzureKeyCredential(adminApiKey));

    JsonSerializerOptions serializerOptions = new JsonSerializerOptions
    {
        Converters =
        {
            new MicrosoftSpatialGeoJsonConverter()
        },
        PropertyNamingPolicy = JsonNamingPolicy.CamelCase
    };
    SearchClientOptions clientOptions = new SearchClientOptions
    {
        Serializer = new JsonObjectSerializer(serializerOptions)
    };

    FieldBuilder fieldBuilder = new FieldBuilder
    {
        Serializer = clientOptions.Serializer
    };

    var definition = new SearchIndex(indexName)
    {
        Fields = fieldBuilder.Build(typeof(Hotel))
    };
    adminClient.CreateOrUpdateIndex(definition);

CodePudding user response:

Index fields must be string. When I changed the type from int to string. It succeeds In the exception, it was saying MoveNext in reference to the HttpRequest. The Body had one element, and it contained the error clarification .

  • Related