lucene-solr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gsing...@apache.org
Subject svn commit: r813416 - in /lucene/solr/trunk/src: solrj/org/apache/solr/client/solrj/request/ test/org/apache/solr/client/solrj/ test/test-files/
Date Thu, 10 Sep 2009 13:07:20 GMT
Author: gsingers
Date: Thu Sep 10 13:07:19 2009
New Revision: 813416

URL: http://svn.apache.org/viewvc?rev=813416&view=rev
Log:
SOLR-1411: Add support for uploading ContentStreams via SolrJ

Added:
    lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/request/AbstractUpdateRequest.java
  (with props)
    lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/request/ContentStreamUpdateRequest.java
  (with props)
    lucene/solr/trunk/src/test/test-files/books.csv
      - copied unchanged from r813405, lucene/solr/trunk/example/exampledocs/books.csv
    lucene/solr/trunk/src/test/test-files/mailing_lists.pdf
      - copied unchanged from r813405, lucene/solr/trunk/site/mailing_lists.pdf
Modified:
    lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/request/UpdateRequest.java
    lucene/solr/trunk/src/test/org/apache/solr/client/solrj/SolrExampleTests.java

Added: lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/request/AbstractUpdateRequest.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/request/AbstractUpdateRequest.java?rev=813416&view=auto
==============================================================================
--- lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/request/AbstractUpdateRequest.java
(added)
+++ lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/request/AbstractUpdateRequest.java
Thu Sep 10 13:07:19 2009
@@ -0,0 +1,126 @@
+package org.apache.solr.client.solrj.request;
+
+import org.apache.solr.client.solrj.SolrRequest;
+import org.apache.solr.client.solrj.SolrServer;
+import org.apache.solr.client.solrj.SolrServerException;
+import org.apache.solr.client.solrj.response.UpdateResponse;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.common.params.UpdateParams;
+
+import java.io.IOException;
+
+
+/**
+ *
+ *
+ **/
+public abstract class AbstractUpdateRequest extends SolrRequest {
+  protected ModifiableSolrParams params;
+  protected int commitWithin = -1;
+
+  public enum ACTION {
+    COMMIT,
+    OPTIMIZE
+  }
+
+  public AbstractUpdateRequest(METHOD m, String path) {
+    super(m, path);
+  }
+
+  /** Sets appropriate parameters for the given ACTION */
+  public AbstractUpdateRequest setAction(ACTION action, boolean waitFlush, boolean waitSearcher
) {
+    return setAction(action, waitFlush, waitSearcher, 1);
+  }
+
+  public AbstractUpdateRequest setAction(ACTION action, boolean waitFlush, boolean waitSearcher,
int maxSegments ) {
+    if (params == null)
+      params = new ModifiableSolrParams();
+
+    if( action == ACTION.OPTIMIZE ) {
+      params.set( UpdateParams.OPTIMIZE, "true" );
+      params.set(UpdateParams.MAX_OPTIMIZE_SEGMENTS, maxSegments);
+    }
+    else if( action == ACTION.COMMIT ) {
+      params.set( UpdateParams.COMMIT, "true" );
+    }
+    params.set( UpdateParams.WAIT_FLUSH, String.valueOf(waitFlush));
+    params.set( UpdateParams.WAIT_SEARCHER, String.valueOf(waitSearcher));
+    return this;
+  }
+
+  public AbstractUpdateRequest setAction(ACTION action, boolean waitFlush, boolean waitSearcher,
int maxSegments , boolean expungeDeletes) {
+    setAction(action, waitFlush, waitSearcher,maxSegments) ;
+    params.set(UpdateParams.EXPUNGE_DELETES, String.valueOf(expungeDeletes));
+    return this;
+  }
+
+  /**
+   * @since Solr 1.4
+   */
+  public AbstractUpdateRequest rollback() {
+    if (params == null)
+      params = new ModifiableSolrParams();
+
+    params.set( UpdateParams.ROLLBACK, "true" );
+    return this;
+  }
+
+  public void setParam(String param, String value) {
+    if (params == null)
+      params = new ModifiableSolrParams();
+    params.set(param, value);
+  }
+
+  /** Sets the parameters for this update request, overwriting any previous */
+  public void setParams(ModifiableSolrParams params) {
+    this.params = params;
+  }
+
+  @Override
+  public ModifiableSolrParams getParams() {
+    return params;
+  }
+
+  @Override
+  public UpdateResponse process( SolrServer server ) throws SolrServerException, IOException
+  {
+    long startTime = System.currentTimeMillis();
+    UpdateResponse res = new UpdateResponse();
+    res.setResponse( server.request( this ) );
+    res.setElapsedTime( System.currentTimeMillis()-startTime );
+    return res;
+  }
+
+  public boolean isWaitFlush() {
+    return params != null && params.getBool(UpdateParams.WAIT_FLUSH, false);
+  }
+
+  public boolean isWaitSearcher() {
+    return params != null && params.getBool(UpdateParams.WAIT_SEARCHER, false);
+  }
+
+  public ACTION getAction() {
+    if (params==null) return null;
+    if (params.getBool(UpdateParams.COMMIT, false)) return ACTION.COMMIT;
+    if (params.getBool(UpdateParams.OPTIMIZE, false)) return ACTION.OPTIMIZE;
+    return null;
+  }
+
+  public void setWaitFlush(boolean waitFlush) {
+    setParam( UpdateParams.WAIT_FLUSH, waitFlush+"" );
+  }
+
+  public void setWaitSearcher(boolean waitSearcher) {
+    setParam( UpdateParams.WAIT_SEARCHER, waitSearcher+"" );
+  }
+
+  public int getCommitWithin() {
+    return commitWithin;
+  }
+
+  public void setCommitWithin(int commitWithin) {
+    this.commitWithin = commitWithin;
+  }
+
+
+}

Propchange: lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/request/AbstractUpdateRequest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/request/ContentStreamUpdateRequest.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/request/ContentStreamUpdateRequest.java?rev=813416&view=auto
==============================================================================
--- lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/request/ContentStreamUpdateRequest.java
(added)
+++ lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/request/ContentStreamUpdateRequest.java
Thu Sep 10 13:07:19 2009
@@ -0,0 +1,79 @@
+package org.apache.solr.client.solrj.request;
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+import org.apache.solr.client.solrj.SolrRequest;
+import org.apache.solr.client.solrj.SolrServer;
+import org.apache.solr.client.solrj.SolrServerException;
+import org.apache.solr.client.solrj.response.UpdateResponse;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.common.params.SolrParams;
+import org.apache.solr.common.util.ContentStream;
+import org.apache.solr.common.util.ContentStreamBase;
+
+import java.io.IOException;
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+
+/**
+ * Basic functionality to upload a File to a Solr Cell
+ * <p/>
+ * See http://wiki.apache.org/solr/ExtractingRequestHandler
+ *
+ **/
+public class ContentStreamUpdateRequest extends AbstractUpdateRequest {
+  List<ContentStream> contentStreams;
+
+  /**
+   *
+   * @param url The URL for Solr Cell.
+   */
+  public ContentStreamUpdateRequest(String url) {
+    super(METHOD.POST, url);
+    contentStreams = new ArrayList<ContentStream>();
+  }
+
+  @Override
+  public Collection<ContentStream> getContentStreams() throws IOException {
+    return contentStreams;
+  }
+
+  /**
+   * Add a File to the {@link org.apache.solr.common.util.ContentStream}s.
+   * @param file The File to add.
+   * @throws IOException if there was an error with the file.
+   *
+   * @see #getContentStreams()
+   * @see org.apache.solr.common.util.ContentStreamBase.FileStream
+   */
+  public void addFile(File file) throws IOException {
+    addContentStream(new ContentStreamBase.FileStream(file));
+  }
+
+  /**
+   * Add a {@link org.apache.solr.common.util.ContentStream} to {@link #getContentStreams()}
+   * @param contentStream The {@link org.apache.solr.common.util.ContentStream}
+   */
+  public void addContentStream(ContentStream contentStream){
+    contentStreams.add(contentStream);
+  }
+  
+}

Propchange: lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/request/ContentStreamUpdateRequest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/request/UpdateRequest.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/request/UpdateRequest.java?rev=813416&r1=813415&r2=813416&view=diff
==============================================================================
--- lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/request/UpdateRequest.java (original)
+++ lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/request/UpdateRequest.java Thu
Sep 10 13:07:19 2009
@@ -41,8 +41,12 @@
  * @version $Id$
  * @since solr 1.3
  */
-public class UpdateRequest extends SolrRequest
-{
+public class UpdateRequest extends AbstractUpdateRequest {
+  /**
+   * Kept for back compatibility.
+   *
+   * @deprecated Use {@link AbstractUpdateRequest.ACTION} instead
+   */
   public enum ACTION {
     COMMIT,
     OPTIMIZE
@@ -53,9 +57,6 @@
   private List<String> deleteById = null;
   private List<String> deleteQuery = null;
 
-  private ModifiableSolrParams params;
-  private int commitWithin = -1;
-  
   public UpdateRequest()
   {
     super( METHOD.POST, "/update" );
@@ -132,11 +133,18 @@
     return this;
   }
 
-  /** Sets appropriate parameters for the given ACTION */
+  /** Sets appropriate parameters for the given ACTION
+   *
+   * @deprecated Use {@link org.apache.solr.client.solrj.request.AbstractUpdateRequest.ACTION}
instead
+   * */
   public UpdateRequest setAction(ACTION action, boolean waitFlush, boolean waitSearcher )
{
     return setAction(action, waitFlush, waitSearcher, 1);
   }
 
+  /**
+   *
+   * @deprecated Use {@link org.apache.solr.client.solrj.request.AbstractUpdateRequest.ACTION}
instead
+   */
   public UpdateRequest setAction(ACTION action, boolean waitFlush, boolean waitSearcher,
int maxSegments ) {
     if (params == null)
       params = new ModifiableSolrParams();
@@ -153,34 +161,17 @@
     return this;
   }
 
+  /**
+   *
+   *
+   * @deprecated Use {@link org.apache.solr.client.solrj.request.AbstractUpdateRequest.ACTION}
instead
+   */
   public UpdateRequest setAction(ACTION action, boolean waitFlush, boolean waitSearcher,
int maxSegments , boolean expungeDeletes) {
     setAction(action, waitFlush, waitSearcher,maxSegments) ;
     params.set(UpdateParams.EXPUNGE_DELETES,""+expungeDeletes);
     return this;
   }
 
-  /**
-   * @since Solr 1.4
-   */
-  public UpdateRequest rollback() {
-    if (params == null)
-      params = new ModifiableSolrParams();
-
-    params.set( UpdateParams.ROLLBACK, "true" );
-    return this;
-  }
-  
-
-  public void setParam(String param, String value) {
-    if (params == null)
-      params = new ModifiableSolrParams();
-    params.set(param, value);
-  }
-
-  /** Sets the parameters for this update request, overwriting any previous */
-  public void setParams(ModifiableSolrParams params) {
-    this.params = params;
-  }
 
   public void setDocIterator(Iterator<SolrInputDocument> docIterator) {
     this.docIterator = docIterator;
@@ -261,21 +252,6 @@
   //--------------------------------------------------------------------------
   //--------------------------------------------------------------------------
 
-  @Override
-  public ModifiableSolrParams getParams() {
-    return params;
-  }
-  
-  @Override
-  public UpdateResponse process( SolrServer server ) throws SolrServerException, IOException
-  {
-    long startTime = System.currentTimeMillis();
-    UpdateResponse res = new UpdateResponse();
-    res.setResponse( server.request( this ) );
-    res.setElapsedTime( System.currentTimeMillis()-startTime );
-    return res;
-  }
-  
   //--------------------------------------------------------------------------
   // 
   //--------------------------------------------------------------------------
@@ -296,34 +272,4 @@
     return deleteQuery;
   }
 
-  public boolean isWaitFlush() {
-    return params != null && params.getBool(UpdateParams.WAIT_FLUSH, false);
-  }
-
-  public boolean isWaitSearcher() {
-    return params != null && params.getBool(UpdateParams.WAIT_SEARCHER, false);
-  }
-
-  public ACTION getAction() {
-    if (params==null) return null;
-    if (params.getBool(UpdateParams.COMMIT, false)) return ACTION.COMMIT; 
-    if (params.getBool(UpdateParams.OPTIMIZE, false)) return ACTION.OPTIMIZE;
-    return null;
-  }
-
-  public void setWaitFlush(boolean waitFlush) {
-    setParam( UpdateParams.WAIT_FLUSH, waitFlush+"" );
-  }
-
-  public void setWaitSearcher(boolean waitSearcher) {
-    setParam( UpdateParams.WAIT_SEARCHER, waitSearcher+"" );
-  }
-
-  public int getCommitWithin() {
-    return commitWithin;
-  }
-
-  public void setCommitWithin(int commitWithin) {
-    this.commitWithin = commitWithin;
-  }
 }

Modified: lucene/solr/trunk/src/test/org/apache/solr/client/solrj/SolrExampleTests.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/test/org/apache/solr/client/solrj/SolrExampleTests.java?rev=813416&r1=813415&r2=813416&view=diff
==============================================================================
--- lucene/solr/trunk/src/test/org/apache/solr/client/solrj/SolrExampleTests.java (original)
+++ lucene/solr/trunk/src/test/org/apache/solr/client/solrj/SolrExampleTests.java Thu Sep
10 13:07:19 2009
@@ -20,6 +20,7 @@
 
 import java.io.IOException;
 import java.io.StringWriter;
+import java.io.File;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
@@ -31,6 +32,8 @@
 import org.apache.solr.client.solrj.request.SolrPing;
 import org.apache.solr.client.solrj.response.FieldStatsInfo;
 import org.apache.solr.client.solrj.request.UpdateRequest;
+import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;
+import org.apache.solr.client.solrj.request.AbstractUpdateRequest;
 import org.apache.solr.client.solrj.response.LukeResponse;
 import org.apache.solr.client.solrj.response.QueryResponse;
 import org.apache.solr.client.solrj.response.FacetField;
@@ -38,6 +41,7 @@
 import org.apache.solr.client.solrj.util.ClientUtils;
 import org.apache.solr.common.SolrInputDocument;
 import org.apache.solr.common.util.XML;
+import org.apache.solr.common.util.NamedList;
 import org.apache.solr.common.params.FacetParams;
 
 /**
@@ -239,8 +243,41 @@
     
     Assert.assertEquals( 1, rsp.getResults().getNumFound() );
   }
-  
-  
+
+
+  public void testContentStreamRequest() throws Exception {
+    SolrServer server = getSolrServer();
+    server.deleteByQuery( "*:*" );// delete everything!
+    server.commit();
+    QueryResponse rsp = server.query( new SolrQuery( "*:*") );
+    Assert.assertEquals( 0, rsp.getResults().getNumFound() );
+
+    ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update/csv");
+    up.addFile(new File("books.csv"));
+    up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
+    NamedList<Object> result = server.request(up);
+    assertNotNull("Couldn't upload books.csv", result);
+    rsp = server.query( new SolrQuery( "*:*") );
+    Assert.assertEquals( 10, rsp.getResults().getNumFound() );
+
+    server.deleteByQuery( "*:*" );// delete everything!
+    server.commit();
+    rsp = server.query( new SolrQuery( "*:*") );
+    Assert.assertEquals( 0, rsp.getResults().getNumFound() );
+
+    up = new ContentStreamUpdateRequest("/update/extract");
+    up.addFile(new File("mailing_lists.pdf"));
+    up.setParam("literal.id", "mailing_lists.pdf");
+    up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
+    result = server.request(up);
+    assertNotNull("Couldn't upload mailing_lists.pdf", result);
+    rsp = server.query( new SolrQuery( "*:*") );
+    Assert.assertEquals( 1, rsp.getResults().getNumFound() );
+
+
+  }
+
+
   protected void assertNumFound( String query, int num ) throws SolrServerException, IOException
   {
     QueryResponse rsp = getSolrServer().query( new SolrQuery( query ) );



Mime
View raw message