在 Azure 认知搜索中将 2 个 Azure SQL 表组合成 1 个索引

Combine 2 Azure SQL tables into 1 index in Azure Cognitive Search

我正在关注 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)));

房间 table 包含一列 'HotelId',它指向其所属酒店的 ID。

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

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

Azure 搜索不支持附加到集合字段(Hotels 索引中的房间)- 您似乎已经对数据建模并期望它支持。

相反,您可以尝试将酒店中的所有房间拼合到一个字段中(可能是 Azure 搜索可以使用的字符串化 JSON 表示形式)。