Azure Search Service 无法映射复杂对象的集合。

huangapple go评论63阅读模式
英文:

Azure Search Service unable to map collection of complex objects

问题

我正在使用Azure Search .NET SDK从Azure SQL数据库中获取数据。

除了其他字段之外,数据库视图返回一个具有以下结构的复杂对象列表:

[
    {
        "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
            }
        ]
    }
]

我用于映射的模型(在.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; }
}

所以,即使在索引器运行时拥有相同的结构,我得到以下错误:

JSON数组与元素类型 'Object' 映射到 'Collection(Edm.ComplexType)'。期望的类型为 'Edm.ComplexType'

我猜测索引器将数据库的JSON列表识别为 Object,无法映射到列表(Collection),但我不明白为什么以及如何解决这个问题。

有人遇到过相同的问题吗?

编辑:

Azure SQL数据库没有内置的数据类型可以自然地映射到Azure Cognitive Search中的 Collection(Edm.String) 字段

所以我必须使用 FieldMappingFunction,但根据我发现的信息,我只能使用它们定义的函数。它们中没有一个在我的情况下有用,因为它们有 jsonArrayToStringCollection,但我需要 jsonArrayTo<COMPLEX>Collection

英文:

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:

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

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

public class Entity {
   ...
   public List&lt;Assignment&gt;? 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 &#39;Object&#39; map to Collection(Edm.ComplexType)&#39;). The expected type was &#39;Edm.ComplexType&#39;

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&lt;COMPLEX&gt;Collection

答案1

得分: 0

根据官方示例,你应该能够对Assignments进行索引。我认为问题出在List<T>上。尝试将其改为数组:

从:

public List&lt;Assignment&gt;? Assignments { get; set; }

改为:

public Assignment[] Assignments { get; set; }
英文:

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

from:

public List&lt;Assignment&gt;? 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

答案2

得分: 0

我已成功解决了这个问题。默认情况下(即使您使用SELECT TOP 1),FOR JSON AUTO/PATH 子句会用方括号包围结果。所以,在我的情况下,我期望得到的是 &quot;Attribute&quot;: {&quot;Id&quot;: 141, &quot;Name&quot;: &quot;Inspection date&quot;, &quot;DataTypeId&quot;: 5 },就像我在 Attribute 类中已经有的那样。

英文:

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 &quot;Attribute&quot;: [{&quot;Id&quot;: 141, &quot;Name&quot;: &quot;Inspection date&quot;, &quot;DataTypeId&quot;: 5 }] I expected to have &quot;Attribute&quot;: {&quot;Id&quot;: 141, &quot;Name&quot;: &quot;Inspection date&quot;, &quot;DataTypeId&quot;: 5 } as I already had in the Attribute class.

huangapple
  • 本文由 发表于 2023年3月3日 19:32:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626551.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定