在 Azure 搜索中索引字典 属性
Index a dictionary property in azure search
我有一个 属性 类型为 Dictionary<string, string>
的 DTO。它没有注释。当我上传我的 DTO 并调用 indexClient.Documents.Index(batch)
时,我从服务返回此错误:
The request is invalid. Details: parameters : A node of type 'StartObject' was read from the JSON reader when trying to read the contents of the property 'Data'; however, a 'StartArray' node was expected.
我发现避免它的唯一方法是将它设置为空。这就是我创建索引的方式:
var fields = FieldBuilder.BuildForType<DTO>();
client.Indexes.Create(new Index
{
Name = indexName,
Fields = fields
});
如何为我的词典编制索引?
Azure 认知搜索不支持行为类似于松散类型 属性 包(如字典)的字段。索引中的所有字段必须具有明确定义的 EDM type.
如果您在设计时不知道可能的字段集,您有几个选择,但它们有很大的警告:
- 在您的应用程序代码中,在索引文档时发现新字段时将其添加到索引定义中。更新索引会增加整体写入路径的延迟,因此根据添加新字段的频率,这可能实用也可能不实用。
- 将您的 "dynamic" 字段建模为一组 name/value 集合字段,每个字段对应一种所需的数据类型。例如,如果发现一个新的字符串字段 "color",其值为 "blue",您上传的文档可能如下所示:
{
"id": "123",
"someOtherField": 3.5,
"dynamicStringFields": [
{
"name": "color",
"value": "blue"
}
]
}
方法 #1 有撞到 limit on the maximum number of fields per index 的风险。
方法 #2 有可能在查询中遇到 limit on the maximum number of elements across all complex collections per document. It also complicates the query model, especially for cases where you might want correlated 语义。
我有一个 属性 类型为 Dictionary<string, string>
的 DTO。它没有注释。当我上传我的 DTO 并调用 indexClient.Documents.Index(batch)
时,我从服务返回此错误:
The request is invalid. Details: parameters : A node of type 'StartObject' was read from the JSON reader when trying to read the contents of the property 'Data'; however, a 'StartArray' node was expected.
我发现避免它的唯一方法是将它设置为空。这就是我创建索引的方式:
var fields = FieldBuilder.BuildForType<DTO>();
client.Indexes.Create(new Index
{
Name = indexName,
Fields = fields
});
如何为我的词典编制索引?
Azure 认知搜索不支持行为类似于松散类型 属性 包(如字典)的字段。索引中的所有字段必须具有明确定义的 EDM type.
如果您在设计时不知道可能的字段集,您有几个选择,但它们有很大的警告:
- 在您的应用程序代码中,在索引文档时发现新字段时将其添加到索引定义中。更新索引会增加整体写入路径的延迟,因此根据添加新字段的频率,这可能实用也可能不实用。
- 将您的 "dynamic" 字段建模为一组 name/value 集合字段,每个字段对应一种所需的数据类型。例如,如果发现一个新的字符串字段 "color",其值为 "blue",您上传的文档可能如下所示:
{
"id": "123",
"someOtherField": 3.5,
"dynamicStringFields": [
{
"name": "color",
"value": "blue"
}
]
}
方法 #1 有撞到 limit on the maximum number of fields per index 的风险。
方法 #2 有可能在查询中遇到 limit on the maximum number of elements across all complex collections per document. It also complicates the query model, especially for cases where you might want correlated 语义。