Make delicious recipes!

Solrj Example



Following is a running example of Solrj with just the following files.
./pom.xml
./src/main/java/com/prismoskills/solrj/pojos/Item.java
./src/main/java/com/prismoskills/solrj/SolrDao.java
./src/main/java/com/prismoskills/solrj/SolrjExample.java
./src/main/java/com/prismoskills/solrj/SolrServerFactory.java
./src/test/java/com/prismoskills/solrj/AppTest.java
(Assuming maven and java are already installed in the system and Solr is running on the default port 8983)

  1. pom.xml needs to have the following dependencies:
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.prismoskills.solrj</groupId>
      <artifactId>solrj_example</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <name>example</name>
      <url>http://maven.apache.org</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
    
    
    
      <dependencies>
    
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
    
        <dependency>
          <artifactId>solr-solrj</artifactId>
          <groupId>org.apache.solr</groupId>
          <version>4.1.0</version>
          <type>jar</type>
          <scope>compile</scope>
        </dependency>
    
        <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-simple</artifactId>
          <version>1.7.2</version>
        </dependency>
    
    	<dependency>
    	  <groupId>commons-logging</groupId>
    	  <artifactId>commons-logging</artifactId>
    	  <version>1.1.1</version>
        </dependency>
    
      </dependencies>
    
    
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.1</version>
            <configuration>
              <transformers>
                <transformer implementation=
                  "org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <manifestEntries>
                    <Main-Class>com.prismoskills.solrj.SolrjExample</Main-Class>
                  </manifestEntries>
                </transformer>
              </transformers>
            </configuration>
            <executions>
              <execution>
                <phase>package</phase>
                <goals>
                  <goal>shade</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    
    </project>
    


  2. Item.java is a normal POJO class.
    package com.prismoskills.solrj.pojos;
    
    import org.apache.solr.client.solrj.beans.Field;
    
    public class Item 
    {
        @Field("id")
        String id;
        @Field("name")
        String name;
        @Field
        Float price;
        
        public Item() {} // Empty constructor is required
        
        public Item(String id, String name, Float price)
        {
            super();
            this.id = id;
            this.name = name;
            this.price = price;
        }
        
        // Getter Setters
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Float getPrice() {
            return price;
        }
        public void setPrice(Float price) {
            this.price = price;
        }
    }
    


  3. SolrServerFactory.java encapsulates the creation of SolrServer.
    
    package com.prismoskills.solrj;
    
    import java.util.Map;
    import java.util.concurrent.ConcurrentHashMap;
    
    import org.apache.solr.client.solrj.SolrServer;
    import org.apache.solr.client.solrj.impl.HttpSolrServer;
    
    public final class SolrServerFactory 
    {
        Map <String, SolrServer> urlToServer = new ConcurrentHashMap <String, SolrServer> ();
        static SolrServerFactory instance = new SolrServerFactory ();
        
        public static SolrServerFactory getInstance()
        {
            return instance;
        }
        
        private SolrServerFactory ()
        {
        }
        
        public SolrServer createServer (String solrURL)
        {
            if (urlToServer.containsKey(solrURL))
                return urlToServer.get(solrURL);
            
            /*
            HttpSolrServer is thread-safe and if you are using the following constructor,
            you *MUST* re-use the same instance for all requests.  If instances are created on
            the fly, it can cause a connection leak. The recommended practice is to keep a
            static instance of HttpSolrServer per solr server url and share it for all requests.
            See https://issues.apache.org/jira/browse/SOLR-861 for more details
          */
          SolrServer server = new HttpSolrServer( solrURL );
          urlToServer.put(solrURL, server);
          return server;
        }
    }
    


  4. SolrDao.java is the most important class here.
    package com.prismoskills.solrj;
    
    import java.io.IOException;
    import java.util.Collection;
    import java.util.Collections;
    
    import org.apache.solr.client.solrj.SolrQuery;
    import org.apache.solr.client.solrj.SolrServerException;
    import org.apache.solr.client.solrj.impl.HttpSolrServer;
    import org.apache.solr.client.solrj.request.UpdateRequest;
    import org.apache.solr.client.solrj.response.QueryResponse;
    import org.apache.solr.client.solrj.response.UpdateResponse;
    import org.apache.solr.common.SolrDocumentList;
    import org.apache.solr.common.SolrInputDocument;
    
    import com.prismoskills.solrj.SolrServerFactory;
    
    public class SolrDao <T>
    {
    
        HttpSolrServer server = null;
        
        public SolrDao (String solrURL)
        {
            server = (HttpSolrServer) SolrServerFactory.getInstance().createServer(solrURL);
            configureSolr (server);
        }
        
        public void put (T dao)
        {
            put (createSingletonSet (dao));
        }
        
        public void put (Collection<T> dao)
        {
            try 
            {
                UpdateResponse rsp = server.addBeans (dao);
                System.out.println ("Added documents to solr. Time taken = " + rsp.getElapsedTime() + ". " + rsp.toString());
            }
            catch (SolrServerException e)
            {
                e.printStackTrace();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        
        public void putDoc (SolrInputDocument doc)
        {
            putDoc (createSingletonSet(doc));
        }
        
        public void putDoc (Collection<SolrInputDocument> docs)
        {
            try 
            {
                long startTime = System.currentTimeMillis();
                UpdateRequest req = new UpdateRequest();
                req.setAction( UpdateRequest.ACTION.COMMIT, false, false );
                req.add (docs);
                UpdateResponse rsp = req.process( server );
                System.out.print ("Added documents to solr. Time taken = " + rsp.getElapsedTime() + ". " + rsp.toString());
                long endTime = System.currentTimeMillis();
                System.out.println (" , time-taken=" + ((double)(endTime-startTime))/1000.00 + " seconds");
            }
            catch (SolrServerException e)
            {
                e.printStackTrace();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        
        public QueryResponse readAll () 
        {
            SolrQuery query = new SolrQuery();
            query.setQuery( "*:*" );
            //query.addSortField( "price", SolrQuery.ORDER.asc );
            QueryResponse rsp = null;
            try 
            {
                rsp = server.query( query );
            }
            catch (SolrServerException e) 
            {
                e.printStackTrace();
            }
            return rsp;
        }
        
        public SolrDocumentList readAllDocs ()
        {
            SolrQuery query = new SolrQuery();
            query.setQuery( "*:*" );
            //query.addSortField( "price", SolrQuery.ORDER.asc );
            QueryResponse rsp = null;
            try {
                rsp = server.query( query );
            } catch (SolrServerException e) 
            {
                e.printStackTrace();
                return null;
            }
            SolrDocumentList docs = rsp.getResults();
            return docs;
        }
        
        private void configureSolr(HttpSolrServer server) 
        {
            server.setMaxRetries(1); // defaults to 0.  > 1 not recommended.
            server.setConnectionTimeout(5000); // 5 seconds to establish TCP
            // The following settings are provided here for completeness.
            // They will not normally be required, and should only be used 
            // after consulting javadocs to know whether they are truly required.
            server.setSoTimeout(1000);  // socket read timeout
            server.setDefaultMaxConnectionsPerHost(100);
            server.setMaxTotalConnections(100);
            server.setFollowRedirects(false);  // defaults to false
            // allowCompression defaults to false.
            // Server side must support gzip or deflate for this to have any effect.
            server.setAllowCompression(false);
        }
        
        private <U> Collection<U> createSingletonSet(U dao) 
        {
            if (dao == null)
                return Collections.emptySet();
            return Collections.singleton(dao);
        }
    
    }
    


  5. SolrjExample.java has the main() function and executes SolrDao functions.
    package com.prismoskills.solrj;
    
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Iterator;
    import java.util.List;
    
    import org.apache.solr.client.solrj.response.QueryResponse;
    import org.apache.solr.common.SolrDocument;
    import org.apache.solr.common.SolrDocumentList;
    import org.apache.solr.common.SolrInputDocument;
    
    import com.prismoskills.solrj.SolrDao;
    import com.prismoskills.solrj.pojos.Item;
    
    
    public class SolrjExample 
    {
        String solrURL = "http://localhost:8983/solr";
        
        public static void main( String[] args )
        {
            new SolrjExample().execute();
        }
        
        public void execute () 
        {
            System.out.println( "Starting off " + this.getClass().toString());
            SolrDao<Item> solrDao = new SolrDao<Item> (solrURL);
            
            addDocuments (solrDao);
            readDocuments (solrDao);
            
            addItems (solrDao);
            readItems (solrDao);
        }
    
        private void readItems(SolrDao<Item> solrDao) 
        {
            QueryResponse rsp = solrDao.readAll();
            List<Item> beans = rsp.getBeans(Item.class);
            for (Item bean : beans)
            {
                System.out.println ("Read item " + bean.getId() + ", name = " + bean.getName());
            }
        }
    
        private void addItems(SolrDao<Item> solrDao) 
        {
            Collection <Item> items = new ArrayList <Item> (3);
            items.add(new Item ("1", "Item 1", 20.50F));
            items.add(new Item ("2", "Item 2", 10.10F));
            items.add(new Item ("3", "Item 3", 30.00F));
            solrDao.put (items);
        }
    
        private void readDocuments(SolrDao<Item> solrDao) 
        {
            SolrDocumentList docs = solrDao.readAllDocs ();
            Iterator<SolrDocument> iter = docs.iterator();
            int count=10;
    
            while (iter.hasNext() && count-- > 0) 
            {
              SolrDocument resultDoc = iter.next();
    
              String content = (String) resultDoc.getFieldValue("content");
              String id = (String) resultDoc.getFieldValue("id"); //id is the uniqueKey field
              System.out.println ("Read " + resultDoc + " with id = " + id + " and content = " + content);
            }
        }
    
        private void addDocuments(SolrDao<Item> solrDao) 
        {
            Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
            for (int i=0; i<1000; i++)
                docs.add( getRandomSolrDoc (i) );
    
            solrDao.putDoc (docs);
        }
    
        private SolrInputDocument getRandomSolrDoc(int count) 
        {
            SolrInputDocument doc = new SolrInputDocument();
            doc.addField( "id", "id"+count, 1.0f );
            doc.addField( "name", "doc"+count, 1.0f );
            doc.addField( "price", count%10 );
            return doc;
        }
    }
    



Results


The above can be run as:
    mvn clean package
    java -jar target/solrj_example-1.0-SNAPSHOT.jar
Starting off class com.prismoskills.solrj.SolrjExample

[main] INFO org.apache.solr.client.solrj.impl.HttpClientUtil - Creating new http client, config:maxConnections=128&maxConnectionsPerHost=32&followRedirects=false

Added documents to solr. Time taken = 238. {responseHeader={status=0,QTime=125}} , time-taken=0.24 seconds
Read SolrDocument{id=1, name=Item 1, price=20.5, price_c=20.5,USD, _version_=1450355752342388736} with id = 1 and content = null
Read SolrDocument{id=2, name=Item 2, price=10.1, price_c=10.1,USD, _version_=1450355752342388737} with id = 2 and content = null
Read SolrDocument{id=3, name=Item 3, price=30.0, price_c=30.0,USD, _version_=1450355752342388738} with id = 3 and content = null
Read SolrDocument{id=id0, name=doc0, price=0.0, price_c=0,USD, _version_=1450356064966934528} with id = id0 and content = null
Read SolrDocument{id=id1, name=doc1, price=1.0, price_c=1,USD, _version_=1450356064967983104} with id = id1 and content = null
Read SolrDocument{id=id2, name=doc2, price=2.0, price_c=2,USD, _version_=1450356064967983105} with id = id2 and content = null
Read SolrDocument{id=id3, name=doc3, price=3.0, price_c=3,USD, _version_=1450356064967983106} with id = id3 and content = null
Read SolrDocument{id=id4, name=doc4, price=4.0, price_c=4,USD, _version_=1450356064967983107} with id = id4 and content = null
Read SolrDocument{id=id5, name=doc5, price=5.0, price_c=5,USD, _version_=1450356064967983108} with id = id5 and content = null
Read SolrDocument{id=id6, name=doc6, price=6.0, price_c=6,USD, _version_=1450356064967983109} with id = id6 and content = null

Added documents to solr. Time taken = 4. {responseHeader={status=0,QTime=0}}
Read item 1, name = Item 1
Read item 2, name = Item 2
Read item 3, name = Item 3
Read item id0, name = doc0
Read item id1, name = doc1
Read item id2, name = doc2
Read item id3, name = doc3
Read item id4, name = doc4
Read item id5, name = doc5
Read item id6, name = doc6







Like us on Facebook to remain in touch
with the latest in technology and tutorials!


Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Name:
Email: (Your email is not shared with anybody)
Comment:

Facebook comments:

Site Owner: Sachin Goyal