Azure AI 搜索设置指南

本指南将帮助你使用 Azure 门户设置 Azure AI 搜索。请按照以下步骤创建和配置 Azure AI 搜索服务。

先决条件

在开始之前,请确保您具备以下条件:

  • Azure 订阅。如果没有 Azure 订阅,可以在 Azure 免费帐户 创建免费帐户。

第 1 步:创建 Azure 存储帐户

  1. 按照创建 Azure 存储帐户 说明创建新的 Azure 存储帐户。
    注意:确保存储帐户的类型是标准通用 V2。

步骤 2:创建 Azure AI 搜索服务

  1. 登录Azure 门户
  2. 在左侧导航窗格中,单击“创建资源”。
  3. 在搜索框中,键入“Azure AI 搜索”,然后从结果列表中选择 Azure AI 搜索
  4. 单击创建按钮。
  5. Basics 选项卡中,提供以下信息:
    • 订阅:选择您的 Azure 订阅。
    • 资源组:创建新资源组或选择现有资源组。
    • 资源名称:输入搜索服务的唯一名称。
    • 区域:选择最接近您的用户的区域。
    • 定价层:选择适合您要求的定价层。您可以从免费套餐开始进行测试。
  6. 单击“查看 + 创建”。
  7. 检查设置并单击“创建”以创建搜索服务。

步骤 3:开始使用 Azure AI 搜索

  1. 部署完成后,导航到 Azure 门户中的搜索服务。
  2. 在搜索服务概述窗格中,复制 URL。它应该看起来像 https://<service-name>.search.windows.net
  3. 在设置 > 键窗格中,复制查询键。
  4. 按照快速入门指南页面中的步骤创建索引、上传数据并执行搜索查询。

步骤 4:使用 Azure AI 搜索工具

Azure AI 搜索与各种工具集成以增强搜索功能。您可以使用Azure CLI、Python SDK、.NET SDK等工具进行高级配置和操作。

使用 Azure CLI

  1. 按照安装 Azure CLI 中的说明安装 Azure CLI。
  2. 使用以下命令登录 Azure CLI:
    1
    az login
  3. 将 Azure AI 搜索实例的端点和 API 密钥存储到环境变量中。
    1
    2
    3
    # zsh/bash
    export AZURE_SEARCH_SERVICE_ENDPOINT=$(az search service show -g <resource-group> -n <service-name> --query "endpoint" -o tsv)
    export AZURE_SEARCH_API_KEY=$(az search service admin-key list -g <resource-group> --search-service-name <service-name> --query "primaryKey" -o tsv)
    1
    2
    3
    # PowerShell
    $env:AZURE_SEARCH_SERVICE_ENDPOINT = az search service show -g <resource-group> -n <service-name> --query "endpoint" -o tsv
    $env:AZURE_SEARCH_API_KEY = $(az search service admin-key list -g <resource-group> --search-service-name <service-name> --query "primaryKey" -o tsv)

使用Python SDK

  1. 安装适用于 Python 的 Azure 认知搜索客户端库:
    1
    pip install azure-search-documents
  2. 使用以下Python代码创建索引并上传文档:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    import os
    from azure.core.credentials import AzureKeyCredential
    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")
    api_key = os.getenv("AZURE_SEARCH_API_KEY")
    index_name = "sample-index"

    credential = AzureKeyCredential(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)

使用.NET SDK

  1. 执行以下命令创建索引并上传文档:
    1
    dotnet run ./AzureSearch.cs
  2. 这是 AzureSearch.cs 的 .NET 代码:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    #:package Azure.Search.Documents@11.*
    #:property PublishAot=false

    using Azure;
    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 apiKey = Environment.GetEnvironmentVariable("AZURE_SEARCH_API_KEY")!;
    var indexName = "sample-index";

    var credential = new AzureKeyCredential(apiKey);
    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}'.");
    有关更详细的信息,请参阅以下文档:

结论

你已使用 Azure 门户和集成工具成功设置 Azure AI 搜索。现在,你可以探索 Azure AI 搜索的更多高级特性和功能,以增强你的搜索解决方案。

如需进一步帮助,请访问 Azure 认知搜索文档