How can I convert float to double while generating an index in AzureAISearch?

92 Views Asked by At

I am using .NET SDK to create an index in Azure AI Search Service. I have multiple float fields in the model to index :

[SimpleField(IsFilterable = true, IsSortable = true, IsFacetable = true)]
public float? MonitoringFee { get; set; }

And I'm getting this error:

{"error":{"code":"InvalidRequestParameter","message":"The request is invalid. Details: definition : The field 'MonitoringFee' cannot specify a field type 'Edm.Single' because it is not supported.","details":[{"code":"InvalidField","message":"The field 'MonitoringFee' cannot specify a field type 'Edm.Single' because it is not supported. Parameters: definition"}]}}

The problem seems to be that the index does not support Edm.Single (float). So, I needed to convert the float fields to double.

I have tried using Custom FloatToDouble JsonSerializer in FieldBuilder and SearchIndexClient. I've also tried the following annotation on the field as well: [JsonConverter(typeof(FloatToDoubleConverter))]

Both of the above tries were fruitless.

Note: I cannot change my model datatypes.

1

There are 1 best solutions below

2
Suresh Chikkam On BEST ANSWER

The problem seems to be that the index does not support Edm.Single (float). So, I needed to convert the float fields to double.

Then try to handle the conversion before indexing the data. Before sending the data to Azure AI Search for indexing, transform the float fields to double in your application code.

  • Here I have implemented a custom approach for serialization and deserialization.
// Define a custom model class that mirrors your existing model but with double fields
public class CustomModel
{
    [SimpleField(IsFilterable = true, IsSortable = true, IsFacetable = true)]
    public double? MonitoringFee { get; set; }
    // Add other fields as needed
}

// Method to transform your existing model to the custom model with double fields
public CustomModel TransformModel(Model model)
{
    return new CustomModel
    {
        MonitoringFee = (double?)model.MonitoringFee,
        // Transform other fields as needed
    };
}

// Retrieve your data from the source (assuming a list of Model objects)
List<Model> dataList = GetDataFromSource();

// Transform each model object to the custom model with double fields
List<CustomModel> transformedData = dataList.Select(TransformModel).ToList();

// Create an instance of the SearchIndexClient
SearchIndexClient indexClient = new SearchIndexClient("<search-service-name>", "<index-name>", new SearchCredentials("<api-key>"));

// Assuming you have a method to convert your custom model to documents
var documents = transformedData.Select(ConvertToDocument);

// Assuming you have a method to index documents
IndexBatch<Document> batch = IndexBatch.Upload(documents);
indexClient.Documents.Index(batch);
  • Before indexing, transform each model object from the existing data to the custom model with double fields. Then, you can proceed with indexing the transformed data as usually.

Here are some sample data for testing:

List<Model> sampleData = new List<Model>
{
    new Model { MonitoringFee = 10.5f },
    new Model { MonitoringFee = 20.3f },
    new Model { MonitoringFee = 15.7f }
};

enter image description here

With this ModelTransformer class, you can easily transform all float fields to double fields in any model class:

// Usage example:

// Assuming you have a list of your model objects
List<YourModel> models = GetYourModels();

// Transform all float fields to double fields using the ModelTransformer
var transformedModels = ModelTransformer.TransformFloatsToDoubles(models);

// Now, use the transformedModels to index into Azure Cognitive Search