Mapping a POJO for Solr
To do so you need to annotate the fields/access-methods of your POJO with the org.apache.solr.client.solrj.beans.Field
-Annotation.
Of course those fields need to match the fields of your schema.xml either by their name directly or by the name you point Solr to by giving the name in the Field annotation.
As example you have the following definition of fields
in your schema.xml
<fields>
<field name="id" type="int" indexed="true" stored="true" multiValued="false" />
<field name="title" type="string" indexed="true" stored="true" multiValued="false" />
</fields>
Then you would have a POJO like this
import org.apache.solr.client.solrj.beans.Field;
public class SampleDocument {
public SampleDocument() {
// required for solrj to make an instance
}
public SampleDocument(int id, String title) {
this.id = id;
this.title = title;
}
public String getTitle() {
return title;
}
@Field("title")
public void setTitle(String title) {
this.title = title;
}
}
Using a POJO to update Solr's Index
The code to index those POJOs is rather straight forward. You can use solrj's SolrServer for that purpose.
// connect to your solr server
SolrServer server = new HttpSolrServer("http://HOST:8983/solr/");
// adding a single document
SampleDocument document = new SampleDocument(1, "title 1");
server.addBean(document);
// adding multiple documents
List<SampleDocument> documents = Arrays.asList(
new SampleDocument(2, "title 2"),
new SampleDocument(3, "title 3"));
server.addBeans(documents);
// commit changes
server.commit();
// query solr for something
QueryResponse response = server.query(new SolrQuery("*:*"));
// get the response as List of POJO type
List<SampleDocument> foundDocuments = response.getBeans(SampleDocument.class);
Further reading
The results are a write up of our code and the following references