I'm working on an app that adds dynamic fields to solr DB based on the external REST API response. Fields have suffix *Txt. For now, I have for example documents with fistNameTxt,lastNameTxt. Later I need to use documents on the Java app and my class is:
@SolrDocument
@AllArgsConstructor
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResourceSolre {
String firstNameTxt;
String lastNameTxt;
}
The problem is that external API can change documents and serve me a few more String (or even Long) fields. Solr will add all to DB (because they are dynamic fields) but my class will no longer reflect the documents. The question is how to create a class that will also dynamically handle the Solr documents?
You can use wildcards when mapping Solr fields to your own fields:
@Field("*_txt")
public Map<String, String> dynamicFieldsTxt;
This will give you a map with <FieldName>
mapped to <Value>
.