是否可以在 Azure 搜索索引中填充集合?

Is it possible to populate a collection in an Azure Search index?

我目前一直在使用 Azure 搜索,我偶然发现了一个场景,我有两个 table:一个描述一个地方,另一个为每个地方提供多张照片。

我想使用两个索引器来填充同一个索引,其中一个将填充地点的详细信息(名称、坐标等),而另一个将获取照片,运行 自定义 WebApiSkill 从照片中获取某些细节,并将它们添加到索引上的复杂集合中。

问题是,我不知道如何将照片集填充到索引中。我尝试将我的 outputFieldMappings 定义为 Photos/*/Details,或者只是 Photos/Details,但我不断收到相同的错误:

Microsoft.Rest.Azure.CloudException: 'Output field mapping specifies target field 'Photos/*/PhotoURI' that doesn't exist in the index'

考虑到这一点,我想知道是否可以在 Azure 搜索中填充像这样的复杂集合,如果可以,我将如何做?


表格:

CREATE TABLE Places
(
    Id             bigint         not null  IDENTITY(1,1)  PRIMARY KEY,
    Name           nvarchar(50)   not null,
    Location       nvarchar(50)   not null
);

CREATE TABLE PlacePhotos
(
    PhotoURI  nvarchar(200)  not null  PRIMARY KEY,
    PlaceId   bigint         not null  REFERENCES Places (Id)
);

类 用于创建索引:

public partial class Photo
{
    [IsSearchable]
    public string PhotoURI { get; set; }

    public List<Details> Details { get; set; }
}
public partial class Place
{
    [System.ComponentModel.DataAnnotations.Key]
    public string Id { get; set; }

    [IsSearchable, IsFilterable, IsSortable, IsFacetable]
    public string Name { get; set; } 

    [IsSearchable, IsFilterable]
    public string Location { get; set; }

    [IsSearchable, IsFilterable, IsSortable]
    public List<Photo> Photos { get; set; }

}

索引器(以 PlacePhotos table 作为其数据源):

private static void Create_PlacePhotos_Indexer(SearchServiceClient serviceClient)
{
    var indexerDef = new Indexer(
        name: placePhotosIndexer,
        dataSourceName: placePhotosDataSource,
        targetIndexName: index,
        skillsetName: skillset,
        schedule: new IndexingSchedule
        {
            Interval = new TimeSpan(0, 30, 0)
        },
        fieldMappings: new List<FieldMapping>
        {
            new FieldMapping("PlaceId", "Id"),
            new FieldMapping("PhotoURI", "PhotoURI")
        },
        outputFieldMappings: new List<FieldMapping>
        {
            new FieldMapping("/document/Id", "Id"),
            new FieldMapping("/document/PhotoURI", "Photos/*/PhotoURI"),
            new FieldMapping("/document/Details", "Photos/*/Details")
        }
    );
    serviceClient.Indexers.CreateOrUpdate(indexerDef);
}

Output field mapping 目标字段名称必须是顶级索引字段名称,因此 Photos/*/PhotoURI 无效,您必须直接针对 Photos

到"shape"要映射的Photos值,可以使用ShaperSkill

{
  "@odata.type": "#Microsoft.Skills.Util.ShaperSkill",
  "context": "/document/photos/*",
  "inputs": [
    {
      "name": "PhotoURI",
      "source": "/document/photos/*/PhotoURI"
    },
    {
      "name": "Details",
      "source": "/document/photos/*/Details"
    }
  ],
  "outputs": [
    {
      "name": "output",
      "targetName": "shapedValue"
    }
  ]
}

和输出字段映射

new FieldMapping("/document/photos/*/shapedValue", "Photos")

注意 - 您可能需要重新安排输出 URI 和详细信息的技能,以便它们共享一个公共 context。从上面的代码片段来看,每个 document 似乎只有 1 张照片,这与您的索引定义不同。