【问题标题】:Adding Key Phrase Extraction to Azure Cognitive Search将关键短语提取添加到 Azure 认知搜索
【发布时间】:2021-11-28 15:35:30
【问题描述】:

我已成功集成 Azure 认知搜索,并且通常按预期工作。现在,我被要求整合关键短语提取技能组。

我们已经创建了多合一键,更新了索引以包括语言检测和关键短语提取(关键短语需要文档上的语言代码),并且索引更新是成功的......或者我们相信.查看 Azure 控制台时,我看到所有已定义的技能集,属性存在于文档中,但这些字段中没有数据。

我的示例文档如下所示

public class SearchableDocument {
    [SimpleField(IsKey = true, IsFilterable = true)]
    public string Id { get; set; }
    
    [SearchableField(IsFilterable = true)]
    public string Text { get; set; }

    [SimpleField]
    public string[] KeyPhrases { get; set; }

    [SimpleField]
    public string LanguageCode { get; set; }
}

那么索引设置是这样的

public void CreateOrUpdateSkillsetAsync()
{
    var serviceEndpoint = CreateServiceEndPoint();
    var credential = new AzureKeyCredential(_cognitiveApiKey);

    var languageDetectSkill = CreateLanguageDetectionSkill();
    var keyPhraseSkill = CreateKeyPhraseExtractionSkill();

    var skills = new List<SearchIndexerSkill>
    {
        languageDetectSkill,
        keyPhraseSkill
    };

    var skillSetName = _cognitiveIndexName;
    var skillSet = new SearchIndexerSkillset(skillSetName, skills);
    skillSet.Description = _configProvider.GetCurrentEnvironmentName();
    skillSet.CognitiveServicesAccount = new CognitiveServicesAccountKey(_cognitiveServiceAllInOneKey);
    
    var client = new SearchIndexerClient(serviceEndpoint, credential);
    client.CreateOrUpdateSkillset(skillSet);
}

private LanguageDetectionSkill CreateLanguageDetectionSkill()
{
    var inputMappings = new List<InputFieldMappingEntry>();

    inputMappings.Add(new InputFieldMappingEntry("text")
    {
        Source = "/document/Text",
    });

    var outputMappings = new List<OutputFieldMappingEntry>
    {
        new OutputFieldMappingEntry("languageCode")
        {
            TargetName = "LanguageCode"
        }
    };

    var skill = new LanguageDetectionSkill(inputMappings, outputMappings)
    {
        Name = "Language Detection",
        Description = "Detect language",
        Context = "/document/*",
    };

    return skill;
}

private KeyPhraseExtractionSkill CreateKeyPhraseExtractionSkill()
{
    var inputMappings = new List<InputFieldMappingEntry>();

    inputMappings.Add(new InputFieldMappingEntry("text")
    {
        Source = "/document/Text",
        
    });
    inputMappings.Add(new InputFieldMappingEntry("languageCode")
    {
        Source = "/document/LanguageCode"
    });

    var outputMappings = new List<OutputFieldMappingEntry>
    {
        new OutputFieldMappingEntry("keyPhrases")
        {
            TargetName = "KeyPhrases"
        }
    };

    var keyPhraseExtractionSkill = new KeyPhraseExtractionSkill(inputMappings, outputMappings)
    {
        Name = "KeyPhrase Extraction",
        Description = "Extract the key phrases",
        Context = "/document/*",
        DefaultLanguageCode = KeyPhraseExtractionSkillLanguage.En,
    };

    return keyPhraseExtractionSkill;
}

在使用技能集更新索引后,我期待属性 LangaugeCodeKeyPhrases 会开始填写。但这不适用于新文档或现有文档。

【问题讨论】:

    标签: azure azure-cognitive-search azure-cognitive-services


    【解决方案1】:

    skillset 需要附加到索引器才能运行,然后从数据源读取、执行扩充并写入索引。如果您检查您的索引器执行历史记录,应该会有一些可以提供一些提示的警告。

    1. 技能输入应参考数据源的属性名称,而不是索引。根据您的数据来自哪里,它需要不同的“名称”来引用输入文本。如果您的数据源类型是具有默认解析的存储帐户,那么输入可能应该是/document/content 而不是/document/Text;或者如果是 Cosmos DB,那么它将是 /document/&lt;property name&gt;;如果文本来自 blob 元数据,请参阅 here

    2. 技能上下文应该是/document,因为每个文档都是一个“属性包”而不是一个数组。

    3. 具有技能集的索引器需要output field mappings 来描述要填充的索引字段。所以从你的索引定义来看,它会是

    "outputFieldMappings": [
            {
                "sourceFieldName": "/document/LanguageCode",
                "targetFieldName": "LanguageCode"
            },
            {
                "sourceFieldName": "/document/KeyPhrases",
                "targetFieldName": "KeyPhrases"
            }
    ]
    
    1. 每次索引或技能组定义发生更改时,索引器都需要resetrun 再次获取更改。或者,考虑使用cache 以避免重新运行未更改的技能。

    【讨论】:

    • 我正在通过 C# api 以编程方式将文档添加到搜索索引中(我必须进行大量数据清理,所以我不能直接指向我的数据库然后去)。所以理论上,我的来源是 Azure 搜索索引。
    • 不幸的是,如果您将文档直接推送到索引,则无法使用技能集。是否可以进行有视野的擦洗?这可能是理想的设置,因此敏感数据永远不会离开数据库,您可以按计划设置索引器以在后台获取数据,而无需直接调用索引 API:docs.microsoft.com/en-us/azure/search/…
    • 知道了。谢谢。
    猜你喜欢
    • 2020-07-03
    • 2021-12-09
    • 2020-08-12
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 2020-03-27
    • 1970-01-01
    • 2023-01-05
    相关资源
    最近更新 更多