【问题标题】:ElasticSearch/NEST indexing policy for Dictionary items字典项目的 ElasticSearch/NEST 索引策略
【发布时间】:2021-02-09 12:59:18
【问题描述】:

我有一个包含字段的文档模型,该字段是一个字典,可以采用任何值,如下所示:

public class Document
{
    public string Id { get; set; }

    public string Description { get; set; }

    public Dictionary<string, object> Permissions { get; set; }

    public Dictionary<string, object> Metadata { get; set; }
}

例如

client.IndexDocument(new Document
{
    Id = "4",
    Description = "ordinary document",
    Metadata = new Dictionary<string, object>
    {
        { "publish_date", new DateTime(1930, 10, 11) },
        { "author_country", "RU" },
        { "salary", 2590.00 },
        { "likes", 23 },
    }
})

映射是自动完成的,因此默认分析器会更改字典的值,例如:将字符串更改为小写或删除破折号,因此此代码不会返回任何结果:

var result = client.Search<Document>(s => s
    .Query(q =>
        q.Term(new Field("metadata.author_country"), "RU"))
    );

如何为任何元数据值禁用此行为?或者设置自定义分析器?还是仅对给定类型的值禁用它?

【问题讨论】:

    标签: c# elasticsearch nest


    【解决方案1】:

    您应该将文档字段的映射设置为正确的类型。 在此处查看更多信息: https://www.elastic.co/guide/en/elasticsearch/reference/7.10/mapping.html 这里列出了所有类型: https://www.elastic.co/guide/en/elasticsearch/reference/7.10/mapping-types.html

    要在 Nest 中执行此操作,您需要在文档定义中添加正确的属性。

    在此处查看详细信息: https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/attribute-mapping.html

    如果我理解您要执行的操作,那么您需要将元数据类型设置为嵌套,因为您要搜索元数据:

    https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html

    【讨论】:

    • Elastic 对所有字段进行自动映射 - 并且运行正常。我只想自定义这种行为。我刚刚观察到另一件奇怪的事情。嵌套字段仅在命名为 Metadata 时动态映射。当我添加另一个具有相同Dictionary&lt;string, object&gt; 类型(Dictionary&lt;string, object&gt; Permissions {get;set;})的嵌套字段时,它没有被映射为已执行(它的元素被映射到unknown type)。这是为什么?我该如何更改?
    • 只用 Document 类定义中的属性更改映射:elastic.co/guide/en/elasticsearch/client/net-api/current/…
    • 你能更精确一点吗?我尝试将 [Object][Nested] 添加到我的 Permissions 属性中 - 在重新创建索引 kibana 后,仍然会在此属性的值上显示 unknown type
    • 我创建了一个屏幕截图来可视化我的问题:ibb.co/qrRNch9
    • 权限字段配置在文档下?它应该像元数据一样自动映射它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    • 2016-06-17
    • 2014-11-12
    • 1970-01-01
    • 1970-01-01
    • 2017-04-02
    相关资源
    最近更新 更多