Dictionary <string,object>的NEST映射

use*_*852 9 elasticsearch nest

我试图使用NEST,无法弄清楚如何与这个类一起使用它

public class Metric {   
    public DateTime Timestamp { get; set; }     
    public Dictionary<string,object> Measurement { get; set; }  
}
Run Code Online (Sandbox Code Playgroud)

我如何使用这样的类新的流畅映射?

我打算用这样的方式:

var mesurements = new Dictionary<string, object>();
mesurements["visits"] = 1;
mesurements["url"] = new string[] {"/help", "/about"};

connection.Index(new Metric() {
      Timestamp = DateTime.UtcNow, 
      Measurement = mesurements
});
Run Code Online (Sandbox Code Playgroud)

是否可以针对字典编写查询?如果我想从昨天开始使用关键名称为"访问"的mesurenemt获取所有度量标准,那么它将如何?

Mar*_*man 12

你不要have使用映射,在这种情况下你可以很好地依赖elasticsearch的无模式特性.

json序列化器会将其写为:

{
    "timestamp" : "[datestring]",
    "measurement" : {
        "visits" : 1,
        "url" : [ "/help", "/about"]
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以使用NEST查询"measurement.visits"字段的存在.

var result = client.Search<Metric>(s=>s
    .From(0)
    .Size(10)
    .Filter(filter=>filter
        .Exists("measurement.visits")
    )
);
Run Code Online (Sandbox Code Playgroud)

result.Documents现在使用字典中的visits键来保存前10个指标Measurement.

如果您确实希望使用新的流畅映射显式映射该字典中的可能键:

var result = client.MapFluent<Metric>(m => m
    .Properties(props => props
        .Object<Dictionary<string,object>>(s => s
            .Name(p => p.Measurement)
            .Properties(pprops => pprops
                .Number(ps => ps
                    .Name("visits")
                    .Type(NumberType.@integer)
                )
                .String(ps => ps
                    .Name("url")
                    .Index(FieldIndexOption.not_analyzed))
                )
            )
        )
    )
);
Run Code Online (Sandbox Code Playgroud)

请记住,我们没有使用此映射关闭动态映射,因此您仍然可以将其他键插入字典而不会破坏elasticsearch.只有现在,elasticsearch才会知道访问是一个实际的整数,我们不想分析网址值.

因为我们没有使用任何类型的访问器(类型为.Name()调用Metric).Object<Dictionary<string,object>>也可以.Object<object>.