簡體   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