Azure Machine Learning - 使用 REST API 创建 Azure AI 搜索索引

本文介绍如何使用 Azure AI 搜索 REST AP和用于发送和接收请求的 REST 客户端以交互方式构建请求。
关注TechLead,分享AI全维度知识。作者拥有10+年互联网服务架构、AI产品研发经验、团队管理经验,同济本复旦硕,复旦机器人智能实验室成员,阿里云认证的资深架构师,项目管理专业人士,上亿营收AI产品研发负责人。

环境准备

  • Postman 应用,用于将请求发送到 Azure AI 搜索。

  • 创建 Azure AI 搜索服务\]或在当前订阅下[查找现有服务](https://portal.azure.com/#blade/HubsExtension/BrowseResourceBlade/resourceType/Microsoft.Search%2FsearchServices)。 可以使用本快速入门的免费服务。

REST 调用需要在每个请求中使用服务 URL 和访问密钥。 搜索服务是使用这二者创建的,因此,如果向订阅添加了 Azure AI 搜索,则请按以下步骤获取必需信息:

  1. 登录到 Azure 门户,在搜索服务的"概览"页中获取 URL。 示例终结点可能类似于 https://mydemo.search.windows.net

  2. 在"设置">"密钥"中,获取有关该服务的完全权限的管理员密钥 。 有两个可交换的管理员密钥,为保证业务连续性而提供,以防需要滚动一个密钥。 可以在请求中使用主要或辅助密钥来添加、修改和删除对象。

所有请求对发送到服务的每个请求都需要 API 密钥。 具有有效的密钥可以在发送请求的应用程序与处理请求的服务之间建立信任关系,这种信任关系以每个请求为基础。

连接到 Azure AI 搜索

连接信息在 URI 终结点中指定。 集合变量用于表示搜索服务名称和 API 密钥。 本快速入门中的典型 URI 如下所示:

复制代码
https://{{service-name}}.search.windows.net/indexes/hotels-quickstart?api-version=2020-06-30

请注意 HTTPS 前缀、服务变量的名称、对象的名称(在本例中为索引集合中的某个索引的名称)和 api-version。 api-version 是必需的。

请求头组合包括两个元素:Content-Type,以及用于向 Azure AI 搜索进行身份验证的 api-keyapi-key 指定为变量,也是必需的。

若要使请求成功,需要提供服务名称和 api-key 作为集合变量。

  1. 打开 Postman 应用并导入集合。

  2. 选择集合的访问菜单,选择"编辑",并提供搜索服务的服务名称和密钥。

1 - 创建索引

在 Azure AI 搜索中,通常会先创建索引,然后再连同数据一起加载索引。 本任务将使用[创建索引 REST API]。

需扩展 URL 以包含 hotels-quickstart 索引名称。

  1. 将谓词设置为 PUT。

  2. 复制此 URL https://{``{service-name}}.search.windows.net/indexes/hotels-quickstart?api-version=2020-06-30

  3. 在请求正文中提供索引定义(接下来提供可直接复制的代码)。

  4. 选择"发送"。

索引定义

字段集合定义文档结构。 每个文档必须包含这些字段,每个字段必须具有一个数据类型。 字符串字段用于全文搜索。 如果你需要使数值数据可供搜索,则需要将数值数据强制转换为字符串。

字段的属性决定了允许的操作。 默认情况下,REST API 允许很多操作。 例如,默认情况下,所有字符串均可供搜索、检索、筛选、分面。 通常,仅当需要禁用某种行为时,才要设置属性。

复制代码
{
    "name": "hotels-quickstart",  
    "fields": [
        {"name": "HotelId", "type": "Edm.String", "key": true, "filterable": true},
        {"name": "HotelName", "type": "Edm.String", "searchable": true, "filterable": false, "sortable": true, "facetable": false},
        {"name": "Description", "type": "Edm.String", "searchable": true, "filterable": false, "sortable": false, "facetable": false, "analyzer": "en.lucene"},
        {"name": "Category", "type": "Edm.String", "searchable": true, "filterable": true, "sortable": true, "facetable": true},
        {"name": "Tags", "type": "Collection(Edm.String)", "searchable": true, "filterable": true, "sortable": false, "facetable": true},
        {"name": "ParkingIncluded", "type": "Edm.Boolean", "filterable": true, "sortable": true, "facetable": true},
        {"name": "LastRenovationDate", "type": "Edm.DateTimeOffset", "filterable": true, "sortable": true, "facetable": true},
        {"name": "Rating", "type": "Edm.Double", "filterable": true, "sortable": true, "facetable": true},
        {"name": "Address", "type": "Edm.ComplexType", 
        "fields": [
        {"name": "StreetAddress", "type": "Edm.String", "filterable": false, "sortable": false, "facetable": false, "searchable": true},
        {"name": "City", "type": "Edm.String", "searchable": true, "filterable": true, "sortable": true, "facetable": true},
        {"name": "StateProvince", "type": "Edm.String", "searchable": true, "filterable": true, "sortable": true, "facetable": true},
        {"name": "PostalCode", "type": "Edm.String", "searchable": true, "filterable": true, "sortable": true, "facetable": true},
        {"name": "Country", "type": "Edm.String", "searchable": true, "filterable": true, "sortable": true, "facetable": true}
        ]
     }
  ]
}

提交此请求后,会获得 HTTP 201 响应,指示索引已成功创建。 可以在门户中验证此操作,但请注意,门户页有刷新时间间隔,因此可能需要等待一到两分钟。

2 - 加载文档

创建索引和填充索引是分开的步骤。 在 Azure AI 搜索中,索引包含所有可搜索的数据。 在此场景中,数据以 JSON 文档的形式提供。 本任务将使用[添加、更新或删除文档 REST API]。

需扩展 URL 以包含 docs 集合与 index 操作。

  1. 将谓词设置为 POST。

  2. 复制此 URL https://{``{service-name}}.search.windows.net/indexes/hotels-quickstart/docs/index?api-version=2020-06-30

  3. 在请求的正文中提供 JSON 文档(接下来提供可直接复制的代码)。

  4. 选择"发送"。

要载入索引的 JSON 文档

请求正文包含四个要添加到 hotels 索引的文档。

复制代码
{
    "value": [
    {
    "@search.action": "upload",
    "HotelId": "1",
    "HotelName": "Secret Point Motel",
    "Description": "The hotel is ideally located on the main commercial artery of the city in the heart of New York. A few minutes away is Time's Square and the historic centre of the city, as well as other places of interest that make New York one of America's most attractive and cosmopolitan cities.",
    "Category": "Boutique",
    "Tags": [ "pool", "air conditioning", "concierge" ],
    "ParkingIncluded": false,
    "LastRenovationDate": "1970-01-18T00:00:00Z",
    "Rating": 3.60,
    "Address": 
        {
        "StreetAddress": "677 5th Ave",
        "City": "New York",
        "StateProvince": "NY",
        "PostalCode": "10022",
        "Country": "USA"
        } 
    },
    {
    "@search.action": "upload",
    "HotelId": "2",
    "HotelName": "Twin Dome Motel",
    "Description": "The hotel is situated in a  nineteenth century plaza, which has been expanded and renovated to the highest architectural standards to create a modern, functional and first-class hotel in which art and unique historical elements coexist with the most modern comforts.",
    "Category": "Boutique",
    "Tags": [ "pool", "free wifi", "concierge" ],
    "ParkingIncluded": false,
    "LastRenovationDate": "1979-02-18T00:00:00Z",
    "Rating": 3.60,
    "Address": 
        {
        "StreetAddress": "140 University Town Center Dr",
        "City": "Sarasota",
        "StateProvince": "FL",
        "PostalCode": "34243",
        "Country": "USA"
        } 
    },
    {
    "@search.action": "upload",
    "HotelId": "3",
    "HotelName": "Triple Landscape Hotel",
    "Description": "The Hotel stands out for its gastronomic excellence under the management of William Dough, who advises on and oversees all of the Hotel's restaurant services.",
    "Category": "Resort and Spa",
    "Tags": [ "air conditioning", "bar", "continental breakfast" ],
    "ParkingIncluded": true,
    "LastRenovationDate": "2015-09-20T00:00:00Z",
    "Rating": 4.80,
    "Address": 
        {
        "StreetAddress": "3393 Peachtree Rd",
        "City": "Atlanta",
        "StateProvince": "GA",
        "PostalCode": "30326",
        "Country": "USA"
        } 
    },
    {
    "@search.action": "upload",
    "HotelId": "4",
    "HotelName": "Sublime Cliff Hotel",
    "Description": "Sublime Cliff Hotel is located in the heart of the historic center of Sublime in an extremely vibrant and lively area within short walking distance to the sites and landmarks of the city and is surrounded by the extraordinary beauty of churches, buildings, shops and monuments. Sublime Cliff is part of a lovingly restored 1800 palace.",
    "Category": "Boutique",
    "Tags": [ "concierge", "view", "24-hour front desk service" ],
    "ParkingIncluded": true,
    "LastRenovationDate": "1960-02-06T00:00:00Z",
    "Rating": 4.60,
    "Address": 
        {
        "StreetAddress": "7400 San Pedro Ave",
        "City": "San Antonio",
        "StateProvince": "TX",
        "PostalCode": "78216",
        "Country": "USA"
        }
    }
  ]
}

在几秒钟内,应在会话列表中看到 HTTP 201 响应。 这指示已成功创建文档。

如果收到 207,则指示至少有一个文档无法上传。 如果收到 404,则表示请求的标头或正文有语法错误:请验证是否已更改终结点,使之包括 /docs/index

提示

对于所选数据源,可[创建索引器],这可简化并减少索引所需的代码量。

3 - 搜索索引

现在,索引和文档集已加载,可以使用搜索文档 REST API 针对它们发出查询了。

需扩展 URL,以包含使用搜索运算符指定的查询表达式。

  1. 将谓词设置为 GET。

  2. 复制此 URL https://{``{service-name}}.search.windows.net/indexes/hotels-quickstart/docs?search=*&$count=true&api-version=2020-06-30

  3. 选择Send

此查询为空,在搜索结果中返回文档的计数。 在选择"发送"后,请求和响应应类似于以下针对 Postman 的屏幕截图。 状态代码应为 200。

尝试其他查询示例来了解语法。 你可以执行字符串搜索、逐字筛选查询、限制结果集、将搜索范围限定为特定字段等。

复制代码
# Query example 1 - Search on restaurant and wifi
# Return only the HotelName, Description, and Tags fields
https://{{service-name}}.search.windows.net/indexes/hotels-quickstart/docs?search=restaurant wifi&$count=true&$select=HotelName,Description,Tags&api-version=2020-06-30

# Query example 2 - Apply a filter to the index to find hotels rated 4 or highter
# Returns the HotelName and Rating. Two documents match
https://{service-name}}.search.windows.net/indexes/hotels-quickstart/docs?search=*&$filter=Rating gt 4&$select=HotelName,Rating&api-version=2020-06-30

# Query example 3 - Take the top two results, and show only HotelName and Category in the results
https://{service-name}}.search.windows.net/indexes/hotels-quickstart/docs?search=boutique&$top=2&$select=HotelName,Category&api-version=2020-06-30

# Query example 4 - Sort by a specific field (Address/City) in ascending order
https://{service-name}}.search.windows.net/indexes/hotels-quickstart/docs?search=pool&$orderby=Address/City asc&$select=HotelName, Address/City, Tags, Rating&api-version=2020-06-30

获取索引属性

还可以使用获取统计信息来查询文档计数和索引大小:

复制代码
https://{{service-name}}.search.windows.net/indexes/hotels-quickstart/stats?api-version=2020-06-30

向 URL 添加 /stats 会返回索引信息。 在 Postman 中,请求应如下所示,响应包括文档计数和所用空间(以字节为单位)。

请注意,api-version 语法有所不同。 对于此请求,请使用 ? 来追加 api-version。 ? 将 URL 路径与查询字符串分隔开,而 & 将查询字符串中的每个"名称=值"对分隔开。 就此查询来说,api-version 是查询字符串中的第一个项,也是唯一项。

关注TechLead,分享AI全维度知识。作者拥有10+年互联网服务架构、AI产品研发经验、团队管理经验,同济本复旦硕,复旦机器人智能实验室成员,阿里云认证的资深架构师,项目管理专业人士,上亿营收AI产品研发负责人。

相关推荐
沃达德软件4 分钟前
图像处理与复原技术
图像处理·人工智能·深度学习·神经网络·目标检测·计算机视觉·目标跟踪
坐在地上想成仙5 分钟前
人工智能商业落地思考:从人类行为图谱到技术栈映射
人工智能
zhangfeng11335 分钟前
ModelScope(魔搭社区)介绍与模型微调全指南 中国版Hugging Face GPU租借平台 一站式开源模型社区与服务平台
人工智能·开源
PaperRed ai写作降重助手6 分钟前
如何选择适合自己的AI智能降重写作软件
人工智能·深度学习·aigc·ai写作·论文降重·论文查重·智能降重
友思特 智能感知7 分钟前
友思特案例 | 金属行业视觉检测案例三:彩涂钢板表面纹理检测
人工智能·视觉检测
李永奉16 分钟前
杰理芯片SDK开发-ENC双麦降噪配置/调试教程
人工智能·单片机·嵌入式硬件·物联网·语音识别
Dfreedom.25 分钟前
图像滤波:非线性滤波与边缘保留技术
图像处理·人工智能·opencv·计算机视觉·非线性滤波·图像滤波
小白跃升坊36 分钟前
基于1Panel的AI运维
linux·运维·人工智能·ai大模型·教学·ai agent
kicikng40 分钟前
走在智能体前沿:智能体来了(西南总部)的AI Agent指挥官与AI调度官实践
人工智能·系统架构·智能体协作·ai agent指挥官·ai调度官·应用层ai
测试者家园42 分钟前
测试用例智能生成:是效率革命,还是“垃圾进,垃圾出”的新挑战?
人工智能·职场和发展·测试用例·测试策略·质量效能·智能化测试·用例设计