Quickstart: Create a search index in PowerShell by using REST APIs
In this Azure AI Search quickstart, learn how to create, load, and query a search index by using PowerShell and the Azure AI Search REST APIs. This article explains how to run PowerShell commands interactively. Alternatively, you can download and run a PowerShell script that performs the same operations.
If you don't have an Azure subscription, create a free account before you begin.
Prerequisites
The following services and tools are required for this quickstart:
- PowerShell 7.3 or later, using Invoke-RestMethod for sequential and interactive steps.
- Create an Azure AI Search service or find an existing service under your current subscription. You can use a free service for this quickstart.
Copy a search service key and URL
In this quickstart, REST calls include the service URL and an access key on every request. A search service is created with both, so if you added Azure AI Search to your subscription, follow these steps to get the necessary information.
Sign in to the Azure portal. On your search service Overview page, get the URL. An example endpoint might look like
https://mydemo.search.windows.net
.Select Settings > Keys and then get an admin key for full rights on the service. Two interchangeable admin keys are provided for business continuity if you need to roll one over. You can use either the primary or secondary key on requests for adding, modifying, and deleting objects.
All requests require an API key on every request sent to your service. Having a valid key establishes trust, on a per-request basis, between the application sending the request and the service that handles it.
Connect to Azure AI Search
In PowerShell, create a
$headers
object to store the content type and API key. Replace the admin API key (YOUR-ADMIN-API-KEY
) with a key that's valid for your search service. You only have to set this header once for the duration of the session, but you add it to every request.$headers = @{ 'api-key' = '<YOUR-ADMIN-API-KEY>' 'Content-Type' = 'application/json' 'Accept' = 'application/json' }
Create a
$url
object that specifies the service's indexes collection. Replace the service name (YOUR-SEARCH-SERVICE-NAME
) with a valid search service.$url = "https://<YOUR-SEARCH-SERVICE-NAME>.search.windows.net/indexes?api-version=2024-07-01&`$select=name"
Run
Invoke-RestMethod
to send a GET request to the service and verify the connection. AddConvertTo-Json
so that you can view the responses sent back from the service.Invoke-RestMethod -Uri $url -Headers $headers | ConvertTo-Json
If the service is empty and has no indexes, results are similar to the following example. Otherwise, you see a JSON representation of index definitions.
{ "@odata.context": "https://mydemo.search.windows.net/$metadata#indexes", "value": [ ] }
Create an index
Unless you're using the portal, an index must exist on the service before you can load data. This step defines the index and pushes it to the service. The Create Index REST API is used for this step.
Required elements of an index include a name and a fields collection. The fields collection defines the structure of a document. Each field has a name, type, and attributes that determine how it's used (for example, whether it's full-text searchable, filterable, or retrievable in search results). Within an index, one of the fields of type Edm.String
must be designated as the key for document identity.
This index is named hotels-quickstart
and has the field definitions you see in the following code. It's a subset of a larger Hotels index used in other walkthrough articles. The field definitions are trimmed in this quickstart for brevity.
Paste this example into PowerShell to create a
$body
object that contains the index schema.$body = @" { "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} ] } ] } "@
Set the URI to the indexes collection on your service and the
hotels-quickstart
index.$url = "https://<YOUR-SEARCH-SERVICE>.search.windows.net/indexes/hotels-quickstart?api-version=2024-07-01"
Run the command with
$url
,$headers
, and$body
to create the index on the service.Invoke-RestMethod -Uri $url -Headers $headers -Method Put -Body $body | ConvertTo-Json
Results should look similar to this example, which shows only the first two fields for brevity:
{ "@odata.context": "https://mydemo.search.windows.net/$metadata#indexes/$entity", "@odata.etag": "\"0x8D6EDE28CFEABDA\"", "name": "hotels-quickstart", "defaultScoringProfile": null, "fields": [ { "name": "HotelId", "type": "Edm.String", "searchable": true, "filterable": true, "retrievable": true, "sortable": true, "facetable": true, "key": true, "indexAnalyzer": null, "searchAnalyzer": null, "analyzer": null, "synonymMaps": "" }, { "name": "HotelName", "type": "Edm.String", "searchable": true, "filterable": false, "retrievable": true, "sortable": true, "facetable": false, "key": false, "indexAnalyzer": null, "searchAnalyzer": null, "analyzer": null, "synonymMaps": "" }, . . . ] }
Tip
For verification, you could also check the Indexes list in the portal.
Load documents
To push documents, use an HTTP POST request to your index's URL endpoint. The REST API for this task is Index Documents.
Paste this example into PowerShell to create a
$body
object that contains the documents you want to upload.This request includes two full records and one partial record. The partial record demonstrates that you can upload incomplete documents. The
@search.action
parameter specifies how indexing is done. Valid values includeupload
,merge
,mergeOrUpload
, anddelete
. ThemergeOrUpload
behavior either creates a new document forhotelId = 3
or updates the contents if it already exists.$body = @" { "value": [ { "@search.action": "upload", "HotelId": "1", "HotelName": "Stay-Kay City Hotel", "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": "Old Century Hotel", "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": "Gastronomic 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 Palace Hotel", "Description": "Sublime Palace 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 Palace 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" } } ] } "@
Set the endpoint to the
hotels-quickstart
docs collection and include the index operation (indexes/hotels-quickstart/docs/index
).$url = "https://<YOUR-SEARCH-SERVICE>.search.windows.net/indexes/hotels-quickstart/docs/index?api-version=2024-07-01"
Run the command with
$url
,$headers
, and$body
to load documents into thehotels-quickstart
index.Invoke-RestMethod -Uri $url -Headers $headers -Method Post -Body $body | ConvertTo-Json
Results should look similar to the following example. You should see a status code of 201.
{ "@odata.context": "https://mydemo.search.windows.net/indexes(\u0027hotels-quickstart\u0027)/$metadata#Collection(Microsoft.Azure.Search.V2019_05_06.IndexResult)", "value": [ { "key": "1", "status": true, "errorMessage": null, "statusCode": 201 }, { "key": "2", "status": true, "errorMessage": null, "statusCode": 201 }, { "key": "3", "status": true, "errorMessage": null, "statusCode": 201 }, { "key": "4", "status": true, "errorMessage": null, "statusCode": 201 } ] }
Search an index
This step shows you how to query an index by using the Search Documents API.
Be sure to use single quotation marks on search $urls
. Query strings include $
characters, and you can omit having to escape them if the entire string is enclosed in single quotation marks.
Set the endpoint to the
hotels-quickstart
docs collection and add asearch
parameter to pass in a query string.This string executes an empty search (
search=*
), returning an unranked list (search score = 1.0) of arbitrary documents. By default, Azure AI Search returns 50 matches at a time. As structured, this query returns an entire document structure and values. Add$count=true
to get a count of all documents in the results.$url = 'https://<YOUR-SEARCH-SERVICE>.search.windows.net/indexes/hotels-quickstart/docs?api-version=2024-07-01&search=*&$count=true'
Run the command to send the
$url
to the service.Invoke-RestMethod -Uri $url -Headers $headers | ConvertTo-Json
Results should look similar to the following output:
{ "@odata.context": "https://mydemo.search.windows.net/indexes(\u0027hotels-quickstart\u0027)/$metadata#docs(*)", "@odata.count": 4, "value": [ { "@search.score": 0.1547872, "HotelId": "2", "HotelName": "Old Century Hotel", "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.6, "Address": "@{StreetAddress=140 University Town Center Dr; City=Sarasota; StateProvince=FL; PostalCode=34243; Country=USA}" }, { "@search.score": 0.009068266, "HotelId": "3", "HotelName": "Gastronomic 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\u0027s restaurant services.", "Category": "Resort and Spa", "Tags": "air conditioning bar continental breakfast", "ParkingIncluded": true, "LastRenovationDate": "2015-09-20T00:00:00Z", "Rating": 4.8, "Address": "@{StreetAddress=3393 Peachtree Rd; City=Atlanta; StateProvince=GA; PostalCode=30326; Country=USA}" }, . . . ] }
Try a few other query examples to get a feel for the syntax. You can do a string search, verbatim $filter
queries, limit the results set, scope the search to specific fields, and more.
# Query example 1
# Search the entire index for the terms 'restaurant' and 'wifi'
# Return only the HotelName, Description, and Tags fields
$url = 'https://<YOUR-SEARCH-SERVICE>.search.windows.net/indexes/hotels-quickstart/docs?api-version=2024-07-01&search=restaurant wifi&$count=true&$select=HotelName,Description,Tags'
# Query example 2
# Apply a filter to the index to find hotels rated 4 or higher
# Returns the HotelName and Rating. Two documents match.
$url = 'https://<YOUR-SEARCH-SERVICE>.search.windows.net/indexes/hotels-quickstart/docs?api-version=2024-07-01&search=*&$filter=Rating gt 4&$select=HotelName,Rating'
# Query example 3
# Take the top two results, and show only HotelName and Category in the results
$url = 'https://<YOUR-SEARCH-SERVICE>.search.windows.net/indexes/hotels-quickstart/docs?api-version=2024-07-01&search=boutique&$top=2&$select=HotelName,Category'
# Query example 4
# Sort by a specific field (Address/City) in ascending order
$url = 'https://<YOUR-SEARCH-SERVICE>.search.windows.net/indexes/hotels-quickstart/docs?api-version=2024-07-01&search=pool&$orderby=Address/City asc&$select=HotelName, Address/City, Tags, Rating'
Clean up resources
When you're working in your own subscription, it's a good idea at the end of a project to identify whether you still need the resources you created. Resources left running can cost you money. You can delete resources individually or delete the resource group to delete the entire set of resources.
You can find and manage resources in the portal by using the All resources or Resource groups link in the leftmost pane.
If you're using a free service, remember that you're limited to three indexes, indexers, and data sources. You can delete individual items in the portal to stay under the limit.
Next steps
In this quickstart, you used PowerShell to step through the basic workflow for creating and accessing content in Azure AI Search. With the concepts in mind, we recommend that you move on to more advanced scenarios, such as indexing from Azure data sources: