Warm tip: This article is reproduced from stackoverflow.com, please click
java lucene solr solrj builder

How can I dynamically create Java class from Solr db when I don't know how many String fields docume

发布于 2020-04-22 10:23:40

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?

Questioner
Piotr Supel
Viewed
59
MatsLindh 2020-02-06 19:49

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>.