nest - How can I convert class properties into uppercase and rename few without Attribute in elasticsearch with c# -
i using elasticsearch in c# put data elasticsearch. have kept c# class & properties per database (pascalcase). however, requirement convert properties uppercase , change name of few name. same should possible class name well.
i not want achieve via data annotations. there way can made generic c# classes?
i using nest 5.x version.
for example,
class foo { public string thismessage {get; set; } public string anothermessage {get; set; } }
should convert into
class fooabc { public string thismessage {get; set; } public string {get; set; } }
it's possible both nest without using attributes:
to change casing of poco property names when serialized , sent elasticsearch, use
defaultfieldnameinferrer(func<string, string>)
to change name of type , rename properties, use
infermappingfor<t>()
typename()
,rename()
here's example
void main() { var pool = new singlenodeconnectionpool(new uri("http://localhost:9200")); var defaultindex = "default-index"; var connectionsettings = new connectionsettings(pool) .defaultfieldnameinferrer(s => s.toupperinvariant()) .infermappingfor<foo>(m => m .typename("fooabc") .rename(p => p.anothermessage, "another") ) .defaultindex(defaultindex); var client = new elasticclient(connectionsettings); if (client.indexexists(defaultindex).exists) client.deleteindex(defaultindex); var indexresponse = client.index(new foo { thismessage = "this message", anothermessage = "another message" }); } class foo { public string thismessage { get; set; } public string anothermessage { get; set; } }
the index request , response
post http://localhost:9200/default-index/fooabc?pretty=true { "thismessage": "this message", "another": "another message" } status: 201 { "_index" : "default-index", "_type" : "fooabc", "_id" : "avtahkdcii5clkx9kicz", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "created" : true }
Comments
Post a Comment