You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-commits@lucene.apache.org by Apache Wiki <wi...@apache.org> on 2007/09/10 21:22:33 UTC

[Solr Wiki] Update of "Solrj" by ryan

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 ryan:
http://wiki.apache.org/solr/Solrj

The comment on the change is:
initial solrj page... more to come

New page:
<!> ["Solr1.3"] 

Solrj is a java client to access solr.  It offers a java interface to add, update, and query the solr index.

[[TableOfContents]]

= 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.

{{{
  String url = "http://localhost:8983/solr"
  SolrServer server = new CommonsHttpSolrServer( url );
  ...
}}} 

If you need to control other connection settings, you can cast the server to !CommonsHttpSolrServer.

{{{
  String url = "http://localhost:8983/solr"
  SolrServer server = new CommonsHttpSolrServer( url );
  ((CommonsHttpSolrServer)server).setConnectionTimeout(5);
  ((CommonsHttpSolrServer)server).setDefaultMaxConnectionsPerHost(100);
  ((CommonsHttpSolrServer)server).setMaxTotalConnections(100);
}}} 


== 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.  

{{{

  SolrCore core = SolrCore.getSolrCore();
  SolrServer server = new EmbeddedSolrServer( core );
  ...

}}} 

If you need to use solr in an embedded application, this is the reccomended approach.  It allows you to work with the same interface whether or not you have access to HTTP.


= Usage =

/!\ TODO - coming soon...

(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 );

    SolrInputDocument doc2 = new SolrInputDocument();
    doc2.addField( "id", "id2", 1.0f );
    doc2.addField( "name", "doc2", 1.0f );
    doc2.addField( "price", 20 );
    
    Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
    docs.add( doc1 );
    docs.add( doc2 );
    
    // Add the documents
    server.add( docs );
    server.commit();
    
    SolrQuery query = new SolrQuery();
    query.setQuery( "*:*" );
    query.addSortField( "price", SolrQuery.ORDER.asc );
    QueryResponse rsp = server.query( query );
    
    Assert.assertEquals( 2, rsp.getResults().getNumFound() );
    System.out.println( rsp.getResults() );

}}}