-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
Let's say there are two int values in elasticsearch called "IntValue1" and "IntValue2".
I now would like to get them individually with different searches (don't ask why, this is just a simplified example).
To make it easier, there will be a helper method which takes an object-path and just returns the first found field as integer.
This could look like: (Note: MyElasticObject is just the class for the mapping)
private int GetValue(Expression<Func<MyElasticObject, object>> field)
{
ISearchRequest searchRequest = new SearchDescriptor<MyElasticObject>();
searchRequest.Size = 1;
searchRequest.Fields = new[] { Nest.Resolvers.Property.Path(field) };
var response = Search(searchRequest);
if (response.FieldSelections.Any())
{
var fields = response.FieldSelections.First();
var fieldValues = fields.FieldValues(field);
var fieldValue = fieldValues.First();
return (int) fieldValue;
}
return 0;
}This should then be used like this:
var val1 = GetValue(x => x.IntValue1);
var val2 = GetValue(x => x.IntValue2);The problem there is, that the casting to int failed because it's actually just unboxing the fieldValue which was is of type long.
Issues / Questions:
- Is there any other possibility to fill the searchRequest.Fields with the objectPath of the correct Type (Expression<Func<MyElasticObject, int>>, does not work because Nest.Resolvers.Property.Path only accepts the one with object)?
- There exists a generic fields.FieldValues where the type can be passed but this also does not work if the object path is not of type object
- Shouldn't the values in the FieldSelections be according to the ElasticMapping? The mapping defined IntValue1 and IntValue2 as integer but the value (casted to object) in FieldValues are longs.
As a QuickFix, I can use Convert.ToInt32(), but for me it seems not right that an int value is saved as long and then needs to be converted to int again.
Any help / feedback would be great!
Cheers,
Roman