本文介紹如何使用 Azure AI 搜索 REST AP和用于發(fā)送和接收請求的 REST 客戶端以交互方式構(gòu)建請求。
關(guān)注TechLead,分享AI全維度知識。作者擁有10+年互聯(lián)網(wǎng)服務(wù)架構(gòu)、AI產(chǎn)品研發(fā)經(jīng)驗、團隊管理經(jīng)驗,同濟本復旦碩,復旦機器人智能實驗室成員,阿里云認證的資深架構(gòu)師,項目管理專業(yè)人士,上億營收AI產(chǎn)品研發(fā)負責人。文章來源地址http://www.zghlxwxcb.cn/news/detail-760872.html
環(huán)境準備
-
Postman 應(yīng)用,用于將請求發(fā)送到 Azure AI 搜索。
-
[創(chuàng)建 Azure AI 搜索服務(wù)]或在當前訂閱下查找現(xiàn)有服務(wù)。 可以使用本快速入門的免費服務(wù)。
復制密鑰和 URL
REST 調(diào)用需要在每個請求中使用服務(wù) URL 和訪問密鑰。 搜索服務(wù)是使用這二者創(chuàng)建的,因此,如果向訂閱添加了 Azure AI 搜索,則請按以下步驟獲取必需信息:
-
登錄到 Azure 門戶,在搜索服務(wù)的“概覽”頁中獲取 URL。 示例終結(jié)點可能類似于
https://mydemo.search.windows.net
。 -
在“設(shè)置”>“密鑰”中,獲取有關(guān)該服務(wù)的完全權(quán)限的管理員密鑰 。 有兩個可交換的管理員密鑰,為保證業(yè)務(wù)連續(xù)性而提供,以防需要滾動一個密鑰。 可以在請求中使用主要或輔助密鑰來添加、修改和刪除對象。
所有請求對發(fā)送到服務(wù)的每個請求都需要 API 密鑰。 具有有效的密鑰可以在發(fā)送請求的應(yīng)用程序與處理請求的服務(wù)之間建立信任關(guān)系,這種信任關(guān)系以每個請求為基礎(chǔ)。
連接到 Azure AI 搜索
連接信息在 URI 終結(jié)點中指定。 集合變量用于表示搜索服務(wù)名稱和 API 密鑰。 本快速入門中的典型 URI 如下所示:
https://{{service-name}}.search.windows.net/indexes/hotels-quickstart?api-version=2020-06-30
請注意 HTTPS 前綴、服務(wù)變量的名稱、對象的名稱(在本例中為索引集合中的某個索引的名稱)和 api-version。 api-version 是必需的。
請求頭組合包括兩個元素:Content-Type
,以及用于向 Azure AI 搜索進行身份驗證的 api-key
。 api-key
指定為變量,也是必需的。
若要使請求成功,需要提供服務(wù)名稱和 api-key 作為集合變量。
-
打開 Postman 應(yīng)用并導入集合。
-
選擇集合的訪問菜單,選擇“編輯”,并提供搜索服務(wù)的服務(wù)名稱和密鑰。
1 - 創(chuàng)建索引
在 Azure AI 搜索中,通常會先創(chuàng)建索引,然后再連同數(shù)據(jù)一起加載索引。 本任務(wù)將使用[創(chuàng)建索引 REST API]。
需擴展 URL 以包含 hotels-quickstart
索引名稱。
-
將謂詞設(shè)置為 PUT。
-
復制此 URL
https://{{service-name}}.search.windows.net/indexes/hotels-quickstart?api-version=2020-06-30
。 -
在請求正文中提供索引定義(接下來提供可直接復制的代碼)。
-
選擇“發(fā)送”。
索引定義
字段集合定義文檔結(jié)構(gòu)。 每個文檔必須包含這些字段,每個字段必須具有一個數(shù)據(jù)類型。 字符串字段用于全文搜索。 如果你需要使數(shù)值數(shù)據(jù)可供搜索,則需要將數(shù)值數(shù)據(jù)強制轉(zhuǎn)換為字符串。
字段的屬性決定了允許的操作。 默認情況下,REST API 允許很多操作。 例如,默認情況下,所有字符串均可供搜索、檢索、篩選、分面。 通常,僅當需要禁用某種行為時,才要設(shè)置屬性。
{
"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 響應(yīng),指示索引已成功創(chuàng)建。 可以在門戶中驗證此操作,但請注意,門戶頁有刷新時間間隔,因此可能需要等待一到兩分鐘。
2 - 加載文檔
創(chuàng)建索引和填充索引是分開的步驟。 在 Azure AI 搜索中,索引包含所有可搜索的數(shù)據(jù)。 在此場景中,數(shù)據(jù)以 JSON 文檔的形式提供。 本任務(wù)將使用[添加、更新或刪除文檔 REST API]。
需擴展 URL 以包含 docs
集合與 index
操作。
-
將謂詞設(shè)置為 POST。
-
復制此 URL
https://{{service-name}}.search.windows.net/indexes/hotels-quickstart/docs/index?api-version=2020-06-30
。 -
在請求的正文中提供 JSON 文檔(接下來提供可直接復制的代碼)。
-
選擇“發(fā)送”。
要載入索引的 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"
}
}
]
}
在幾秒鐘內(nèi),應(yīng)在會話列表中看到 HTTP 201 響應(yīng)。 這指示已成功創(chuàng)建文檔。
如果收到 207,則指示至少有一個文檔無法上傳。 如果收到 404,則表示請求的標頭或正文有語法錯誤:請驗證是否已更改終結(jié)點,使之包括 /docs/index
。
提示
對于所選數(shù)據(jù)源,可[創(chuàng)建索引器],這可簡化并減少索引所需的代碼量。
3 - 搜索索引
現(xiàn)在,索引和文檔集已加載,可以使用搜索文檔 REST API 針對它們發(fā)出查詢了。
需擴展 URL,以包含使用搜索運算符指定的查詢表達式。
-
將謂詞設(shè)置為 GET。
-
復制此 URL
https://{{service-name}}.search.windows.net/indexes/hotels-quickstart/docs?search=*&$count=true&api-version=2020-06-30
。 -
選擇Send。
此查詢?yōu)榭?,在搜索結(jié)果中返回文檔的計數(shù)。 在選擇“發(fā)送”后,請求和響應(yīng)應(yīng)類似于以下針對 Postman 的屏幕截圖。 狀態(tài)代碼應(yīng)為 200。
嘗試其他查詢示例來了解語法。 你可以執(zhí)行字符串搜索、逐字篩選查詢、限制結(jié)果集、將搜索范圍限定為特定字段等。
# 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
獲取索引屬性
還可以使用獲取統(tǒng)計信息來查詢文檔計數(shù)和索引大?。?/p>
https://{{service-name}}.search.windows.net/indexes/hotels-quickstart/stats?api-version=2020-06-30
向 URL 添加 /stats
會返回索引信息。 在 Postman 中,請求應(yīng)如下所示,響應(yīng)包括文檔計數(shù)和所用空間(以字節(jié)為單位)。
請注意,api-version 語法有所不同。 對于此請求,請使用 ?
來追加 api-version。 ?
將 URL 路徑與查詢字符串分隔開,而 & 將查詢字符串中的每個“名稱=值”對分隔開。 就此查詢來說,api-version 是查詢字符串中的第一個項,也是唯一項。文章來源:http://www.zghlxwxcb.cn/news/detail-760872.html
關(guān)注TechLead,分享AI全維度知識。作者擁有10+年互聯(lián)網(wǎng)服務(wù)架構(gòu)、AI產(chǎn)品研發(fā)經(jīng)驗、團隊管理經(jīng)驗,同濟本復旦碩,復旦機器人智能實驗室成員,阿里云認證的資深架構(gòu)師,項目管理專業(yè)人士,上億營收AI產(chǎn)品研發(fā)負責人。
到了這里,關(guān)于Azure Machine Learning - 使用 REST API 創(chuàng)建 Azure AI 搜索索引的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!