Ta vodnik vam bo pomagal nastaviti Azure AI Search z uporabo Azure portala. Sledite spodnjim korakom za ustvarjanje in konfiguracijo vaše storitve Azure AI Search.
Preden začnete, zagotovite, da imate naslednje:
https://<service-name>.search.windows.net.Azure AI Search se integrira z različnimi orodji za izboljšanje vaših iskalnih zmogljivosti. Lahko uporabljate Azure CLI, Python SDK, .NET SDK in druga orodja za napredne nastavitve in operacije.
Prijavite se v Azure CLI z ukazom:
az login
(Priporočeno) Omogočite dostop brez ključa z Microsoft Entra ID (RBAC):
az search service update --name <service-name> --resource-group <resource-group> --auth-options aadOrApiKey
az role assignment create --assignee <your-user-or-principal-id> --role "Search Service Contributor" --scope $(az search service show -g <resource-group> -n <service-name> --query id -o tsv)
az role assignment create --assignee <your-user-or-principal-id> --role "Search Index Data Contributor" --scope $(az search service show -g <resource-group> -n <service-name> --query id -o tsv)
# az search service show has no "endpoint" field; build the URL from the service name.
export AZURE_SEARCH_SERVICE_ENDPOINT="https://<service-name>.search.windows.net"
Z omogočenim RBAC, primeri Python in .NET SDK spodaj preverjajo pristnost z DefaultAzureCredential, ki med lokalnim razvojem uporablja vašo az login sejo — ni potreben administratorski ključ. Oglejte si Povezava z Azure AI Search z uporabo vlog.
# az search service show has no "endpoint" field; build the URL from the service name.
export AZURE_SEARCH_SERVICE_ENDPOINT="https://<service-name>.search.windows.net"
export AZURE_SEARCH_API_KEY=$(az search admin-key show -g <resource-group> --service-name <service-name> --query "primaryKey" -o tsv)
# az search service show has no "endpoint" field; build the URL from the service name.
$env:AZURE_SEARCH_SERVICE_ENDPOINT = "https://<service-name>.search.windows.net"
$env:AZURE_SEARCH_API_KEY = $(az search admin-key show -g <resource-group> --service-name <service-name> --query "primaryKey" -o tsv)
Namestite knjižnico Azure Cognitive Search klient in Azure Identity za Python:
pip install azure-search-documents azure-identity
Uporabite naslednjo kodo v Pythonu za ustvarjanje indeksa in nalaganje dokumentov:
import os
from azure.identity import DefaultAzureCredential
from azure.search.documents import SearchClient
from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents.indexes.models import SearchIndex, SimpleField, edm
service_endpoint = os.getenv("AZURE_SEARCH_SERVICE_ENDPOINT")
index_name = "sample-index"
# Keyless (recommended): uses your `az login` identity via Entra ID RBAC.
# Requires the "Search Service Contributor" and "Search Index Data Contributor" roles.
credential = DefaultAzureCredential()
# Fallback (key-based auth):
# from azure.core.credentials import AzureKeyCredential
# credential = AzureKeyCredential(os.getenv("AZURE_SEARCH_API_KEY"))
index_client = SearchIndexClient(service_endpoint, credential)
fields = [
SimpleField(name="id", type=edm.String, key=True),
SimpleField(name="content", type=edm.String, searchable=True),
]
index = SearchIndex(name=index_name, fields=fields)
index_client.create_index(index)
search_client = SearchClient(service_endpoint, index_name, credential)
documents = [
{"id": "1", "content": "Hello world"},
{"id": "2", "content": "Azure Cognitive Search"}
]
search_client.upload_documents(documents)
Zaženite naslednji ukaz za ustvarjanje indeksa in nalaganje dokumentov:
dotnet run ./AzureSearch.cs
Primer .NET spodaj uporablja DefaultAzureCredential, ki lahko med lokalnim razvojem uporablja vašo prijavo v Azure CLI iz az login.
Tukaj je .NET koda za AzureSearch.cs:
#:package Azure.Search.Documents@11.*
#:package Azure.Identity@1.21.0
#:property PublishAot=false
using Azure;
using Azure.Identity;
using Azure.Search.Documents;
using Azure.Search.Documents.Indexes;
using Azure.Search.Documents.Indexes.Models;
var serviceEndpoint = new Uri(Environment.GetEnvironmentVariable("AZURE_SEARCH_SERVICE_ENDPOINT")!);
var indexName = "sample-index";
// Keyless (recommended): uses your `az login` identity via Entra ID RBAC.
// Requires the "Search Service Contributor" and "Search Index Data Contributor" roles.
var credential = new DefaultAzureCredential();
// Fallback (key-based auth): the `using Azure;` directive above already imports
// AzureKeyCredential; replace the credential line above with:
// var credential = new AzureKeyCredential(Environment.GetEnvironmentVariable("AZURE_SEARCH_API_KEY")!);
var indexClient = new SearchIndexClient(serviceEndpoint, credential);
var fields = new List<SearchField>()
{
new SimpleField("id", SearchFieldDataType.String) { IsKey = true },
new SearchableField("content")
};
var index = new SearchIndex(name: indexName, fields: fields);
var response = await indexClient.CreateOrUpdateIndexAsync(index);
Console.WriteLine($"Index '{response.Value.Name}' ready.");
var searchClient = new SearchClient(serviceEndpoint, indexName, credential);
var documents = new[]
{
new { id = "1", content = "Hello world" },
new { id = "2", content = "Azure Cognitive Search" }
};
var result = await searchClient.UploadDocumentsAsync(documents);
Console.WriteLine($"Uploaded {result.Value.Results.Count} documents to index '{response.Value.Name}'.");
Za bolj podrobne informacije si oglejte naslednjo dokumentacijo:
Uspešno ste nastavili Azure AI Search z uporabo Azure portala in integriranih orodij. Zdaj lahko raziščete bolj napredne funkcije in zmogljivosti Azure AI Search za izboljšanje vaših iskalnih rešitev.
Za nadaljnjo pomoč obiščite Dokumentacijo Azure Cognitive Search.
Omejitev odgovornosti: Ta dokument je bil preveden z uporabo AI prevajalske storitve Co-op Translator. Čeprav si prizadevamo za natančnost, vas prosimo, da upoštevate, da avtomatizirani prevodi lahko vsebujejo napake ali netočnosti. Izvirni dokument v njegovem izvirnem jeziku je treba obravnavati kot avtoritativni vir. Za kritične informacije je priporočljiv strokovni človeški prevod. Ne odgovarjamo za morebitna nesporazume ali napačne interpretacije, ki izhajajo iz uporabe tega prevoda.