简体   繁体   English

如何在NEST中禁用驼峰套管Elasticsearch字段名称?

[英]How to disable camel casing Elasticsearch field names in NEST?

By default, NEST will camel case object and property names when sending an object to Elasticsearch for indexing. 默认情况下,NEST会在将对象发送到Elasticsearch进行索引时使用驼峰大小写对象和属性名称。 How can camel casing field names be disabled in NEST for Elasticsearch documents? 如何在NEST中为Elasticsearch文档禁用骆驼套管字段名称? I've done a fair amount of research and there's a mailing list thread on the subject, but it seems outdated as some of the methods have been renamed or no longer exist. 我做了大量的研究,并且有关于这个主题的邮件列表主题,但它似乎已经过时,因为一些方法已被重命名或不再存在。

IConnectionPool connectionPool = new SniffingConnectionPool(m_ElasticsearchNodeUris);
ConnectionSettings settings = new ConnectionSettings(connectionPool);
settings.SetDefaultTypeNameInferrer(p => p.Name); //This disables camel casing for object type names
ElasticClient client = new ElasticClient(settings);

The info in the mailing list indicates this code should be added to handle things for field names, but the client method doesn't seem to exist: 邮件列表中的信息表明应添加此代码以处理字段名称,但客户端方法似乎不存在:

client.ModifyJsonSerializationSettings(s => s.ContractResolver = new Nest.Resolvers.ElasticResolver(settings);

Does anyone have any updated syntax to handle this? 有没有人有任何更新的语法来处理这个? Thanks. 谢谢。

ConnectionSettings.SetDefaultPropertyNameInferrer() is what you're looking for. ConnectionSettings.SetDefaultPropertyNameInferrer()是您正在寻找的。 This method accepts a function that takes a property name and applies a transformation to it. 此方法接受一个带有属性名称并对其应用转换的函数。 The function is then called on each of your properties before requests are sent to Elasticsearch. 然后,在将请求发送到Elasticsearch之前,会在每个属性上调用该函数。

If you want to keep your property names untouched, then you can do this: 如果您想保持您的属性名称不变,那么您可以这样做:

settings.SetDefaultPropertyNameInferrer(p => p)

p => p here just simply being a function that takes a string (your property name) and returns the same string unmodified. p => p这里只是一个函数,它接受一个字符串(你的属性名)并返回相同的字符串未修改。

在2.5.0版本中,它是:

settings.DefaultFieldNameInferrer(p => p)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM