繁体   English   中英

Azure 搜索 v11:索引复杂类型的可空集合

[英]Azure Search v11: Indexing nullable Collection of Complex Type

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

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

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

v10 没有出现这个问题。 我发现的一种解决方法是将 collections 设置为空 arrays 而不是 null 值,但我想找到一个更好的解决方案。

已编辑:我从 Microsoft.Azure.Search v10.1.0 升级到 Azure.Search.Documents v11.1.1 以下是通用 T ZA2F2ED4F8EBC2CBB4C21A29DDC4 的示例

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来跟踪解决方案。

You can workaround this issue without initializing your collections to an empty array by passing in a JsonSerializerSettings that was similar to what we did in our older Microsoft.Azure.Search library, since it seems from using the JsonPropertyAttribute you're using Newtonsoft.Json (又名 Json.NET) 无论如何:

  1. 如果您还没有添加 package 对 Microsoft.Azure.Core.NewtonsoftJson 的引用。 它最近 GA'd 所以你不需要使用预览,因为我认为System.Text.Json - 我们的默认序列化程序 - 不会尊重你的属性重命名。

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

     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 class 评论和/或UploadDocuments和相关方法上)以及如何保留以前的行为,那会有所帮助还是令人满意?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM