Azure Search SDK:如何指定数据源

时间:2017-11-01 19:13:01

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

我们已经厌倦了在开发过程中手动创建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);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

创建Azure搜索数据源是创建索引

后的单独步骤
  

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

     
      
  1. 使用Azure Search Service REST API
  2.   
  3. 在C#Code
  4. 中使用Microsoft.Azure.Search NuGet包   

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

  • package id =&#34; Microsoft.Azure.Search&#34;版本=&#34; 1.1.1&#34; targetFramework =&#34; net461&#34;
  • package id =&#34; Microsoft.Rest.ClientRuntime.Azure&#34;版本=&#34; 2.5.2&#34; targetFramework =&#34; net45&#34;

示例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;
}

答案 1 :(得分:0)

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

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

谢谢,

Luis Cabrera | Azure搜索