So, I have a java based web project that displays information retrieved from 3 separate services, hosted on different servers, I use Apache Http Client to
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;
}
}
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);
The results are a write up of our code and the following references
Yes that is possible, I was doing some similar thing once.
But I think you should write some converter, which would fetch the results from SolrDocumentList
and from each SolrDocument
put the result to your POJO's.
Read about setting the query parameters and getting the results with solrj
, and I think it wouldn't be a problem for you to do this. The converter itself should be easy to write, because you can access every field indexed from the result.
Also pay attention to narrowing the results itself.
These are some guides, as I don't feel experienced with solr searching. I don't know if it will help you much, but I hope so.
String id; //input parameter to search with
SolrQuery q = new SolrQuery();
q.set("q","id:" + id);
q.set("fl","*"); // fetch all fields to map to corresponding POJO
QueryResponse queryResponse = solrClient.query("My Collection Name", q);
//because I search by ID, I expect a list of just one element
MyPojo myPojo = queryResponse.getBeans(MyPojo.class).get(0);