Skip to content

Sort failing due to serialization error #1863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
curtismbeard opened this issue Feb 23, 2016 · 5 comments
Closed

Sort failing due to serialization error #1863

curtismbeard opened this issue Feb 23, 2016 · 5 comments

Comments

@curtismbeard
Copy link

When I apply a sort, the response is throwing an error when trying to convert the hits back to concrete types.

After looking into the code, it appears that the _score is null when a sort is applied. However, it fails to convert and throws an error. If I set TrackScores to true, it works correctly (_score comes back as 0 in this case). This happens on line 125 in https://github.com/elastic/elasticsearch-net/blob/master/src/Nest/CommonAbstractions/SerializationBehavior/StatefulDeserialization/ConcreteTypeConverter.cs

Here is the exception:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException was unhandled by user code
HResult=-2146233088
Message=Cannot implicitly convert type 'Newtonsoft.Json.Linq.JValue' to 'double'. An explicit conversion exists (are you missing a cast?)
Source=Anonymously Hosted DynamicMethods Assembly
StackTrace:
at CallSite.Target(Closure , CallSite , Object )
at Nest.ConcreteTypeConverter.GetConcreteTypeUsingSelector[T](JsonSerializer serializer, ConcreteTypeConverter1 realConcreteConverter, JObject jObject) in c:\code\elasticsearch-net\src\Nest\CommonAbstractions\SerializationBehavior\StatefulDeserialization\ConcreteTypeConverter.cs:line 125 at Nest.ConcreteTypeConverter.GetUsingConcreteTypeConverter[T](JsonReader reader, JsonSerializer serializer, ConcreteTypeConverter1 realConcreteConverter) in c:\code\elasticsearch-net\src\Nest\CommonAbstractions\SerializationBehavior\StatefulDeserialization\ConcreteTypeConverter.cs:line 83
at Nest.ConcreteTypeConverter`1.ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer) in c:\code\elasticsearch-net\src\Nest\CommonAbstractions\SerializationBehavior\StatefulDeserialization\ConcreteTypeConverter.cs:line 62
at Nest.DefaultHitJsonConverter.ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer) in c:\code\elasticsearch-net\src\Nest\CommonAbstractions\SerializationBehavior\StatefulDeserialization\ConcreteTypeConverter.cs:line 24
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable(JsonConverter converter, JsonReader reader, Type objectType, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)
InnerException:

@russcam
Copy link
Contributor

russcam commented Mar 2, 2016

@curtismbeard What version of NEST are you using, 2.x?

Do you have a minimum reproducible example that I can have a look at this with?

@RTroywest
Copy link

This should reproduce the error I think.
`

public interface IThing
{
    int Int { get; set; }
    string String { get; set; }
}   

class ThingA : IThing
{
    public int Int { get; set; }
    public string String { get; set; }
    public string A { get; set; }
}

class ThingB : IThing
{
    public int Int { get; set; }
    public string String { get; set; }
    public string B { get; set; }
}
class ThingC : IThing
{
    public int Int { get; set; }
    public string String { get; set; }
    public string C { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
       var client = new ElasticClient(settings);

        if (!client.IndexExists(new IndexExistsRequest("multitype")).Exists)
        {
            client.CreateIndex("multitype");
            var response = client.Map<ThingA>(m => m.Index("multitype").AutoMap());
            if (!response.IsValid)
            {
                throw new Exception(response.DebugInformation);
            }
            response = client.Map<ThingB>(m => m.Index("multitype").AutoMap());
            if (!response.IsValid)
            {
                throw new Exception(response.DebugInformation);
            }
            response = client.Map<ThingC>(m => m.Index("multitype").AutoMap());
            if (!response.IsValid)
            {
                throw new Exception(response.DebugInformation);
            }
        }

        client.Index(new ThingA {A = "Somthing for A", Int = 1, String = "thinga"});
        client.Index(new ThingA {A = "Somthing for A", Int = 2, String = "thinga"});
        client.Index(new ThingB {B = "Somthing for B", Int = 3, String = "thingb"});

        var searchResponse = client.Search<IThing>(
            s =>
                s.Index<ThingA>()//search correct index
                    .Type("thinga,thingb,thingc")
                    .ConcreteTypeSelector((v, y) =>
                    {
                        switch (y.Type)
                        {
                            case "thinga":
                                return typeof (ThingA);
                            case "thingb":
                                return typeof (ThingB);
                            case "thingc":
                                return typeof (ThingC);
                            default:
                                throw new Exception(y.Type);
                        }
                    })
                    .Query(x => x.Term(trm => trm.String, "thingb"))
                    .Sort(srt => srt.Ascending(fld => fld.Int))
                    .Skip(0)
                    .Take(10));}}`

@curtismbeard
Copy link
Author

I will work on a simple example that reproduces the issue. I am using 2.0.2.

@tbrooks007
Copy link

I ran into this issue last night. I am using v2.0.2 as well. I was sorting on both the _score field and a mapped date field. When sorting by the score only, I didn't receive the error but when I tried to sort on both the score and the date fields I received the same error listed in this ticket. In NEST 1.4 this worked. After doing some more reading on sorting, I see that scores will not be returned if you are sorting on a field unless you set TrackScores to true. This will ensure that the that scores will be tracked and computed. After I add this to my query, the query returned mixed type results using both _score and the mapped date field. All serialization worked as well.

Hope this helps.

@gmarz gmarz added Bug labels Mar 18, 2016
gmarz added a commit that referenced this issue Mar 18, 2016
@gmarz gmarz closed this as completed in 1655c93 Mar 18, 2016
@gmarz
Copy link
Contributor

gmarz commented Mar 18, 2016

Thank you all for reporting this. It'll be fixed in the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants