Dictionary <string,list <string>&gt;的NEST映射

时间:2017-01-06 23:14:05

标签: dictionary elasticsearch mapping nest

我有一个以Dictionary<string, List<string>>为成员的班级。我可以使用automap插入数据,但无法使用字典中的键和/或值查询文档。如果我使用匹配查询,它将返回索引中的所有内容。我尝试使用术语,嵌套/非嵌套查询和QueryString查询,但它们都没有返回任何文档。

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将字典转换为以下映射,我不确定它是否正确表示我正在寻找的内容。

"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"
        }
    }
},

1 个答案:

答案 0 :(得分:0)

自动映射时,NEST by default will map one level down in the object graph.对于Dictionary<string, List<string>>属性,字典的公共属性最终会被映射,这是不可取的。

有几种方法可以控制

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.通过流畅的映射覆盖控制映射

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

使用.Properties()会覆盖Automapping中的任何推断映射。