中禁用驼峰式大小写

中禁用驼峰式大小写

本文介绍了如何在 NEST 中禁用驼峰式大小写 Elasticsearch 字段名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,NEST 将对象名称和属性名称发送到 Elasticsearch 以进行索引.如何在 Elasticsearch 文档的 NEST 中禁用骆驼大小写字段名称?我做了大量的研究,有一个 邮件列表主题,但它似乎已经过时,因为某些方法已重命名或不再存在.

By default, NEST will camel case object and property names when sending an object to Elasticsearch for indexing. How can camel casing field names be disabled in NEST for Elasticsearch documents? 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() 正是您要找的.此方法接受一个函数,该函数采用属性名称并对其应用转换.然后在将请求发送到 Elasticsearch 之前,在您的每个属性上调用该函数.

ConnectionSettings.SetDefaultPropertyNameInferrer() is what you're looking for. 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.

如果你想保持你的属性名称不变,那么你可以这样做:

If you want to keep your property names untouched, then you can do this:

settings.SetDefaultPropertyNameInferrer(p => p)

p =>p 这里只是一个函数,它接受一个字符串(您的属性名称)并返回未修改的相同字符串.

p => p here just simply being a function that takes a string (your property name) and returns the same string unmodified.

这篇关于如何在 NEST 中禁用驼峰式大小写 Elasticsearch 字段名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 07:23