【问题标题】:Azure Search SDK: how to specify data sourceAzure 搜索 SDK:如何指定数据源
【发布时间】:2017-11-02 07:53:32
【问题描述】:

我们已经厌倦了在开发过程中手动创建 Azure 搜索索引,因此我正在编写一个控制台应用程序来为我们做这件事。我们已经在 Azure 中有一个数据源,它是一个数据库视图。该视图将提供索引。我的问题是如何在创建索引时指定该数据源?到目前为止,这是我的代码(不包括 Employee 类定义):

using Microsoft.Azure.Search;
using Microsoft.Azure.Search.Models;

namespace AdvancedSearchIndexGenerator
{
    class Program
    {
        static void Main()
        {
            SearchServiceClient client = new SearchServiceClient(Util.AzureSearchServiceName, new SearchCredentials(Util.AzureSearchApiKey));

            var definition = new Index()
            {
                Name = Util.AzureSearchIndexName,
                Fields = FieldBuilder.BuildForType<Employee>()
            };

            client.Indexes.Create(definition);
        }
    }
}

【问题讨论】:

    标签: c# azure azure-cognitive-search azure-search-.net-sdk


    【解决方案1】:

    创建 Azure 搜索数据源是创建索引后的单独步骤

    您可以通过 2 种方式创建 Azure 搜索数据源:

    1. 使用Azure Search Service REST API
    2. 在 C# 代码中使用 Microsoft.Azure.Search NuGet 包

    使用的 NuGet 包(相当旧,这些包的新版本可能有不同的实现):

    • 包 id="Microsoft.Azure.Search" 版本="1.1.1" targetFramework="net461"
    • 包 id="Microsoft.Rest.ClientRuntime.Azure" 版本="2.5.2" targetFramework="net45"

    下面编写示例 C# 代码以使用 CosmosDB 创建 Azure 搜索数据源:

    using Microsoft.Azure.Search.Models;
    using Microsoft.Rest.Azure;
    
    DataSource dataSource = CreateDataSource(sqlQuery, collectionName, indexName, dataSourceName, dataSourceConnectionString);
    AzureOperationResponse<DataSource> operation = await client.DataSources.CreateOrUpdateWithHttpMessagesAsync(dataSource);
    
    private DataSource GetDataSource(string sqlQuery, string collectionName, string indexName, string dataSourceName, string dataSourceConnectionString)
    {
        DataSource dataSource = new DataSource();
        dataSource.Name = dataSourceName;
        dataSource.Container = GetDataSourceContainer(sqlQuery, collectionName);
        dataSource.Credentials = new DataSourceCredentials(dataSourceConnectionString);
        dataSource.Type = "documentdb";
        return dataSource;
    }       
    
    private DataContainer GetDataSourceContainer(string sqlQuery, string collectionName)
    {
        DataContainer container = new DataContainer();
        container.Query = sqlQuery;
        container.Name = collectionName;
        return container;
    }
    

    【讨论】:

    • 其实数据源是独立于索引的。我们的已经存在,我们不打算放弃或重新创建它。我只需要知道如何将我的新索引(我正在以编程方式创建)指向该数据源。你的回答不包括那个。
    • 我不好,那是 Microsoft.Azure.Search.Models.Indexer 吗?您可以在其中提及 TargetIndexName 和 DataSourceName
    【解决方案2】:

    创建索引时不指定数据源。您可以在创建索引器时指定数据源,该索引器将从数据源中提取数据并将其推送到索引中。

    您会注意到您可以将 dataSourceName 作为参数传递给Indexer creation

    谢谢,

    路易斯·卡布雷拉 | Azure 搜索

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-13
      • 2019-12-01
      • 1970-01-01
      • 2021-03-01
      相关资源
      最近更新 更多