lucene-solr-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Erik Hatcher <>
Subject Re: Java or PHP code to trigger solr?
Date Thu, 29 Jun 2006 01:26:58 GMT

On Jun 28, 2006, at 3:39 PM, UpAndGone wrote:
> I was just planning to write a web service like front-end to Lucene  
> when I
> heard of this project. Are there any examples on integrating Solr  
> yet? I see
> the wiki section, but it is empty. How can I trigger solr, say from  
> my Java
> app without using curl?

My indexer currently does this to post to Solr from Java:

   private void postToSolr(String xml) throws IOException {
//    System.out.println("-------------");
//    System.out.println("xml = " + xml);
//    System.out.println("-------------");
     PostMethod post = new PostMethod("http://localhost:8983/solr/ 
     post.setRequestEntity(new StringRequestEntity(xml));
     post.setRequestHeader("Content-type", "text/xml; charset=utf-8");

     HttpClient httpclient = new HttpClient();
     // Execute request
     try {
       int result = httpclient.executeMethod(post);
       String response = post.getResponseBodyAsString();
//      System.out.println(response);
       Pattern pattern = Pattern.compile("status=\\\"(\\d*)\\\">(.*)\ 
\<\\/result\\>", Pattern.DOTALL);
       Matcher matcher = pattern.matcher(response);
       while (matcher.find()) {
         String status =;
         String message =;
         if (!"0".equals(status)) {
           throw new IOException(message);
     } finally {
       // Release current connection to the connection pool once you  
are done

postToSolr("<commit/>");  // for example

My error checking is lame though, mainly because Solr doesn't (or  
didn't?) send an HTTP error response when bad things happened in a  
request handler for instance.

I've been too lazy (well, ok, swamped) to pull out this flakey custom  
hacked code with this much more elegant and robust API contributed  
here <>.  This will be  
incorporated in Solr's codebase in some form when we figure out where  
to put it, how to distributed it, and document it.  If you use it,  
feedback on it would be appreciated.


View raw message