微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Azure搜索:为复杂类型创建索引

如何解决Azure搜索:为复杂类型创建索引

我有一个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);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。