azureazure-cognitive-searchazure-search-.net-sdk

Azure Search Service unable to map collection of complex objects


I'm using Azure Search .NET SDK to ingest data from Azure SQL Database.

Besides other fields, the database View returns a list of complex objects with the following structure:

[
    {
        "AttributeId": 133,
        "Value": "07.02.2023",
        "Attribute": [
            {
                "Id": 133,
                "Name": "Date",
                "DataTypeId": 4
            }
        ]
    },
    {
        "AttributeId": 141,
        "Value": "28.01.2023 06:59",
        "Attribute": [
            {
                "Id": 141,
                "Name": "Inspection date",
                "DataTypeId": 5
            }
        ]
    }
]

And the model I use for mapping (in .NET):

public class Entity {
   ...
   public List<Assignment>? Assignments { get; set; }
}

public class Assignment
{
    public int AttributeId { get; set; }

    public string Value { get; set; }

    public Attribute Attribute { get; set; }
}

public class Attribute
{
    public int Id { get; set; }

    public string Name { get; set; }

    public int DataTypeId { get; set; }
}

So, even if I have the same structure when the indexer is running the tries to ingest the data, I get the following error:

JSON arrays with element type 'Object' map to Collection(Edm.ComplexType)'). The expected type was 'Edm.ComplexType'

I presume that the indexer recognizes the database JSON list as an Object and cannot be mapped to List (Collection), but I can't understand why and how I can fix this problem.

Has anyone faced the same problem?

Edited:

Azure SQL Database doesn't have a built-in data type that naturally maps to Collection(Edm.String) fields in Azure Cognitive Search

So I have to use a FieldMappingFunction but from what I found, I can use only their defined functions. None of them are useful in my case because they have jsonArrayToStringCollection but I need jsonArrayTo<COMPLEX>Collection


Solution

  • I've managed to fix the problem. By default (even if you are using SELECT TOP 1), the FOR JSON AUTO/PATH clause surrounds the result with square brackets. So, in my case instead of "Attribute": [{"Id": 141, "Name": "Inspection date", "DataTypeId": 5 }] I expected to have "Attribute": {"Id": 141, "Name": "Inspection date", "DataTypeId": 5 } as I already had in the Attribute class.