Mapping the NEST dictionary <string, object>
I am trying to use NEST and cannot figure out how to use it with this class
public class Metric { public DateTime Timestamp { get; set; } public Dictionary<string,object> Measurement { get; set; } } How can I use the new smooth mapping with such a class?
I plan to use it as follows:
var mesurements = new Dictionary<string, object>(); mesurements["visits"] = 1; mesurements["url"] = new string[] {"/help", "/about"}; connection.Index(new Metric() { Timestamp = DateTime.UtcNow, Measurement = mesurements }); Can I write a dictionary query? If I wanted to get all the metrics from yesterday using mesurenemt with the key name "visit", what would it look like?
You do not use have to use matching, in which case you can rely on the true elasticsearch scheme.
The json serial analyzer will write this as:
{ "timestamp" : "[datestring]", "measurement" : { "visits" : 1, "url" : [ "/help", "/about"] } } You can query for the existence of the "measurement.visits" field using NEST.
var result = client.Search<Metric>(s=>s .From(0) .Size(10) .Filter(filter=>filter .Exists("measurement.visits") ) ); result.Documents now holds the first 10 metrics with the visits key in the Measurement dictionary.
If you want to explicitly display possible keys in this dictionary using the new smooth mapping:
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)) ) ) ) ) ); Remember that we did not turn off dynamic matching with this matching, so that you can insert other keys into the dictionary without breaking up elasticsearch. Only now elasticsearch will know that visits are a real integer and we donβt want to parse URL values.
since we donβt use any typed accessories (a call of type .Name () is entered in Metric ) .Object<Dictionary<string,object>> can also be .Object<object> .