增强索引以包含多种语言

已完成

可以将对多种语言的支持添加到搜索索引。 通过提供要支持的所有不同语言中的所有翻译文本字段,可以手动添加语言支持。 还可以选择使用 Azure AI 服务通过扩充管道提供翻译的文本。

在这里,你将了解如何向索引添加具有不同语言的字段。 然后,你会将结果限制为具有特定语言的字段。 最后,创建计分概要文件,以提高最终用户的本机语言。

添加特定于语言的字段

若要将多种语言添加到索引,首先,请标识需要翻译的所有字段。 然后,为要支持的每种语言复制这些字段。

例如,如果索引具有英语说明字段,则为法语翻译添加 description_fr,为德语添加 description_de。 对于每个字段,请将其定义添加到相应的语言分析器

索引的 JSON 定义如下所示:

    {
      "name": "description",
      "type": "Edm.String",
      "facetable": false,
      "filterable": false,
      "key": false,
      "retrievable": true,
      "searchable": true,
      "sortable": false,
      "analyzer": "en.microsoft",
      "indexAnalyzer": null,
      "searchAnalyzer": null,
      "synonymMaps": [],
      "fields": []
    },
    {
      "name": "description_de",
      "type": "Edm.String",
      "facetable": false,
      "filterable": false,
      "key": false,
      "retrievable": true,
      "searchable": true,
      "sortable": false,
      "analyzer": "de.microsoft",
      "indexAnalyzer": null,
      "searchAnalyzer": null,
      "synonymMaps": [],
      "fields": []
    },
    {
      "name": "description_fr",
      "type": "Edm.String",
      "facetable": false,
      "filterable": false,
      "key": false,
      "retrievable": true,
      "searchable": true,
      "sortable": false,
      "analyzer": "fr.microsoft",
      "indexAnalyzer": null,
      "searchAnalyzer": null,
      "synonymMaps": [],
      "fields": []
    },
    {
      "name": "description_it",
      "type": "Edm.String",
      "facetable": false,
      "filterable": false,
      "key": false,
      "retrievable": true,
      "searchable": true,
      "sortable": false,
      "analyzer": "it.microsoft",
      "indexAnalyzer": null,
      "searchAnalyzer": null,
      "synonymMaps": [],
      "fields": []
    },

限制语言的字段

在本模块中,你已了解如何限制搜索请求中返回的字段。 还可以选择要搜索的字段。 特定于语言的搜索解决方案可以将这两项功能组合在一起,以专注于其中具有特定语言的字段。

search='parfait pour se divertir'&$select=listingId, description_fr, city, region, tags&$searchFields=tags, description_fr&queryType=full

在上述结果中使用 searchFieldsselect 属性将从房地产样本数据库返回这些结果。

{
  "@odata.context": "https://advanced-cognitive-search.search.windows.net/indexes('realestate-us-sample-index')/$metadata#docs(*)",
  "value": [
    {
      "@search.score": 12.124968,
      "listingId": "OTM4MjY1OA2",
      "description_fr": "Il s'agit d'un condo et est parfait pour se divertir.  Cette maison offre des vues côtières Situé à proximité d'une rivière et un bureau, moulures and une véranda couverte.",
      "city": "Seattle",
      "region": "wa",
      "tags": [
        "condo",
        "entertaining",
        "coastal views",
        "river",
        "office",
        "crown mouldings",
        "covered front porch"
      ]
    },

通过 Azure AI 服务使用多种语言扩充索引

如果你无权访问翻译内容,可使用 Azure AI 服务来扩充索引并添加已翻译的字段。

这些步骤是为每个语言添加字段,为每个语言添加技能,然后将已翻译的文本映射到正确的字段。

例如,让我们将日语和乌克兰语翻译添加到零售属性索引示例。

添加新字段

使用以下属性将两个新字段添加到索引中,第一个用于存储日语翻译,第二个用于存储乌克兰语翻译:

{
  "name": "description_jp",
  "type": "Edm.String",
  "facetable": false,
  "filterable": false,
  "key": false,
  "retrievable": true,
  "searchable": true,
  "sortable": false,
  "analyzer": "ja.microsoft",
  "indexAnalyzer": null,
  "searchAnalyzer": null,
  "synonymMaps": [],
  "fields": []
},
{
  "name": "description_uk",
  "type": "Edm.String",
  "facetable": false,
  "filterable": false,
  "key": false,
  "retrievable": true,
  "searchable": true,
  "sortable": false,
  "analyzer": "uk.microsoft",
  "indexAnalyzer": null,
  "searchAnalyzer": null,
  "synonymMaps": [],
  "fields": []
}

添加翻译技能组

将两个技能添加到技能组定义中,以将 document/description 字段翻译为两种语言。

"skills": [
  {
    "@odata.type": "#Microsoft.Skills.Text.TranslationSkill",
    "name": "#1",
    "description": null,
    "context": "/document/description",
    "defaultFromLanguageCode": "en",
    "defaultToLanguageCode": "ja",
    "suggestedFrom": "en",
    "inputs": [
      {
        "name": "text",
        "source": "/document/description"
      }
    ],
    "outputs": [
      {
        "name": "translatedText",
        "targetName": "description_jp"
      }
    ]
  },
  {
    "@odata.type": "#Microsoft.Skills.Text.TranslationSkill",
    "name": "#2",
    "description": null,
    "context": "/document/description",
    "defaultFromLanguageCode": "en",
    "defaultToLanguageCode": "uk",
    "suggestedFrom": "en",
    "inputs": [
      {
        "name": "text",
        "source": "/document/description"
      }
    ],
    "outputs": [
      {
        "name": "translatedText",
        "targetName": "description_uk"
      }
    ]
  }
]

将翻译的输出映射到索引中

最后一步是更新索引器以将翻译的文本映射到索引中。

"outputFieldMappings": [
  {
    "sourceFieldName": "/document/description/description_jp",
    "targetFieldName": "description_jp"
  },
  {
    "sourceFieldName": "/document/description/description_uk",
    "targetFieldName": "description_uk"
  }
]

文档现在都有两个新的翻译说明字段。

  "value": [
    {
      "@search.score": 1,
      "listingId": "OTM4MjI2NQ2",
      "beds": 5,
      "baths": 4,
      "description": "This is an apartment residence and is perfect for entertaining.  This home provides lakefront property located close to parks and features a detached garage, beautiful bedroom floors, and lots of storage.",
      "description_de": "Dies ist eine Wohnanlage und ist perfekt für Unterhaltung.  Dieses Haus bietet Seeliegenschaft Parks in der Nähe und verfügt über eine freistehende Garage schöne Zimmer-Etagen and viel Stauraum.",
      "description_fr": "Il s'agit d'un appartement de la résidence et est parfait pour se divertir.  Cette maison offre propriété au bord du lac Situé à proximité de Parcs et dispose d'un garage détaché, planchers de belle chambre and beaucoup de rangement.",
      "description_it": "Si tratta di un appartamento residence ed è perfetto per intrattenere.  Questa casa fornisce proprietà lungolago Situato vicino ai parchi e dispone di un garage indipendente, piani di bella camera da letto and sacco di stoccaggio.",
      "description_es": "Se trata de una residencia Apartamento y es perfecto para el entretenimiento.  Esta casa ofrece propiedad de lago situado cerca de parques y cuenta con un garaje independiente, pisos de dormitorio hermoso and montón de almacenamiento.",
      "description_pl": "Jest to apartament residence i jest idealny do zabawy.  Ten dom zapewnia lakefront Wlasciwosc usytuowany w poblizu parków i oferuje garaz wolnostojacy, piekna sypialnia podlogi and mnóstwo miejsca do przechowywania.",
      "description_nl": "Dit is een appartement Residentie en is perfect voor entertaining.  Dit huis biedt lakefront eigenschap vlakbij parken en beschikt over een vrijstaande garage, mooie slaapkamer vloeren and veel opslag.",
      "description_jp": "これはアパートの住居であり、娯楽に最適です。 この家は公園の近くに位置する湖畔のプロパティを提供し、独立したガレージ、美しいベッドルームの床とストレージの多くを備えています。",
      "description_uk": "Це багатоквартирна резиденція і прекрасно підходить для розваг.  Цей будинок забезпечує нерухомість на березі озера, розташовану недалеко від парків, і має окремий гараж, красиві підлоги спальні та багато місць для зберігання речей.",
      ...
    },