字典< string,List< string>>的NEST映射. [英] NEST mapping of Dictionary<string,List<string>>

查看:69
本文介绍了字典< string,List< string>>的NEST映射.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,其中 Dictionary< string,List< string>> 作为成员之一.我可以使用自动映射插入数据,但是无法使用字典中的键和/或值查询文档.如果我使用匹配查询,它将返回索引中的所有内容.我尝试使用术语,嵌套/非嵌套查询和QueryString查询,但是它们都不返回任何文档.

I have a class with Dictionary<string, List<string>> as one of the members. I'm able to insert the data with automap, but unable to query the documents with the keys and/or values in the dictionary. If I use match query, it returns everything in the index. I tried using Terms, Nested/non-nested query, and QueryString query, but none of them returns any document.

class ESSchema
{
    [String(Index = FieldIndexOption.NotAnalyzed, Store = false)]
    public string Machine { get; set; }

    [String(Index = FieldIndexOption.NotAnalyzed, Store = false)]
    public string Filename { get; set; }

    [Number(NumberType.Long, Store = false)]
    public long BatchNumber { get; set; }

    [String(Index = FieldIndexOption.NotAnalyzed, Store = false)]
    public string Environment { get; set; } = null;

    [Nested]
    //[Object(Store = false)]
    public Dictionary<string, List<string>> KeysValues { get; set; }
}

自动映射将字典转换为以下不确定的映射,我不确定它是我所寻找的内容的正确表示.

Automap converts the dictionary to the following mapping which I'm not sure is the correct representation of what I'm looking for.

"keysValues": {
    "type": "nested",
    "properties": {
        "comparer": {
            "properties": {

            },
            "type": "object"
        },
        "count": {
            "type": "integer"
        },
        "keys": {
            "properties": {
                "count": {
                    "type": "integer"
                }
            },
            "type": "object"
        },
        "values": {
            "properties": {
                "count": {
                    "type": "integer"
                }
            },
            "type": "object"
        },
        "item": {
            "type": "string"
        }
    }
},

推荐答案

自动映射时,

When Automapping, NEST by default will map one level down in the object graph. In the case of your Dictionary<string, List<string>> property, the public properties of the dictionary end up being mapped, which is undesirable.

有两种方法可以控制

1.将 -1 用于 maxRecursion 传递给 AutoMap()

class ESSchema
{
    [String(Index = FieldIndexOption.NotAnalyzed, Store = false)]
    public string Machine { get; set; }

    [String(Index = FieldIndexOption.NotAnalyzed, Store = false)]
    public string Filename { get; set; }

    [Number(NumberType.Long, Store = false)]
    public long BatchNumber { get; set; }

    [String(Index = FieldIndexOption.NotAnalyzed, Store = false)]
    public string Environment { get; set; } = null;

    [Object(Store = false)]
    public Dictionary<string, List<string>> KeysValues { get; set; }
}

client.Map<ESSchema>(m => m
    .AutoMap(-1)
);

这将导致

{
  "properties": {
    "machine": {
      "type": "string",
      "store": false,
      "index": "not_analyzed"
    },
    "filename": {
      "type": "string",
      "store": false,
      "index": "not_analyzed"
    },
    "batchNumber": {
      "type": "long",
      "store": false
    },
    "environment": {
      "type": "string",
      "store": false,
      "index": "not_analyzed"
    },
    "keysValues": {
      "type": "object",
      "store": false,
      "properties": {}
    }
  }
}

2.通过流畅的贴图覆盖控制贴图

2.Controlling the mapping through fluent mapping overrides

client.Map<ESSchema>(m => m
    .AutoMap()
    .Properties(p => p
        .Object<Dictionary<string, List<string>>>(o => o
            .Name(n => n.KeysValues)
            .Store(false)
        )
    )
);

使用 .Properties()会覆盖从自动映射中推断出的所有映射.

Using .Properties() overrides any inferred mapping from Automapping.

这篇关于字典&lt; string,List&lt; string&gt;&gt;的NEST映射.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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