如何使用复杂的 targetField 在 Azure 搜索中创建字段映射

How to create a field mapping in Azure Search with a complex targetField

我使用 Azure 搜索索引器为 MongoDB CosmosDB 中的文档编制索引,其中包含具有名为 _id 的字段的对象。 由于 Azure 搜索不允许在索引中的字段名称开头使用下划线,因此我想创建一个字段映射。

JSON Cosmos 中的结构 --> index 中的结构

{
  "id": "test"
  "name": "test",
  "productLine": {
     "_id": "123",       --> "id": "123"
     "name": "test"
  }
}

documentation 正是以这种情况为例 仅适用于顶级字段。

"fieldMappings" : [ { "sourceFieldName" : "_id", "targetFieldName" : "id" } ]}

我尝试了以下方法:

"fieldMappings" : [ { "sourceFieldName" : "productLine/_id", "targetFieldName" : "productLine/id" } ] }

导致错误说明:

Value is not accepted. Valid values: "doc_id", "name", "productName".

为作为子字段的目标字段创建映射的正确方法是什么?

无法直接映射子字段。您可以通过添加 Skillset with a Shaper cognitive skill to the indexer, and an output field mapping.

来解决这个问题

你也会想要attach a Cognitive Services resource to the skillset. The shaper skill doesn't get billed, but attaching a Cognitive Services resource allows you to process more than 20 documents per day

塑形技能

{
  "@odata.type": "#Microsoft.Skills.Util.ShaperSkill",
  "context": "/document",
  "inputs": [
    {
      "name": "id",
      "source": "/document/productLine/_id"
    },
    {
      "name": "name",
      "source": "/document/productLine/name"
    }
  ],
  "outputs": [
    {
      "name": "output",
      "targetName": "renamedProductLine"
    }
  ]
}

索引器技能集和输出字段映射

"skillsetName": <skillsetName>,
"outputFieldMappings": [
    {
        "sourceFieldName": "/document/renamedProductLine",
        "targetFieldName": "productLine"
    }
]