John K
John K

Reputation: 28907

How to automap in Nest without writing to the elasticsearch index?

Is it possible to leverage the NEST auto-mapping features to obtain Nest Property and Type objects without actually writing them into the elastic index via PUT Mapping and Create Index APIs?

For example, I'd like to auto-map this CLR class Company:

public class Company
{
    public string Name { get; set; }
}

and store the elastic mappings into variables like these:

Nest.TypeMapping typeCo = null; // for the mapped Company type
Nest.IProperty propCoName = null;  // for the mapped Company Name property 

but not to write the Company mapping to the index.

I could write to a temp index as a workaround but I suspect this isn't necessary.

Using elasticsearch 5.x and Nest 5.

Upvotes: 0

Views: 1033

Answers (1)

Russ Cam
Russ Cam

Reputation: 125528

Depending on exactly what you need, you could take a couple of different approaches

Using PropertyWalker

var walker = new PropertyWalker(typeof(Company), null);   
var properties = walker.GetProperties();

will provide the IProperty types as are inferred by automapping.

Using TypeMappingDescriptor<T>

var descriptor = (ITypeMapping)new TypeMappingDescriptor<Company>()
    .AutoMap();

will provide IProperty types in the .Properties property inferred from automapping, in addition to any other properties of ITypeMapping. Need to use the descriptor here as opposed to TypeMapping because the descriptor has the .AutoMap() method. Also need to cast to the interface as interface properties are explicitly implemented.

Upvotes: 0

Related Questions