c# - 在 Azure 认知搜索中将 2 个 Azure SQL 表合并为 1 个索引

标签 c# azure azure-sql-database azure-cognitive-search azure-cognitive-services

我正在关注this example关于如何将 2 个不同的数据源合并到 1 个索引中,结果正是我想要的: 来自一个数据源的索引酒店,每个酒店都带有来自另一数据源的房间详细信息数组(或列表)。

就我而言,我的两个数据源都来自一个不使用分区键的 Azure SQL 数据库:

        DataSource hotelSource = DataSource.AzureSql(
            name: "hotels-sql",
            sqlConnectionString: Configuration["ConnectionStrings"],
            tableOrViewName: "hotels");
        hotelSource.DataChangeDetectionPolicy = new SqlIntegratedChangeTrackingPolicy();

        DataSource roomSource = DataSource.AzureSql(
            name: "rooms-sql",
            sqlConnectionString: Configuration["ConnectionStrings"],
            tableOrViewName: "rooms");
        roomSource.DataChangeDetectionPolicy = new SqlIntegratedChangeTrackingPolicy();

我像这样设置索引:

    fields = new List<Field>
    {
        Field.New("Id", DataType.String, isKey: true),
        Field.New("Name", DataType.String, isSearchable: true, isRetrievable: false, analyzerName: AnalyzerName.StandardLucene),
        Field.New("Description", DataType.String, isSearchable: true, isRetrievable: false, analyzerName: AnalyzerName.StandardLucene),
        Field.New("Category", DataType.String, isRetrievable: false, isFilterable: true),
        new Field("Rooms", DataType.Collection(DataType.Complex), new List<Field>
        {
            Field.New("Name", DataType.String, isRetrievable: false, isSearchable: true, analyzerName: AnalyzerName.StandardLucene),
            Field.New("Description", DataType.String, isRetrievable: false, isSearchable: true, analyzerName: AnalyzerName.StandardLucene),
            Field.New("Category", DataType.String, isRetrievable: false, isFilterable: true)
        })
    };

    var definition = new Index()
    {
        Name = indexName,
        Fields = fields,
        ScoringProfiles = new List<ScoringProfile>
        {
            new ScoringProfile("main", new TextWeights(new Dictionary<string, double>
            {
                {"Name", 1},
                {"Description", 0.8},
                {"Rooms/Name", 0.4},
                {"Rooms/Description", 0.3}
            }))
        },
        DefaultScoringProfile = "main"
    };

    Index index = searchService.Indexes.Create(definition);

我像这样设置映射:

Indexer hotelIndexer = new Indexer(
                name: "hotels-indexer",
                dataSourceName: hotelSource.Name,
                targetIndexName: index.Name,
                schedule: new IndexingSchedule(TimeSpan.FromMinutes(5)));

List<FieldMapping> map = new List<FieldMapping> {
                new FieldMapping("HotelId", "Id")
            };

Indexer roomIndexer = new Indexer(
                name: "rooms-indexer",
                dataSourceName: roomSource.Name,
                targetIndexName: index.Name,
                fieldMappings: map,
                schedule: new IndexingSchedule(TimeSpan.FromMinutes(5)));

Rooms 表包含一列“HotelId”,它指向它所属的酒店的 ID。

结果应该是酒店索引中的房间列表由 roomIndexer 填充房间,但实际结果是房间与酒店一起索引,就好像它们本身就是酒店一样。房间列表仍为空。

我希望我提供了足够的信息。

最佳答案

Azure 搜索不支持附加到集合字段(“酒店”索引中的“房间”) - 看来您已经对数据进行了建模,期望它能做到这一点。

相反,您可以尝试将酒店中的所有房间扁平化为单个字段(可能采用 Azure 搜索可以使用的字符串化 JSON 表示形式)。

关于c# - 在 Azure 认知搜索中将 2 个 Azure SQL 表合并为 1 个索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59426235/

相关文章:

C# 值/引用传递?

c# - 检测简单的触摸手势

c - 如何在Raspberry Pi上运行Azure IoT C SDK的端到端测试?

python - 监听来自 python 的 Azure 队列消息以进行 Web 作业

azure - "Turn off"Azure 应用服务和 Azure SQL Server 以降低成本

sql-server - 用于接收 XML 数据并导入 SQL 数据库的 Azure 服务

c# - WCF 属性和 linq to sql

azure - Azure 中 Redis 缓存的一致超时

sql-server - Azure SQL 数据库时间点恢复需要超过 24 小时

c# - 从其他应用程序的入口点启动应用程序android