简体   繁体   English

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

[英]Azure Search: Create Index for complex types

I have a blob storage that contains some xml-files.我有一个包含一些 xml 文件的 blob 存储。 I want to use the power of Azure Search to easily find documents in this blob storage.我想利用 Azure 搜索的强大功能在此 Blob 存储中轻松查找文档。 The structure of the XML-files are in this format: 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>

Creating an index in Azure Search fails because the index requires marking a field as IsKey at the top level, but I don't have such a field.在 Azure 搜索中创建索引失败,因为索引需要在顶级将字段标记为IsKey ,但我没有这样的字段。 How can I solve this?我该如何解决这个问题? Underneath is the code to generate an index for ComplexTypes:下面是为 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
    }
};

Can anyone guide me in the correct direction?谁能指导我正确的方向?

I would recommend using a Class for creating the Index.我建议使用类来创建索引。

For example:例如:

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; }
    }
}

And then create it using something like the following:然后使用以下内容创建它:

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);
}

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

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