【问题标题】:Elasticsearch.Net.UnexpectedElasticsearchClientException during deserilize result反序列化结果期间的 Elasticsearch.Net.UnexpectedElasticsearchClientException
【发布时间】:2019-04-16 08:54:35
【问题描述】:

我有一个 c# 项目,我想向我的弹性搜索服务器发送请求。 这是我的连接和弹性搜索客户端:

ConnectionSettings connectionSettings;
ElasticClient elasticClient;
connectionSettings = new ConnectionSettings(new 
Uri("http://192.168.2.197:9292/"));        
elasticClient = new ElasticClient(connectionSettings);

这是我的要求:

var response = elasticClient.Search<NewsDataModel>(s => s
.Index("news-index")
.Type("title")
.Query(q => q.QueryString(qs => qs.Query("ny"))));

这是我的模型:

public class NewsDataModel  {      
public string _id { get; set; }     
public string title { get; set; }     
public string content { get; set; }       
public string summary { get; set; }
}

但是当我发送请求时,我得到了这个异常:

Elasticsearch.Net.UnexpectedElasticsearchClientException: '无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Int64' 因为该类型需要 JSON 原始值(例如 string, number, boolean, null) 来正确反序列化。 要修复此错误,请将 JSON 更改为 JSON 原始值(例如字符串、数字、布尔值、null)或更改反序列化类型 所以它是一个普通的 .NET 类型(例如,不是像 整数,而不是数组或列表之类的集合类型) 从 JSON 对象反序列化。也可以添加 JsonObjectAttribute 强制它从 JSON 对象反序列化的类型。 路径“hits.total.value”,第 1 行,位置 113。

我该如何解决这个异常?

【问题讨论】:

    标签: c# elasticsearch nest


    【解决方案1】:

    我遇到了同样的问题,似乎NEST 6.6.0 库与 Elasticsearch 7.0 不兼容。 我必须将NEST 更新为 7.0.0(此时为 alpha)。

    【讨论】:

    • 这对我有用,版本 7.0.0-alpha1,谢谢 :)
    • 7.0.0 版本可用
    【解决方案2】:

    在使用 NEST 6.8 并连接到 ES 7.9 时,我遇到了同样的问题。在 SearchDescriptor 中,调用 TotalHitsAsInteger(true) 方法使其工作。

            var searchResponse =  esClient.Search<_Doc>(s => s
                .Query(pq => pq.Term(new Field("type", null), "<type value>"))
                .Index("<Index Name>")
                .Size(3)
                .TotalHitsAsInteger(true)
            );
    

    【讨论】:

      猜你喜欢
      • 2011-10-18
      • 1970-01-01
      • 1970-01-01
      • 2023-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-31
      相关资源
      最近更新 更多