2017-05-25 35 views
0

我正在使用Azuren搜索和Azure表存儲和.net,我試圖索引表並使分區鍵現在可過濾這工作正常,直到我嘗試插入東西在那裏我得到一個BadRequest沒有太多額外的信息。 這是我的課波紋管當嘗試覆蓋分區鍵使用Azuren搜索和Azure表存儲與.NET變糟的請求

using System; 
using Microsoft.Azure.Search; 
using Microsoft.Azure.Search.Models; 
using Microsoft.WindowsAzure.Storage.Table; 

     [SerializePropertyNamesAsCamelCase] 
    public class Asset : TableEntity 
    { 
     public Asset(string name) 
     { 
      Name = name; 
     } 

     public Asset() 
     { 

     } 

     public Asset(string name, DateTimeOffset toBePublished, string pkey) 
     { 
      Name = name; 
      ToBePublishedDate = toBePublished; 
      PartitionKey = pkey; 
     } 

     [System.ComponentModel.DataAnnotations.Key] 
     public string Id { get; set; } = DateTimeOffset.UtcNow.ToString("O") 
      .Replace("+", string.Empty) 
      .Replace(":", string.Empty) 
      .Replace(".", string.Empty); 

     [IsFilterable, IsSortable, IsSearchable] 
     public new string PartitionKey { get; set; } 

     [IsFilterable, IsSortable, IsSearchable] 
     public string Name { get; set; } = "TemptAsset " + new Guid(); 

     [IsFilterable, IsSortable] 
     public int? Version { get; set; } = 1; 

     [IsFilterable, IsSortable] 
     public DateTimeOffset? ToBePublishedDate { get; set; } = DateTimeOffset.UtcNow; 

     [IsFilterable, IsSortable] 
     public DateTimeOffset? ToBeRetiredDate { get; set; } = null; 

     [IsFilterable, IsSearchable, IsSortable] 
     public string Company { get; set; } = "TempCompany"; 
     [IsFilterable, IsSortable] 
     public bool IsApproved { get; set; } = false; 

     [IsFilterable, IsSortable] 
     public bool IsDraft { get; set; } = true; 


    } 

此運行,該指數已成功創建參見下文 enter image description here

現在,如果我嘗試將實體添加到表中我得到一個錯誤請求,但做同樣的在我的實體中註釋掉PartitionKey並且這個工作正常。 這是我如何創建我的索引

AzureSearch.CreateAssetNameIndex(AzureSearch.CreateSearchServiceClient()); 

    and the methods called bellow 




using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Threading; 
    using System.Threading.Tasks; 
    using AssetSynch.Models; 
    using Microsoft.Azure.Search; 
    using Microsoft.Azure.Search.Models; 
     public static SearchServiceClient CreateSearchServiceClient() 
     { 
      string searchServiceName = "*****"; 
      string adminApiKey = "********"; 

      SearchServiceClient serviceClient = new SearchServiceClient(searchServiceName, 
       new SearchCredentials(adminApiKey)); 

      return serviceClient; 
     } 

    public static async void CreateAssetNameIndex(SearchServiceClient serviceClient) 
     { 
      Index definition = new Index 
      { 
       Name = "assetname", 
       Fields = FieldBuilder.BuildForType<Asset>() 
      }; 

      await serviceClient.Indexes.CreateAsync(definition); 
     } 

如果我回到使用郵遞員的錯誤,這是例外,我得到

 { 
    "innerExceptions": [ 
    { 
     "requestInformation": { 
     "httpStatusCode": 400, 
     "httpStatusMessage": "Bad Request", 
     "serviceRequestID": "59efbc9a-0002-002c-3570-d5d55c000000", 
     "contentMd5": null, 
     "etag": null, 
     "requestDate": "Thu, 25 May 2017 17:05:01 GMT", 
     "targetLocation": 0, 
     "extendedErrorInformation": { 
      "errorCode": "PropertiesNeedValue", 
      "errorMessage": "The values are not specified for all properties in the entity.\nRequestId:59efbc9a-0002-002c-3570-d5d55c000000\nTime:2017-05-25T16:05:06.5197909Z", 
      "additionalDetails": {} 
     }, 
     "isRequestServerEncrypted": false 
     } 
    } 
    ] 
} 

如果我從實體刪除該分區鍵,然後重新運行相同的代碼重新創建索引的同一段代碼執行成功。 我注意到的是現在有2個分區鍵在我的實體上,其中一個將保持爲空,請參閱圖像波紋管,並且我的屬性不會覆蓋原始。

enter image description here

有我丟失的東西嗎?

+0

您提到您正在使用Azure存儲SDK,但代碼適用於Search Service SDK。你能解釋一下嗎? –

+0

對不起我在想什麼我會打字我會修復這個 – Harry

+0

如果你正在運行它作爲一個控制檯應用程序,你可以跟蹤在一個工具,如Fiddler的請求/響應。你應該看到更多關於錯誤的細節。 –

回答

1

根據你的代碼,我發現你的資產已經使用新的關鍵字來修改基類的分區屬性。

但是這隻會隱藏base.partition不會覆蓋它。

public new string PartitionKey { get; set; } 

在設置在資產類別的價值,你會發現它包含兩個分區如下:

enter image description here

因此,如果基類的分區鍵值爲空,它會返回400錯誤。

所以如果你想添加新的實體到表中,你需要設置基類(TableEntity)分區鍵值。

所以,我建議你可以改變你的資產如下:

[SerializePropertyNamesAsCamelCase] 
    public class Asset : TableEntity 
    { 
     public Asset(string name) 
     { 
      Name = name; 
      base.PartitionKey = this.PartitionKey; 
     } 

     public Asset() 
     { 
      base.PartitionKey = this.PartitionKey; 
     } 

     public Asset(string name, DateTimeOffset toBePublished, string pkey) 
     { 

      Name = name; 
      ToBePublishedDate = toBePublished;   
      PartitionKey = pkey; 
      base.PartitionKey = this.PartitionKey; 
     } 

     [Key] 
     [IsFilterable] 
     public string Id { get; set; } = DateTimeOffset.UtcNow.ToString("O") 
      .Replace("+", string.Empty) 
      .Replace(":", string.Empty) 
      .Replace(".", string.Empty); 

     [IsFilterable, IsSortable, IsSearchable] 
     public new string PartitionKey { get; set; } 

     [IsFilterable, IsSortable, IsSearchable] 
     public string Name { get; set; } = "TemptAsset " + new Guid(); 

     [IsFilterable, IsSortable] 
     public int? Version { get; set; } = 1; 

     [IsFilterable, IsSortable] 
     public DateTimeOffset? ToBePublishedDate { get; set; } = DateTimeOffset.UtcNow; 

     [IsFilterable, IsSortable] 
     public DateTimeOffset? ToBeRetiredDate { get; set; } = null; 

     [IsFilterable, IsSearchable, IsSortable] 
     public string Company { get; set; } = "TempCompany"; 
     [IsFilterable, IsSortable] 
     public bool IsApproved { get; set; } = false; 

     [IsFilterable, IsSortable] 
     public bool IsDraft { get; set; } = true; 


    } 

如果你想使用表存儲作爲數據源,我建議你可以參考這個article

+0

謝謝,它現在確實有道理。我會看看這篇文章,以更好地理解這一點。 – Harry