0
votes

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.

1
Am I understanding correctly that your goal is to just query and deserialize the Elastic document values via an index that already exists and has a structure that would correlate to your model?Miek
@MikeMichaels No. I want to serialize .NET objects into Nest (also CLR) data types, but not actually write to an index. Kind of like replace the writing with a NOOP but keep the results of the would-be mapping.John K

1 Answers

0
votes

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.