问题描述
我有一个Blob存储,其中包含一些xml文件。我想使用Azure搜索的功能轻松地在此Blob存储中查找文档。 XML文件的结构采用以下格式:
<items>
<item>
<id>12345</id>
<text>This is an example</text>
<item>
<item>
<id>12346</id>
<text>This is an example</text>
<item>
</items>
在Azure搜索中创建索引失败,因为该索引需要在顶级将字段标记为IsKey
,但是我没有这样的字段。我该如何解决?下面是为ComplexTypes生成索引的代码:
var complexField = new ComplexField("items");
complexField.Fields.Add(new SearchableField("id") { IsKey = true,IsFilterable = true,IsSortable = true });
complexField.Fields.Add(new SearchableField("text") { IsFilterable = true,IsSortable = true });
var index = new SearchIndex(IndexName)
{
Fields =
{
complexField
}
};
有人可以指引我正确的方向吗?
解决方法
我建议使用Class创建索引。
例如:
using Microsoft.Azure.Search;
using System.ComponentModel.DataAnnotations;
namespace Test
{
public class Records
{
[Key]
[IsSortable,IsFilterable]
public string id { get; set; }
[IsSortable,IsFilterable]
public string text { get; set; }
}
}
然后使用以下内容创建它:
var _serviceClient = new SearchServiceClient("<ServiceName>",new SearchCredentials("<ApiKey">));
public bool Create()
{
var newIndex = new Microsoft.Azure.Search.Models.Index()
{
Name = "<Index_Name>",Fields = FieldBuilder.BuildForType<Records>()
};
_serviceClient.Indexes.Create(newIndex);
}