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 ShalinMangar: http://wiki.apache.org/solr/Solrj The comment on the change is: Fixed headlines and formatting ------------------------------------------------------------------------------ - <!> ["Solr1.3"] + <!> ["Solr1.3"] - - Solrj is a java client to access solr. It offers a java interface to add, update, and query the solr index. [[TableOfContents]] + + Solrj is a java client to access solr. It offers a java interface to add, update, and query the solr index. /!\ NOTE -- the default solrj update command expects to use a standard !RequestHandler, '''not''' the deprecated !SolrUpdateServlet. Make sure your solrconfig.xml includes: {{{ @@ -13, +13 @@ If your are calling the server.commit() and the documents are not getting commited, this is most likely your problem. - == CommonsHttpSolrServer == + = 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. @@ -22, +22 @@ CommonsHttpSolrServer server = new CommonsHttpSolrServer( url ); ... }}} - === Setting XMLResponseParser === + == Setting XMLResponseParser == 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 XMLResponseParser()); }}} - === Changing other Connection Settings === + == Changing other Connection Settings == If you need to control other connection settings, you can cast the server to !CommonsHttpSolrServer. {{{ @@ -40, +40 @@ [[Anchor(EmbeddedSolrServer)]] - == EmbeddedSolrServer == + = EmbeddedSolrServer = The [http://svn.apache.org/repos/asf/lucene/solr/trunk/client/java/solrj/src/org/apache/solr/client/solrj/embedded/EmbeddedSolrServer.java EmbeddedSolrServer] provides the same interface without requiring an HTTP connection. @@ -52, +52 @@ }}} - If you need to use solr in an embedded application, this is the recommended approach. It allows you to work with the same interface whether or not you have access to HTTP. + If you need to use solr in an embedded application, this is the recommended approach. It allows you to work with the same interface whether or not you have access to HTTP. - /!\ Note -- !EmbeddedSolrServer works only with handlers registered in [http://wiki.apache.org/solr/SolrConfigXml solrconfig.xml]. A [http://wiki.apache.org/solr/SolrRequestHandler RequestHandler] must be mapped to /update for a request to /update to function. + /!\ Note -- !EmbeddedSolrServer works only with handlers registered in [http://wiki.apache.org/solr/SolrConfigXml solrconfig.xml]. A [http://wiki.apache.org/solr/SolrRequestHandler RequestHandler] must be mapped to /update for a request to /update to function. = Usage = - - /!\ TODO - coming soon... - Solrj is designed as an extendable framework to pass [http://svn.apache.org/repos/asf/lucene/solr/trunk/client/java/solrj/src/org/apache/solr/client/solrj/SolrRequest.java SolrRequest] to the [http://svn.apache.org/repos/asf/lucene/solr/trunk/client/java/solrj/src/org/apache/solr/client/solrj/SolrServer.java SolrServer] and return a [http://svn.apache.org/repos/asf/lucene/solr/trunk/client/java/solrj/src/org/apache/solr/client/solrj/SolrResponse.java SolrResponse]. @@ -67, +64 @@ == Adding Data to Solr == - * Get an instance of server first + * Get an instance of server first {{{ SolrServer server = getSolrServer(); }}} - * If you wish to clean up the index before adding data do this + * If you wish to clean up the index before adding data do this {{{ server.deleteByQuery( "*:*" );// delete everything! }}} - * Construct a document + * Construct a document {{{ 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). + * 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 + * Create a collection of documents {{{ Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>(); docs.add( doc1 ); docs.add( doc2 ); }}} - *Add the documents to Solr + * Add the documents to Solr {{{ server.add( docs ); }}} - * Do a commit + * Do a commit {{{ server.commit(); }}} - *To immediately commit after adding documents, you could use: + * To immediately commit after adding documents, you could use: {{{ UpdateRequest req = new UpdateRequest(); @@ -114, +111 @@ }}} == Reading Data from Solr == - * Get an instance of server first + * Get an instance of server first {{{ SolrServer server = getSolrServer(); }}} - * Construct a !SolrQuery + * Construct a !SolrQuery {{{ SolrQuery query = new SolrQuery(); query.setQuery( "*:*" ); query.addSortField( "price", SolrQuery.ORDER.asc ); }}} - * Query the server + * Query the server {{{ QueryResponse rsp = server.query( query ); }}} - * Get the results + * Get the results {{{ SolrDocumentList docs = rsp.getResults(); }}}
