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