【问题标题】:ElasticSearch NEST - all fields are NULLElasticSearch NEST - 所有字段均为 NULL
【发布时间】:2020-03-02 14:10:54
【问题描述】:

我正在尝试使用 NEST 从弹性搜索中检索数据。一切都会好的,但 NEST 所有字段都返回 null。但是,在调试模式下,我看到它正确计算了文档但没有显示字段的值。

我已经做了什么:

  • 检查过的映射,我觉得很好
  • 尝试过的字符串查询
  • 尝试获取源然后读取数据
  • 试过NEST returns null instead of fields 这些解决方案也没有帮助
  • 将 Product.cs 字段名称重命名为 camelCase 也无济于事

这是我现在的代码

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
                    }
                }
            }
        }
    }
}
}

也许我的映射不正确?任何答案都会有所帮助。谢谢。

EDIT ElasticSearch 文档通过邮递员获取:

{
"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"
            }
        }
    ]
 }
}

【问题讨论】:

  • 您能发布一下弹性搜索中记录的样子吗?
  • @VireshMathad 在帖子中添加。这就是我使用 Postman 得到的结果
  • 我猜你应该检查“Hits”而不是文档。
  • @VireshMathad 不一样.. 甚至没有找到字段...

标签: c# elasticsearch nest


【解决方案1】:

所以问题是客户端试图反序列化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 的值类型(例如 intbooldecimal 等),更改此设置将导致无法正确反序列化。如果您遇到该问题,只需在类型后面添加可空操作符?,它应该可以修复它。

【讨论】:

  • 您是否在此解决方案中添加了其他内容?我尝试实现这一点,但出现了一个新错误。
  • @BenjaminSalerno 没有。我刚刚添加了这个,它与 7.6.0 版本完美配合
  • 遇到了同样的事情。某些属性必须标记为可为空。当我的 C# 类具有任何值类型(如 intdecimalfloatbool 时,其中基础数据可能为空,则 public bool IsAwesome { get; set; } 变为 public bool? IsAwesome { get; set; })。一旦我这样做了,错误就消失了。我希望更改 DefaultFieldNameInferrer 上的设置不会破坏值类型反序列化。
猜你喜欢
  • 2015-03-30
  • 2021-04-14
  • 2019-04-24
  • 2015-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多