Search code examples
elasticsearchnestelasticsearch-net

Elasticsearch NEST DefaultMappingFor usage


Currently, I have been adding mapping through REST Api and programatically indexing documents using NEST.

However, I recently came across NEST's DefaultMappingFor (in ConnectionSettings) and wondering how it's used. (Won't be able to spike soon hence asking.)

Question: In the source code, if I set mapping using the DefaultMappingFor, does that mean it is not necessary to create the mapping using the Rest Api because, maybe(I think), and NEST will use the declared default mapping whenever a document is indexed?

Thank you.


Solution

  • DefaultMappingFor() and DefaultMappingFor<T>() allows you to configure defaults for a given CLR type. Both allow you to configure:

    1. The default index name to use, through the .IndexName() method
    2. The default type name to use, through the .TypeName() method
    3. The default relation name to use when mapping as Parent/Child relationship, through the .RelationName() method

    In addition, DefaultMappingFor<T>() allows you to configure:

    1. The property on the type to use for the id of the document through the .IdProperty() method
    2. The property on the type to use for the routing parameter (useful to set for Parent/Child relationships, but also when you need routing), through the .Routing() method
    3. Which properties to ignore when serializing the type through the .Ignore() method
    4. Which properties should be serialized with different field names through the .PropertyName() method

    Setting any of these for a type means that NEST will use these over any conventions it has. The typical ones to use are .IndexName() and .TypeName() (although types are removed in Elasticsearch 6.x), and means that you don't need to specify either of these on each request (unless you want to override this default mapping too).