Elasticsearch.Net.UnexpectedElasticsearchClientException:在从 ES 获取数据期间

Posted

技术标签:

【中文标题】Elasticsearch.Net.UnexpectedElasticsearchClientException:在从 ES 获取数据期间【英文标题】:Elasticsearch.Net.UnexpectedElasticsearchClientException: during getting data from ES 【发布时间】:2019-05-14 12:42:13 【问题描述】:

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

var nodes = new Uri[]
            
             new Uri("http://localhost:9200"),
            ;

            connectionPool = new StaticConnectionPool(nodes);
            settings = new ConnectionSettings(connectionPool);
            settings.DefaultIndex("restaurants");
            client = new ElasticClient(settings);

这是我的模型:

class restaurants
    
        public string name  get; set; 
        public GeoCoordinate location  get; set; 
    

这是请求:(在各种变化中)

 var searchResponse = client.Search<restaurants>(s => s.Query(q => q.Match(m => m.Field(f => f.name == "Cafe Cafe"))));

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

Elasticsearch.Net.UnexpectedElasticsearchClientException
  HResult=0x80131500
  Message=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<T>) 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 113.
  Source=Elasticsearch.Net

谁能帮忙,有什么问题?

编辑: 这是我的数据(来自 Kibana 中的搜索):


  "took" : 3,
  "timed_out" : false,
  "_shards" : 
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  ,
  "hits" : 
    "total" : 
      "value" : 1,
      "relation" : "eq"
    ,
    "max_score" : 1.0,
    "hits" : [
      
        "_index" : "restaurants",
        "_type" : "restaurant",
        "_id" : "6",
        "_score" : 1.0,
        "_source" : 
          "name" : "Cafe Cafe",
          "location" : "41.12,-71.34"
        
      
    ]
  

【问题讨论】:

可以分享索引映射吗? 您可以检查一下您的 elasticsearch 版本吗? 您应该将客户端版本更改为 7.0+ 【参考方案1】:

尝试将其切换为m.Field(f =&gt; f.name).Query("Cafe Cafe")

我已经尝试在我的机器上运行你的代码并得到了同样的异常,但是当我将语法更改为这个时,它是有效的。

【讨论】:

以上是关于Elasticsearch.Net.UnexpectedElasticsearchClientException:在从 ES 获取数据期间的主要内容,如果未能解决你的问题,请参考以下文章