Skip to content

Field Value with Object-Path of type Object for int field is long #991

@Roemer

Description

@Roemer

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:

  1. 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)?
  2. 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
  3. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions