Elasticsearch NEST 和基本搜索查询
Elasticsearch NEST and basic Search query
我是 Elasticsearch 的新手,我正在使用 NEST。当我 运行 我在浏览器中查询时 (host/logstash-2019.03.17/_search?pretty) 我得到以下结果:
{
"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
但是,当我尝试使用 ElasticClient 查询相同的索引时,出现以下异常:
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.”
我认为 NEST 能够正确地自动反序列化 JSON,条件是它提供了一个 class,其属性对应于“_source”对象字段。至少这是你可以从 this 教程中推断出来的。
这是我的 POCO class 后跟抛出异常的查询:
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"))));
谁能解释一下我做错了什么?
看起来您使用的是较新版本的 Elasticsearch(可能是 7.0.0 预发行版之一?),其中 the total
field is no longer just an Int64
value
"total" : {
"value" : 10,
"relation" : "eq"
}
NEST 6.x 不处理这个问题,但是 NEST 7.x 会在发布时处理。现在,我建议使用最新的 Elasticsearch 6.x,目前是 6.6.2.
NEST 的主要版本与 Elasticsearch 的主要版本相关联,因此
- NEST 5.x -> Elasticsearch 5.x
- NEST 6.x -> Elasticsearch 6.x
- 等等
但我建议让 NEST 保持专业内未成年人的最新状态;我们在一个专业中保持向后二进制兼容性来帮助解决这个问题。
我是 Elasticsearch 的新手,我正在使用 NEST。当我 运行 我在浏览器中查询时 (host/logstash-2019.03.17/_search?pretty) 我得到以下结果:
{
"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
但是,当我尝试使用 ElasticClient 查询相同的索引时,出现以下异常:
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.”
我认为 NEST 能够正确地自动反序列化 JSON,条件是它提供了一个 class,其属性对应于“_source”对象字段。至少这是你可以从 this 教程中推断出来的。
这是我的 POCO class 后跟抛出异常的查询:
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"))));
谁能解释一下我做错了什么?
看起来您使用的是较新版本的 Elasticsearch(可能是 7.0.0 预发行版之一?),其中 the total
field is no longer just an Int64
value
"total" : {
"value" : 10,
"relation" : "eq"
}
NEST 6.x 不处理这个问题,但是 NEST 7.x 会在发布时处理。现在,我建议使用最新的 Elasticsearch 6.x,目前是 6.6.2.
NEST 的主要版本与 Elasticsearch 的主要版本相关联,因此
- NEST 5.x -> Elasticsearch 5.x
- NEST 6.x -> Elasticsearch 6.x
- 等等
但我建议让 NEST 保持专业内未成年人的最新状态;我们在一个专业中保持向后二进制兼容性来帮助解决这个问题。