代码之家  ›  专栏  ›  技术社区  ›  birdus

Azure Search SDK:如何指定数据源

  •  2
  • birdus  · 技术社区  · 8 年前

    我们已经厌倦了在开发过程中手动创建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 回复  |  直到 8 年前
        1
  •  1
  •   Community Mohan Dere    6 年前

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

    1. 使用 Azure Search Service REST API
    2. 使用Microsoft。蔚蓝色的用C代码搜索NuGet包

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

    • 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;
    }
    
        2
  •  0
  •   Luis Cabrera    8 年前

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

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

    谢谢

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