在Azure搜索中为字典属性编制索引 [英] Index a dictionary property in azure search

查看:97
本文介绍了在Azure搜索中为字典属性编制索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DTO,其属性类型为 Dictionary< string,string> .没有注释.当我上传我的DTO并调用 indexClient.Documents.Index(batch)时,我从服务返回了此错误:

I have a DTO with a property of type Dictionary<string, string>. It's not annotated. When I upload my DTO and call indexClient.Documents.Index(batch), I get this error back from the service:

该请求无效.详细信息:参数:尝试读取属性"Data"的内容时,已从JSON阅读器读取了类型为"StartObject"的节点.但是,应该有一个"StartArray"节点.

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.

我发现避免出现这种情况的唯一方法是将其设置为null.这就是我创建索引的方式:

The only way I've found to avoid it is by setting it to null. This is how I created my index:

var fields = FieldBuilder.BuildForType<DTO>();
client.Indexes.Create(new Index
{
    Name = indexName,
    Fields = fields
});

如何索引我的字典?

推荐答案

Azure认知搜索不支持行为类似于字典等松散类型的属性包的字段.索引中的所有字段都必须具有定义明确的 EDM类型.

Azure Cognitive Search doesn't support fields that behave like loosely-typed property bags like dictionaries. All fields in the index must have a well-defined EDM type.

如果您在设计时不知道可能的字段集,则有几种选择,但是它们带有很大的警告:

If you don't know the set of possible fields at design-time, you have a couple options, but they come with big caveats:

  1. 在应用程序代码中,在建立文档索引的过程中发现新字段时,将它们添加到索引定义中.更新索引会增加整个写入路径的延迟,因此,根据添加新字段的频率,这可能是可行的,也可能是不现实的.
  2. 将动态"字段建模为一组名称/值收集字段,每种所需的数据类型对应一个.例如,如果发现一个新的字符串字段"color",其值为"blue",则您上载的文档可能如下所示:

{
    "id": "123",
    "someOtherField": 3.5,
    "dynamicStringFields": [
        {
            "name": "color",
            "value": "blue"
        }
    ]
}

方法1可能会碰到限制每个索引的最大字段数.

方法2可能会遇到限制每个文档中所有复杂集合中元素的最大数量.它还使查询模型复杂化,尤其是在您可能需要

Approach #2 risks bumping into the 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 semantics in queries.

这篇关于在Azure搜索中为字典属性编制索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆