[英]ElasticSearch NEST - all fields are NULL
我正在尝试使用 NEST 从弹性搜索中检索数据。 一切都会好的,但是 NEST 所有字段都返回 null。 但是,在调试模式下,我看到它正确地计算了文档但没有显示字段的值。
我已经做了什么:
这是我现在的代码
public class ElasticSearch
{
private ElasticClient _client;
public ElasticSearch()
{
var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(node);
settings.DefaultIndex("logsystem.logs");
_client = new ElasticClient(settings);
}
public void searchResults()
{
var searchResults = _client.Search<Product>(s => s.AllIndices());
}
}
产品.cs
[BsonIgnoreExtraElements]
public class Product
{
[BsonId]
[BsonIgnore]
public ObjectId Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string ProductLicenseKey { get; set; }
[Required]
public string Action { get; set; }
[Required]
public string ActionName { get; set; }
[Required]
public string MachineId { get; set; }
}
ElasticSearch 中的映射:
{
"logsystem.logs": {
"mappings": {
"properties": {
"Action": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"ActionName": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"MachineId": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"Name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"ProductLicenseKey": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
也许我的映射不正确? 任何答案都会有所帮助。 谢谢。
编辑 ElasticSearch 文件通过 postman 获得:
{
"took": 11,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 6,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "logsystem.logs",
"_type": "_doc",
"_id": "5e5c1ca2aaa6f1245cc38895",
"_score": 1.0,
"_source": {
"Action": "Button",
"ActionName": "Fixed Single Generation",
"MachineId": "987-654-321",
"Name": "System",
"ProductLicenseKey": "123-456-789"
}
},
{
"_index": "logsystem.logs",
"_type": "_doc",
"_id": "5e5c1cb0aaa6f1245cc38896",
"_score": 1.0,
"_source": {
"Action": "Button",
"ActionName": "Fixed Multiple Generation",
"MachineId": "987-654-321",
"Name": "System",
"ProductLicenseKey": "123-456-789"
}
},
{
"_index": "logsystem.logs",
"_type": "_doc",
"_id": "5e5c1cbdaaa6f1245cc38897",
"_score": 1.0,
"_source": {
"Action": "Button",
"ActionName": "Trackers Single Generation",
"MachineId": "987-654-321",
"Name": "System",
"ProductLicenseKey": "123-456-789"
}
},
{
"_index": "logsystem.logs",
"_type": "_doc",
"_id": "5e5c1ccbaaa6f1245cc38898",
"_score": 1.0,
"_source": {
"Action": "Button",
"ActionName": "Trackers Multiple Generation",
"MachineId": "987-654-321",
"Name": "System",
"ProductLicenseKey": "123-456-789"
}
},
{
"_index": "logsystem.logs",
"_type": "_doc",
"_id": "5e5c1cd3aaa6f1245cc38899",
"_score": 1.0,
"_source": {
"Action": "Button",
"ActionName": "Fixed Multiple Generation",
"MachineId": "987-654-321",
"Name": "System",
"ProductLicenseKey": "123-456-789"
}
},
{
"_index": "logsystem.logs",
"_type": "_doc",
"_id": "5e5c1ce0aaa6f1245cc3889a",
"_score": 1.0,
"_source": {
"Action": "Button",
"ActionName": "Tree Generation",
"MachineId": "987-654-321",
"Name": "System",
"ProductLicenseKey": "123-456-789"
}
}
]
}
}
所以问题是客户端试图反序列化 camelCased JSON 对象的 POCO 属性键并且对于强制转换是严格的。
解决方案:在创建 ES 客户端时,在ConnectionSettings上添加设置属性DefaultFieldNameInferrer 。
public ElasticSearch()
{
var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(node);
settings.DefaultIndex("logsystem.logs");
settings.DefaultFieldNameInferrer(p => p);
_client = new ElasticClient(settings);
}
注意:更改此设置将导致某些字段不再正确反序列化,如果它们是基础数据中可以是 null 的值类型(例如int
、 bool
、 decimal
等)。 如果您遇到该问题,只需添加可为空的, ?
, 类型后的运算符,它应该修复它。
您应该将此技术用于字段命名
例如
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
public class Entity
{
[Nested(Name = "fields")]
public Fields Fields { get; set; }
}
public class Fields
{
[Nested(Name = "keyword")]
public Keyword Keyword { get; set; }
}
public class Keyword
{
[Text(Name = "type")]
public string Type { get; set; }
[Number(Name = "ignore_above")]
public string Ignore { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.