首页 > 解决方案 > 如何通过 Java 以编程方式在 Azure 搜索中设置认知搜索功能(使用 OCR)?

问题描述

我想在我的应用程序中提供全文搜索功能,因此我正在尝试使用认知搜索功能配置 Azure 搜索,以便我可以索引存储在 Azure Blob 存储中的图像和非图像文档。但是,在使用 Azure 搜索的 REST API 通过 Java 代码配置 Azure 搜索时,我无法在 Azure 搜索中利用 OCR 功能,并且图像文档没有被索引。通过 Java 代码(使用 Azure 搜索 REST API)配置 Azure 搜索时,我遗漏了一些配置详细信息。

案例 1:从 Azure 门户,我能够

  1. 使用认知功能(包括 OCR 技能集)、索引、索引器和 Azure Blob 存储来配置 Azure 搜索。
  2. 索引图像和非图像文档,例如 pdf、png、jpg、xls 等。
  3. 搜索索引文档

案例 2:使用 Azure REST API 从 Java 代码中,我能够

  1. 使用认知功能、索引、索引器和 Azure Blob 存储配置 Azure 搜索。
  2. 索引非图像文档,例如 pdf、xls 等。
  3. 搜索索引文档但是,在使用 Azure 搜索的 REST API 通过 Java 代码配置 Azure 搜索时(在案例 2 中),我无法在 Azure 搜索中利用 OCR 功能,并且图像文档没有被索引。通过 Java 代码(使用 Azure 搜索 REST API)配置 Azure 搜索时,我遗漏了一些配置详细信息。

我正在使用 Java 代码 1 中的以下示例 Azure Search Rest API。https://%s.search.windows.net/datasources?api-version=%s 2.https ://%s.search.windows.net/技能集/cog-search-demo-ss?api-version=%s 3. https://%s.search.windows.net/indexes/%s?api-version=%s 4. https://%s .search.windows.net/indexers?api-version=%s

配置jsons:1.datasource.json

{
   "name" : "csstoragetest",
    "type" : "azureblob",
    "credentials" : { "connectionString" : "connectionString" },
    "container" : { "name" : "csblob" }
}
  1. 技能组.json
{
   "description": "Extract text from images and merge with content text to produce merged_text",
  "skills":
  [
    {
      "description": "Extract text (plain and structured) from image.",
      "@odata.type": "#Microsoft.Skills.Vision.OcrSkill",
      "context": "/document/normalized_images/*",
      "defaultLanguageCode": "null",
      "detectOrientation": true,
      "inputs": [
        {
          "name": "image",
          "source": "/document/normalized_images/*"
        }
      ],
      "outputs": [
        {
          "name": "text",
          "targetName": "myText"
        },
        {
          "name": "layoutText",
          "targetName": "myLayoutText"
        }
      ]
    },
    {
      "@odata.type": "#Microsoft.Skills.Text.MergeSkill",
      "description": "Create merged_text, which includes all the textual representation of each image inserted at the right location in the content field.",
      "context": "/document",
      "insertPreTag": " ",
      "insertPostTag": " ",
      "inputs": [
        {
          "name":"text", "source": "/document/content"
        },
        {
          "name": "itemsToInsert", "source": "/document/normalized_images/*/text"
        },
        {
          "name":"offsets", "source": "/document/normalized_images/*/contentOffset"
        }
      ],
      "outputs": [
        {
          "name": "mergedText", "targetName" : "merged_text"
        }
      ]
    }
  ]
}
  1. 索引.json
{
  "name": "azureblob-indexing",
  "fields": [
    { "name": "id", "type": "Edm.String", "key": true, "searchable": false },
    { "name": "content", "type": "Edm.String", "searchable": true, "filterable": false, "sortable": false, "facetable": false }
  ]
}
  1. indexer.json
{
    "name" : "azureblob-indexing1",
  "dataSourceName" : "csstoragetest",
  "targetIndexName" : "azureblob-indexing",
  "schedule" : { "interval" : "PT2H" },
  "skillsetName" : "cog-search-demo-ss",
  "parameters":
  {
    "maxFailedItems":-1,
    "maxFailedItemsPerBatch":-1,
    "configuration":
    {
      "dataToExtract": "contentAndMetadata",
      "imageAction":"generateNormalizedImages",
      "parsingMode": "default",
      "firstLineContainsHeaders": false,
      "delimitedTextDelimiter": ","
    }
  }
}

通过 java 代码配置 Azure 搜索后,图像文档应该在 azure 搜索中建立索引,并且我应该能够根据其中包含的文本搜索它们。

标签: azuresearch

解决方案


尝试将默认语言代码设置为 null ,而无需在Skillset.json中使用引号:

"defaultLanguageCode": null

推荐阅读