On application startup I want to check if schema exists and: 1) if it doesn't -> create schema from Java class containing fields 2) if it differs for aforementioned class -> update the schema 3) if schema maps 1:1 with fields in Java class then do nothing
I've divided my task into 3 subtask and my first one would be to create a schema from existing Java class.
Here is my class (getters and setters omitted):
public class Order {
private String name;
private String phone;
private Address address;
private List<Amenities> amenities;
private BigDecimal distance;
private String image;
private List<Attributes> attributes;
private List<String> networks;
private OrderType orderType;
private Service service;
}
As you can see it has quite many field types which also should be used to create a schema (recursively?). Is it even possible to create a schema like that?
It should be quite simple to do:
First, you need to list all fields of the class, it could be achieved via reflection. After it, you should use Schema API, which will allow you to do CRUD operation on the fields for schema.
/schema/fields
will allow you to get all fields, and then you could use replace-field
for updating field, delete-field
to a deletion of field and add-field
for adding field. For more information just check this documentation.
Caveat: Schema API was added starting from Solr 5.3