gpt4 book ai didi

c# - Azure 搜索 v11 : Indexing nullable Collection of Complex Type

转载 作者:行者123 更新时间:2023-12-05 06:06:16 24 4
gpt4 key购买 nike

我正在将 Azure 认知搜索服务的 SDK 从 v10 更新到 v11。我已按照指南中的所有步骤进行升级,但是我注意到有关索引(合并或上传)操作的奇怪行为:UploadDocumentAsync(但也使用其他用于索引数据的方法)当 Collection (Edm.ComplexType) 类型的属性为 null 时,操作失败,并出现以下错误:

尝试读取属性内容时,从 JSON 读取器读取了“PrimitiveValue”类型的节点。但是,“StartArray”节点应为 json。

IndexDocumentsResult response = await searchClient.UploadDocumentsAsync<T>(documents).ConfigureAwait (false);

在 v10 中,这个问题没有出现。我发现的解决方法是将集合设置为空数组而不是空值,但我想找到更好的解决方案。

编辑:我从 Microsoft.Azure.Search v10.1.0 升级到 Azure.Search.Documents v11.1.1以下是用于索引数据的通用 T 类的示例:

public class IndexEntity
{
[JsonProperty("@search.score")]
public double SearchScore { get; set; }

[JsonProperty("Key")]
public Guid Id { get; set; }

[JsonProperty("Code")]
public string Code { get; set; }

[JsonProperty("ComplexObj")]
public ComplexType[] CollectionOfComplexType{ get; set; }

}

遵循ModelObjectToIndex的定义

public class ComplexType
{
[JsonProperty("Id")]
public string Id { get; set; }

[JsonProperty("Value")]
public string Value { get; set; }
}

基本上,当 CollectionOfComplexType 属性为 null 时,我会收到上述错误。如果我将其设置为空数组,则不会发生错误,但正如前面提到的,我不喜欢这个解决方案,而且在旧版本中这是允许的操作(索引已成功完成)

最佳答案

我们的 Azure.Search.Documents 行为在这方面似乎已经发生了变化。我已经打开https://github.com/Azure/azure-sdk-for-net/issues/18169跟踪分辨率。

您可以通过传入 JsonSerializerSettings 来解决此问题,而无需将集合初始化为空数组,这与我们在旧版 Microsoft.Azure.Search 库中所做的类似,因为它似乎是使用无论如何,您正在使用 Newtonsoft.Json (又名 Json.NET)的 JsonPropertyAttribute:

  1. 添加对 Microsoft.Azure.Core.NewtonsoftJson 的包引用(如果尚未添加)。它最近进行了 GA,因此您不需要使用预览,我认为这是因为 System.Text.Json - 我们的默认序列化程序 - 不会尊重您的属性重命名。 p>

  2. 在创建 SearchClient 之前传入 JsonSerializerSettings,如下所示:

    var settings = new JsonSerializerSettings
    {
    // Customize anything else you want here; otherwise, defaults are used.
    NullValueHandling = NullValueHandling.Ignore,
    };

    var options = new SearchClientOptions
    {
    Serializer = new NewtonsoftJsonObjectSerializer(settings),
    };

    var searchClient = new SearchClient(options);

如果可以的话,我们将讨论如何默认解决此问题。与旧库相比的一大变化是能够自定义所使用的序列化器。默认情况下,我们使用 System.Text.Json,但我们支持其他序列化程序,包括 Newtonsoft.Json。如果有人要传递自己的设置 - 或者甚至想要默认值 - 更改这可能是灾难性的。所以我很好奇:如果我们至少记录了这种行为变化(也许在 SearchClient 类注释和/或 UploadDocuments 和相关方法上)以及如何保留以前的行为,这会有所帮助还是令人满意?

关于c# - Azure 搜索 v11 : Indexing nullable Collection of Complex Type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65823922/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com