1
votes

I'm new to Elasticsearch and I'm using NEST. When I run my query in the browser (host/logstash-2019.03.17/_search?pretty) I get the following result:

{
 "took" : 138,
 "timed_out" : false,
   "shards" : {
     "total" : 1,
     "successful" : 1,
     "skipped" : 0,
     "failed" : 0
  },
  "hits" : {
    "total" : {
     "value" : 10,
     "relation" : "eq"
 },
  "max_score" : 1.0,
  "hits" : [
  {
    "_index" : "logstash-2019.03.17",
    "_type" : "logevent",
    "_id" : "aa7djGkB1zvCMljS8jPd",
    "_score" : 1.0,
    "_source" : {
      "@timestamp" : "2019-03-17T18:15:43.9506399Z",
      "level" : "Info",
      "message" : "Attempting to get results from ElasticSearch",
      "logger" : "App.Api.Controllers.MyController"
    }
  }, OTHER HITS IN THE SAME FORMAT 

However, when I'm trying to query the same index using ElasticClient i get the following exception:

Elasticsearch.Net.UnexpectedElasticsearchClientException: „Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Int64' because the type requires a JSON primitive value (e.g. string, number, boolean, null) to deserialize correctly. To fix this error either change the JSON to a JSON primitive value (e.g. string, number, boolean, null) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'hits.total.value', line 1, position 115.”

I thought NEST is capable of autodeserializing JSON correctly on condition that it is provided with a class whose properties correspond to "_source" object fields. At least this is what you can infer from this tutorial.

Here is my POCO class follwed by the query which throws the exception:

public class Logevent
    {
        public string Id { get; set; }
        public DateTime Timestamp { get; set; }
        public string Level { get; set; }
        public string Message { get; set; }
        public string Logger { get; set; }
    }


var client = new ElasticClient();

var searchResponse = client.Search<Logevent>(s => s.Index("logstash-2019.03.17").Query(q => q.Match(m => m.Field(f => f.Level).Query("message"))));

Could anyone explain what I'm doing wrong?

1
Not an answer to why it can't deserialize the hits total, but aa7djGkB1zvCMljS8jPd is not a valid Guid.. maybe you should make your Id property a string. - stuartd
@stuard Did that, yet the problem hasn't gone. - aspdev
Like I said, not an answer to the problem you've having. I don't know why Nest is failing to parse the hits total (note in the error message it is saying the error is at 'hits.total.value', line 1, position 115.) - stuartd
One possibility - are the versions of Nest and ES compatible..? - stuartd
Can you share your mappings? - LeBigCat

1 Answers

4
votes

It looks like you're using a newer version of Elasticsearch (maybe one of the 7.0.0 prereleases?) where the total field is no longer just an Int64 value

"total" : {
     "value" : 10,
     "relation" : "eq"
}

NEST 6.x does not handle this, NEST 7.x will however, when it's released. For now, I would recommend using the latest Elasticsearch 6.x, currently 6.6.2.

Major versions of NEST are tied to major versions of Elasticsearch, so

  • NEST 5.x -> Elasticsearch 5.x
  • NEST 6.x -> Elasticsearch 6.x
  • etc.

But I would recommend keeping NEST up to date on minors within a major; we maintain backwards binary compatibility within a major to help with this.