Dear Wiki user, You have subscribed to a wiki page or wiki category on "Solr Wiki" for change notification.
The following page has been changed by NoblePaul: http://wiki.apache.org/solr/Solrj ------------------------------------------------------------------------------ If your are calling the server.commit() and the documents are not getting commited, this is most likely your problem. - = SolrServer = - - /!\ TODO - coming soon... - - == CommonsHttpSolrServer == The [http://svn.apache.org/repos/asf/lucene/solr/trunk/client/java/solrj/src/org/apache/solr/client/solrj/impl/CommonsHttpSolrServer.java CommonsHttpSolrServer] uses the [http://jakarta.apache.org/httpcomponents/httpclient-3.x/ Apache Commons HTTP Client] to connect to solr. @@ -27, +22 @@ CommonsHttpSolrServer server = new CommonsHttpSolrServer( url ); ... }}} - === Setting BinaryResponseParser === + === Setting XMLResponseParser === - Anew binary response format is added for solr 1.3. It is a faster/smaller format . Add the following snippet to use that format + SolrJ has uses a binary format as the default format now. For users with Solr 1.2 or older versions of Solr 1.3 must explicitly ask SolrJ to use XML format. {{{ - server.setParser(new BinaryResponseParser()); + server.setParser(new XMLResponseParser()); }}} === Changing other Connection Settings === If you need to control other connection settings, you can cast the server to !CommonsHttpSolrServer. @@ -70, +65 @@ For simplicity, the most common commands are modeled in the [http://svn.apache.org/repos/asf/lucene/solr/trunk/client/java/solrj/src/org/apache/solr/client/solrj/SolrServer.java SolrServer]: - The methods are of the class are + == Adding Data to Solr == + * Get an instance of server first {{{ + SolrServer server = getSolrServer(); - - public UpdateResponse add( SolrInputDocument doc )throws IOException, SolrServerException ; - - public UpdateResponse add( Collection<SolrInputDocument> docs )throws IOException, SolrServerException ; - - public UpdateResponse add( SolrInputDocument doc, boolean overwrite )throws IOException, SolrServerException ; - - public UpdateResponse add( Collection<SolrInputDocument> docs, boolean overwrite )throws IOException, SolrServerException ; - - public UpdateResponse deleteById( String id )throws IOException, SolrServerException ; - - public UpdateResponse deleteByQuery( String query )throws IOException, SolrServerException ; - - public UpdateResponse commit( boolean waitFlush, boolean waitSearcher )throws IOException, SolrServerException ; - - public UpdateResponse optimize( boolean waitFlush, boolean waitSearcher )throws IOException, SolrServerException ; - - public UpdateResponse commit( )throws IOException, SolrServerException ; - - public UpdateResponse optimize( )throws IOException, SolrServerException ; - - public QueryResponse query( SolrParams params )throws IOException, SolrServerException ; - - public SolrPingResponse ping()throws IOException, SolrServerException ; - - public UpdateResponse addBeans(Collection<Object> beans, boolean overwrite )throws SolrServerException, IOException; - - public UpdateResponse addBeans(Collection<Object> beans )throws SolrServerException, IOException; - - public UpdateResponse addBean(Object obj, boolean overwrite) throws IOException, SolrServerException ; - - public UpdateResponse addBean(Object obj) throws IOException, SolrServerException ; - }}} + * If you wish to clean up the index before adding data do this - - For custom handlers or if you need access to functions not exposed in the !SolrServer, you need to work directly with the [http://svn.apache.org/repos/asf/lucene/solr/trunk/client/java/solrj/src/org/apache/solr/client/solrj/SolrRequest.java SolrRequest] and call: {{{ - request.process( server ); + server.deleteByQuery( "*:*" );// delete everything! }}} - - For example, if to immediately commit after adding documents, you could use: + + * Construct a document {{{ - SolrServer server = ... - - UpdateRequest req = new UpdateRequest(); - req.setAction( UpdateRequest.ACTION.COMMIT, false, false ); - req.add( docs ); - UpdateResponse rsp = req.process( server ); - - }}} - - - ------- - - - - (from the test cases) - {{{ - - SolrServer server = getSolrServer(); - - // Empty the database... - server.deleteByQuery( "*:*" );// delete everything! - - // Now add something... SolrInputDocument doc1 = new SolrInputDocument(); doc1.addField( "id", "id1", 1.0f ); doc1.addField( "name", "doc1", 1.0f ); doc1.addField( "price", 10 ); - + }}} + * Construct another document. Each document can be independently be added but it is more efficient to do a batch update. Every call to `SolrServer` is an Http Call (This is not true for !EmbeddedSolrServer). + {{{ SolrInputDocument doc2 = new SolrInputDocument(); doc2.addField( "id", "id2", 1.0f ); doc2.addField( "name", "doc2", 1.0f ); doc2.addField( "price", 20 ); - + }}} + *Create a collection of documents + {{{ Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>(); docs.add( doc1 ); docs.add( doc2 ); - - // Add the documents + }}} + *Add the documents to Solr + {{{ server.add( docs ); + }}} + * Do a commit + {{{ server.commit(); + }}} + *To immediately commit after adding documents, you could use: + {{{ - + + UpdateRequest req = new UpdateRequest(); + req.setAction( UpdateRequest.ACTION.COMMIT, false, false ); + req.add( docs ); + UpdateResponse rsp = req.process( server ); + }}} + + == Reading Data from Solr == + * Get an instance of server first + {{{ + SolrServer server = getSolrServer(); + }}} + * Construct a !SolrQuery + {{{ SolrQuery query = new SolrQuery(); query.setQuery( "*:*" ); query.addSortField( "price", SolrQuery.ORDER.asc ); + }}} + * Query the server + {{{ - QueryResponse rsp = server.query( query ); + QueryResponse rsp = server.query( query ); - - Assert.assertEquals( 2, rsp.getResults().getNumFound() ); - System.out.println( rsp.getResults() ); - + }}} + * Get the results + {{{ + SolrDocumentList docs = rsp.getResults(); }}}
