18

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.

Ellesedil
  • 1,576
  • 1
  • 20
  • 44
  • how are you wanting the `Name` to come across..? `UpperCase, LosweCase..?` please clarify.. if either can't you use the `.ToUpper() or ToLower()` for example – MethodMan Dec 03 '14 at 22:44
  • 1
    @DJKRAZE: I don't want to modify the name at all. When I send an object to be indexed, I want the property names of the object to be unmolested. Note: settings.SetDefaultTypeNameInferrer only affects the object type (which is often inferred to be the Elasticsearch document type if you don't override it with something else more specific when you build your request). That line of code has no bearing on the property names of the object. – Ellesedil Dec 03 '14 at 22:45

2 Answers2

19

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 here just simply being a function that takes a string (your property name) and returns the same string unmodified.

Greg Marzouka
  • 3,315
  • 1
  • 20
  • 17
  • 1
    Excellent. So `settings.SetDefaultTypeNameInferrer(p => p.Name);` and `settings.SetDefaultPropertyNameInferrer(p => p);` will disable camel casing for the entire object (which would be the type and field names in Elasticsearch)? – Ellesedil Dec 04 '14 at 14:54
17

In version 2.5.0 it's:

settings.DefaultFieldNameInferrer(p => p)
Robert Brooker
  • 2,148
  • 24
  • 22