NEST Library Error - sort field descriptor does not contain a definition for <property type> dot net core (2.0)

Hi,

The following section is giving an compilation error in dot net core (2.0) - vs2017, although the same is working fine in dot net 4.5.1 - vs2015.

Error in line - sortDesc = s => s.Field(f => f.Year, sort.Direction);
Error description -
SortFieldDescriptor does not contain a definition for Year. This property is present in the class VehicleItem. (same is the case with few other properties like, mileage, vehicleprice, etc, though, these are also present).

Func<SortDescriptor, IPromise<IList>> sortDesc = null;

        if (!string.IsNullOrEmpty(sort.SortBy))
        {
            char fieldName = sort.SortBy.ElementAt(0);
            switch (fieldName)
            {
                case 'B':
                case 'Y':
                    sortDesc = s => s.Field(f => f.Year, sort.Direction);
                    break;
                default:
                    sortDesc = s => s.Field(f => f.Year, sort.Direction);
                    break;
            }
        }

Let me know, if I need to furnish any further details. Can anyone please check the same and suggest.

Thanks,
Rakesh.

What version (NuGet package version or MyGet package version) of NEST are you using?

Do you have a minimal, complete, reproducible example that demonstrates the problem?

@forloop - the version of NEST is 5.5, which also installs the ElasticSearch.NET version of 5.5. The dot net version is .NET Core 2.0. (the above details are for the application where we are having issues).
The older version, where we do have the functional code are same for NEST and Elastic Search .NET except for the dot net version. Details from package.json -
package id="Elasticsearch.Net" version="5.5.0" targetFramework="net451"
package id="NEST" version="5.5.0" targetFramework="net451"

Yes, we do have the reproducible version of the error, to simulate the error.

Can you post it here or create a gist so that it can be investigated?

image

Please refrain from posting images of code; there's no way to take that and reproduce the issue you're reporting, which only hinders the ability to help.

Can you post a minimal, complete, reproducible example that demonstrates the problem?

@forloop - it may be more appropriate at this stage, to get into an individual thread and have a discussion on the issue through screen share. Please let us know.

It looks like an issue with method overload resolution for .Field()

Cannot distinguish between

Field(Func<SortFieldDescriptor<T>, IFieldSort> sortSelector)

and

Field(Expression<Func<T, object>> field, SortOrder order)

Changing your code to specify the parameter type in the lambda expression rather than letting it be inferred should fix this e.g.

.Field((VehicleItem f) => f.Mileage, sort.Direction)

Hi, @forloop ,

I have tried the same option, but the resulting expression i.e. -
sortDesc = s => s.Field((VehicleItem f) => f.Mileage, sort.Direction);
or,
sortDesc = s => s.Field((VehicleItem f) => f.Year, sort.Direction);

is giving error like -
cannot convert lambda expression to type 'Field' because it is not a delegate type.

OK. You'll need to provide a minimal, complete, reproducible example that demonstrates the problem in order to investigate further.

From the details provided, this doesn't look like an issue with NEST, more the way that it is being used. Without further details however, it's difficult to know.

hi @forloop,

Please find the minimal, complete, reproducible example as is, shown below. There is compilation error in the Class1.cs (rows 26, 29). (and I have installed NEST 5.5.0).

using Nest;
using System;
using System.Collections.Generic;
using System.Linq;

namespace NestTest
{
    public class Class1
    {

        Func<SortDescriptor<VehicleItem>, IPromise<IList<ISort>>> GetElasticSortByField(Sorting shortR, VehicleSearch search)
        {
            if (shortR == null)
                return null;

            Func<SortDescriptor<VehicleItem>, IPromise<IList<ISort>>> sortDesc = null;

            if (!string.IsNullOrEmpty(shortR.SortBy))
            {
                char fieldName = shortR.SortBy.ElementAt(0);

                switch (fieldName)
                {
                    case 'B':
                    case 'Y':
                        sortDesc = s => s.Field(f => f.Year, shortR.Direction);
                        break;
                    case 'P':
                        sortDesc = s => s.Field(f => f.VehiclePrice, shortR.Direction);
                        break;
                    case 'M':
                        sortDesc = s => s.Field(f => f.Mileage, shortR.Direction);
                        break;
                    case 'L':
                        sortDesc = s => s.GeoDistance(a => a.Field("coords").DistanceType(GeoDistanceType.Arc).Order(shortR.Direction).Unit(DistanceUnit.Miles).DistanceType(GeoDistanceType.Arc).Mode(SortMode.Min).PinTo(new Nest.GeoLocation(search.GeoLocation.Latitude, search.GeoLocation.Longitude)));
                        break;
                    default:
                        sortDesc = s => s.Field(f => f.Year, shortR.Direction);
                        break;
                }

            }
            return sortDesc;
        }

    }
    public class VehicleItem
    {
        public VehicleItem()
        {

        }

        public short? Year { get; set; }
        public int Mileage { get; set; }
        public double VehiclePrice { get; set; }
        public double Distance { get; set; }

        public Nest.GeoLocation Coords { get; set; }
    }

    public class VehicleSearch
    {
        public Location GeoLocation { get; set; }
        public Sorting Sort { get; set; }
    }

    public class Location
    {
        public int Zip { get; set; }
        public int Distance { get; set; }
        public double Latitude { get; set; }
        public double Longitude { get; set; }
    }

    public class Sorting
    {
        public string SortBy { get; set; }
        public SortDirection Direction { get; set; }
    }


    public enum SortDirection : int
    {
        Asc = 0,
        Desc = 1,
    }
}

Please use the </> to format the code, or use surrounding ```. I edited your post to add these.

Thanks for the example; how is it supposed to be invoked?

Try to run it in the vs2017 IDE. I added a class library, this is not supposed to execute to any output. The sample code is just to demonstrate the compilation errors.

Direction property on Sorting type should be of type Nest.SortOrder

public class Sorting
{
	public string SortBy { get; set; }
	public Nest.SortOrder Direction { get; set; }
}

Thanks @forloop, this did help.

Great! If it solves the issue, please mark it as the solution :slight_smile:

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.