What is the best practice for schema free models in Java POJO? What is the best practice for schema free models in Java POJO? elasticsearch elasticsearch

What is the best practice for schema free models in Java POJO?


One possibility is the use of polymorphic types, where base class can define common set of properties, sub-classes additional ones. Depending on your needs, you may also need to enable polymorphic handling; that is, depending on whether you know the intended type from context (no need for type info), or if not (that is, it's stored in JSON document; if so, polymorphic handling needed).

However: a simpler and more dynamic (but less type-safe) approach may be to use "dyna-beans" approach, explained for example here:

http://www.cowtowncoder.com/blog/archives/2011/07/entry_458.html

so that you define a set of named, fully-typed core properties; and the rest can be included as needed. Types for such extra properties will need to be "natural" types (Strings, Numbers, Booleans, Map, List), but you can use Jackson to further convert back and forth between these and POJOs (using ObjectMapper.convertValue()).

Or, if you prefer, dynamic properties can also be specified as JsonNodes, in which case you get nicely traversable JSON tree access.