使用 C# Nest API 获取嵌套 json 数据未检索数据
Using C# Nest API to get nested json data is not retrieving data
我有以下 json 来自 elasticsearch:
{
"_index": "data-2016-01-14",
"_type": "type-data",
"_id": "AVJBBNG-TE8FYIA1rf1p",
"_score": 1,
"_source": {
"@message": {
"timestamp": 1452789770326461200,
"eventID": 1452789770326461200,
"eventName": "New",
"Price": "38.34",
"Qty": 100,
"statistic_LatencyValue_ns": 1142470,
"statistic_LatencyViolation": false,
"statistic_LossViolation": false
},
"@timestamp": "2016-01-14T16:42:50.326Z",
"@fields": {
"timestamp": "1452789770326"
}
},
"fields": {
"@timestamp": [
1452789770326
]
}
}
我正在使用 Nest 尝试获取我创建 class 并标记 属性:
的 eventName 数据
public class ElasticTest
{
[ElasticProperty(Type = FieldType.Nested)]
public string eventName { get; set; }
}
但是下面的查询返回了 0 个结果,我做错了什么?
var result = client.Search<CorvilTest>(s => s
.From(0)
.Size(10000)
.Query(x => x
.Term(e => e.eventName,"New"))
);
var r = result.Documents;
映射定义:
{
"data-2016-01-14": {
"mappings": {
"type-data": {
"properties": {
"@fields": {
"properties": {
"timestamp": {
"type": "string"
}
}
},
"@message": {
"properties": {
"OrderQty": {
"type": "long"
},
"Price": {
"type": "string"
},
"eventID": {
"type": "long"
},
"eventName": {
"type": "string"
},
"statistic_LatencyValue_ns": {
"type": "long"
},
"statistic_LatencyViolation": {
"type": "boolean"
},
"statistic_LossViolation": {
"type": "boolean"
},
"timestamp": {
"type": "long"
}
}
},
"@timestamp": {
"type": "date",
"format": "dateOptionalTime"
}
}
}
}
}
}
我看到字段 @message.eventName
正在使用标准分析器,这意味着它的值为 lower-cased 并在索引之前在单词边界处拆分。因此值 "new" 被索引而不是 "New"。阅读更多相关信息 here. You need to be mindful about this fact when using a Term Query
。另一件事是字段 eventName
不是 nested
类型。所以下面的代码应该适合你。
var result = client.Search<CorvilTest>(s => s
.From(0)
.Size(10000)
.Query(x => x
.Term(e => e.Message.EventName, "new"))); // Notice I've used "new" and not "New"
var r = result.Documents;
为使上述代码正常工作,CorvilTest
class 的定义应如下所示:
public class CorvilTest
{
[ElasticProperty(Name = "@message")]
public Message Message { get; set; }
/* Other properties if any */
}
public class Message
{
[ElasticProperty(Name = "eventName")]
public string EventName { get; set; }
}
我有以下 json 来自 elasticsearch:
{
"_index": "data-2016-01-14",
"_type": "type-data",
"_id": "AVJBBNG-TE8FYIA1rf1p",
"_score": 1,
"_source": {
"@message": {
"timestamp": 1452789770326461200,
"eventID": 1452789770326461200,
"eventName": "New",
"Price": "38.34",
"Qty": 100,
"statistic_LatencyValue_ns": 1142470,
"statistic_LatencyViolation": false,
"statistic_LossViolation": false
},
"@timestamp": "2016-01-14T16:42:50.326Z",
"@fields": {
"timestamp": "1452789770326"
}
},
"fields": {
"@timestamp": [
1452789770326
]
}
}
我正在使用 Nest 尝试获取我创建 class 并标记 属性:
的 eventName 数据public class ElasticTest
{
[ElasticProperty(Type = FieldType.Nested)]
public string eventName { get; set; }
}
但是下面的查询返回了 0 个结果,我做错了什么?
var result = client.Search<CorvilTest>(s => s
.From(0)
.Size(10000)
.Query(x => x
.Term(e => e.eventName,"New"))
);
var r = result.Documents;
映射定义:
{
"data-2016-01-14": {
"mappings": {
"type-data": {
"properties": {
"@fields": {
"properties": {
"timestamp": {
"type": "string"
}
}
},
"@message": {
"properties": {
"OrderQty": {
"type": "long"
},
"Price": {
"type": "string"
},
"eventID": {
"type": "long"
},
"eventName": {
"type": "string"
},
"statistic_LatencyValue_ns": {
"type": "long"
},
"statistic_LatencyViolation": {
"type": "boolean"
},
"statistic_LossViolation": {
"type": "boolean"
},
"timestamp": {
"type": "long"
}
}
},
"@timestamp": {
"type": "date",
"format": "dateOptionalTime"
}
}
}
}
}
}
我看到字段 @message.eventName
正在使用标准分析器,这意味着它的值为 lower-cased 并在索引之前在单词边界处拆分。因此值 "new" 被索引而不是 "New"。阅读更多相关信息 here. You need to be mindful about this fact when using a Term Query
。另一件事是字段 eventName
不是 nested
类型。所以下面的代码应该适合你。
var result = client.Search<CorvilTest>(s => s
.From(0)
.Size(10000)
.Query(x => x
.Term(e => e.Message.EventName, "new"))); // Notice I've used "new" and not "New"
var r = result.Documents;
为使上述代码正常工作,CorvilTest
class 的定义应如下所示:
public class CorvilTest
{
[ElasticProperty(Name = "@message")]
public Message Message { get; set; }
/* Other properties if any */
}
public class Message
{
[ElasticProperty(Name = "eventName")]
public string EventName { get; set; }
}