Search code examples
javajsonsolrsolrj

Retrieve Object from Solr


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 retrieve information via REST API in JSON, using Gson library. I convert the Json into POJO's that I use to display information.

Now I want to implement search feature in my project, so I installed Solr on a separate server, what I want is:

  1. Index the JSON in solr server for all 3 services.

  2. fetch search result from Solr in form of POJO's described in my project

I know that point (1) can be done by jsonRequestHandler, but I don't want to write separate logic to index, I am using Solrj in my project to extract information.

So I want to know

  • can solrj use my POJO definition to parse the search results?
  • also any possible workflow for above working scenario, and tools required (I am new to solrj)?

Solution

  • 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