skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. According to official samples, you should be able to index Assignments. I do think the issue is around List. Try to make it an array instead:

    from:

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

    to:

    public Assignment[] Assignments { get; set; }
    

    Source:
    https://github.com/Azure-Samples/search-dotnet-getting-started/blob/master/DotNetHowTo/DotNetHowTo/Hotel.cs

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search