将2个Azure SQL表合并为Azure认知搜索中的1个索引

时间:2019-12-20 13:43:34

标签: 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)));

“房间”表包含一列“ HotelId”,指向其所属酒店的ID。

结果应该是,房间索引器中的“房间”列表由“房间索引器”填充了“房间”,但是实际结果是,“房间”与“酒店”一起被索引,就好像它们本身就是“酒店”一样。 “房间”列表保持空白。

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

1 个答案:

答案 0 :(得分:0)

Azure Search不支持附加到集合字段(“酒店”索引中的“房间”)-看来您已经对数据建模了,期望如此。

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