From 34dce548e36ad57d48a09df02edaf42bf9812f97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20H=C3=B8ydahl?= Date: Wed, 14 Sep 2011 12:28:29 +0000 Subject: [PATCH] SOLR-2742: Add commitWithin to convenience signatures for SolrServer.add(..) git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1170569 13f79535-47bb-0310-9956-ffa450edef68 --- solr/CHANGES.txt | 3 + .../apache/solr/client/solrj/SolrServer.java | 164 +++++++++++++++++- .../solrj/impl/CommonsHttpSolrServer.java | 34 +++- .../solr/client/solrj/SolrExampleTests.java | 23 +++ 4 files changed, 213 insertions(+), 11 deletions(-) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index bc3267407fe..fdf0a41d4fd 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -175,6 +175,9 @@ New Features Optimizations ---------------------- +* SOLR-2742: SolrJ: Provide commitWithinMs as optional parameter for all add() methods, + making the feature more conveniently accessible for developers (janhoy) + * SOLR-1875: Per-segment field faceting for single valued string fields. Enable with facet.method=fcs, control the number of threads used with the "threads" local param on the facet.field param. This algorithm will diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/SolrServer.java b/solr/solrj/src/java/org/apache/solr/client/solrj/SolrServer.java index 4f5f86fec1a..72d049d856f 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/SolrServer.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/SolrServer.java @@ -42,83 +42,237 @@ import org.apache.solr.common.util.NamedList; */ public abstract class SolrServer implements Serializable { + private static final long serialVersionUID = 1L; private DocumentObjectBinder binder; - public UpdateResponse add(Collection docs ) throws SolrServerException, IOException { + /** + * Adds a collection of documents + * @param docs the collection of documents + * @throws SolrServerException + * @throws IOException + */ + public UpdateResponse add(Collection docs) throws SolrServerException, IOException { + return add(docs, -1); + } + + /** + * Adds a collection of documents, specifying max time before they become committed + * @param docs the collection of documents + * @param commitWithinMs max time (in ms) before a commit will happen + * @throws SolrServerException + * @throws IOException + * @since solr 3.4 + */ + public UpdateResponse add(Collection docs, int commitWithinMs) throws SolrServerException, IOException { UpdateRequest req = new UpdateRequest(); req.add(docs); + req.setCommitWithin(commitWithinMs); return req.process(this); } + /** + * Adds a collection of beans + * @param beans the collection of beans + * @throws SolrServerException + * @throws IOException + */ public UpdateResponse addBeans(Collection beans ) throws SolrServerException, IOException { + return addBeans(beans, -1); + } + + /** + * Adds a collection of beans specifying max time before they become committed + * @param beans the collection of beans + * @param commitWithinMs max time (in ms) before a commit will happen + * @throws SolrServerException + * @throws IOException + * @since solr 3.4 + */ + public UpdateResponse addBeans(Collection beans, int commitWithinMs) throws SolrServerException, IOException { DocumentObjectBinder binder = this.getBinder(); ArrayList docs = new ArrayList(beans.size()); for (Object bean : beans) { docs.add(binder.toSolrInputDocument(bean)); } - return add(docs); + return add(docs, commitWithinMs); } + /** + * Adds a single document + * @param doc the input document + * @throws SolrServerException + * @throws IOException + */ public UpdateResponse add(SolrInputDocument doc ) throws SolrServerException, IOException { + return add(doc, -1); + } + + /** + * Adds a single document specifying max time before it becomes committed + * @param doc the input document + * @param commitWithinMs max time (in ms) before a commit will happen + * @throws SolrServerException + * @throws IOException + * @since solr 3.4 + */ + public UpdateResponse add(SolrInputDocument doc, int commitWithinMs) throws SolrServerException, IOException { UpdateRequest req = new UpdateRequest(); req.add(doc); + req.setCommitWithin(commitWithinMs); return req.process(this); } + /** + * Adds a single bean + * @param obj the input bean + * @throws SolrServerException + * @throws IOException + */ public UpdateResponse addBean(Object obj) throws IOException, SolrServerException { - return add(getBinder().toSolrInputDocument(obj)); + return addBean(obj, -1); } - /** waitFlush=true and waitSearcher=true to be inline with the defaults for plain HTTP access + /** + * Adds a single bean specifying max time before it becomes committed + * @param obj the input bean + * @param commitWithinMs max time (in ms) before a commit will happen + * @throws SolrServerException + * @throws IOException + * @since solr 3.4 + */ + public UpdateResponse addBean(Object obj, int commitWithinMs) throws IOException, SolrServerException { + return add(getBinder().toSolrInputDocument(obj),commitWithinMs); + } + + /** + * Performs an explicit commit, causing pending documents to be committed for indexing + *

+ * waitFlush=true and waitSearcher=true to be inline with the defaults for plain HTTP access + * @throws SolrServerException * @throws IOException */ public UpdateResponse commit( ) throws SolrServerException, IOException { return commit(true, true); } - /** waitFlush=true and waitSearcher=true to be inline with the defaults for plain HTTP access + /** + * Performs an explicit optimize, causing a merge of all segments to one. + *

+ * waitFlush=true and waitSearcher=true to be inline with the defaults for plain HTTP access + *

+ * Note: In most cases it is not required to do explicit optimize + * @throws SolrServerException * @throws IOException */ public UpdateResponse optimize( ) throws SolrServerException, IOException { return optimize(true, true, 1); } + /** + * Performs an explicit commit, causing pending documents to be committed for indexing + * @param waitFlush block until index changes are flushed to disk + * @param waitSearcher block until a new searcher is opened and registered as the main query searcher, making the changes visible + * @throws SolrServerException + * @throws IOException + */ public UpdateResponse commit( boolean waitFlush, boolean waitSearcher ) throws SolrServerException, IOException { return new UpdateRequest().setAction( UpdateRequest.ACTION.COMMIT, waitFlush, waitSearcher ).process( this ); } + /** + * Performs an explicit optimize, causing a merge of all segments to one. + *

+ * Note: In most cases it is not required to do explicit optimize + * @param waitFlush block until index changes are flushed to disk + * @param waitSearcher block until a new searcher is opened and registered as the main query searcher, making the changes visible + * @throws SolrServerException + * @throws IOException + */ public UpdateResponse optimize( boolean waitFlush, boolean waitSearcher ) throws SolrServerException, IOException { return optimize(waitFlush, waitSearcher, 1); } + /** + * Performs an explicit optimize, causing a merge of all segments to one. + *

+ * Note: In most cases it is not required to do explicit optimize + * @param waitFlush block until index changes are flushed to disk + * @param waitSearcher block until a new searcher is opened and registered as the main query searcher, making the changes visible + * @param maxSegments optimizes down to at most this number of segments + * @throws SolrServerException + * @throws IOException + */ public UpdateResponse optimize(boolean waitFlush, boolean waitSearcher, int maxSegments ) throws SolrServerException, IOException { return new UpdateRequest().setAction( UpdateRequest.ACTION.OPTIMIZE, waitFlush, waitSearcher, maxSegments ).process( this ); } + /** + * Performs a rollback of all non-committed documents pending. + *

+ * Note that this is not a true rollback as in databases. Content you have previously + * added may have been committed due to autoCommit, buffer full, other client performing + * a commit etc. + * @throws SolrServerException + * @throws IOException + */ public UpdateResponse rollback() throws SolrServerException, IOException { return new UpdateRequest().rollback().process( this ); } + /** + * Deletes a single document by unique ID + * @param id the ID of the document to delete + * @throws SolrServerException + * @throws IOException + */ public UpdateResponse deleteById(String id) throws SolrServerException, IOException { return new UpdateRequest().deleteById( id ).process( this ); } + /** + * Deletes a list of documents by unique ID + * @param ids the list of document IDs to delete + * @throws SolrServerException + * @throws IOException + */ public UpdateResponse deleteById(List ids) throws SolrServerException, IOException { return new UpdateRequest().deleteById( ids ).process( this ); } + /** + * Deletes documents from the index based on a query + * @param query the query expressing what documents to delete + * @throws SolrServerException + * @throws IOException + */ public UpdateResponse deleteByQuery(String query) throws SolrServerException, IOException { return new UpdateRequest().deleteByQuery( query ).process( this ); } + /** + * Issues a ping request to check if the server is alive + * @throws SolrServerException + * @throws IOException + */ public SolrPingResponse ping() throws SolrServerException, IOException { return new SolrPing().process( this ); } + /** + * Performs a query to the Solr server + * @param params an object holding all key/value parameters to send along the request + * @throws SolrServerException + */ public QueryResponse query(SolrParams params) throws SolrServerException { return new QueryRequest( params ).process( this ); } + /** + * Performs a query to the Solr server + * @param params an object holding all key/value parameters to send along the request + * @param method specifies the HTTP method to use for the request, such as GET or POST + * @throws SolrServerException + */ public QueryResponse query(SolrParams params, METHOD method) throws SolrServerException { return new QueryRequest( params, method ).process( this ); } diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CommonsHttpSolrServer.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CommonsHttpSolrServer.java index 5a928857f5f..e60d9afa258 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CommonsHttpSolrServer.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CommonsHttpSolrServer.java @@ -62,6 +62,8 @@ import org.slf4j.LoggerFactory; */ public class CommonsHttpSolrServer extends SolrServer { + private static final long serialVersionUID = 1L; + /** * User-Agent String as identified by the HTTP request by the {@link * org.apache.commons.httpclient.HttpClient HttpClient} to the Solr @@ -613,27 +615,46 @@ public class CommonsHttpSolrServer extends SolrServer /** * Adds the documents supplied by the given iterator. - * * @param docIterator the iterator which returns SolrInputDocument instances - * * @return the response from the SolrServer */ public UpdateResponse add(Iterator docIterator) throws SolrServerException, IOException { + return add(docIterator, -1); + } + + /** + * Adds the documents supplied by the given iterator, specifying max time before they become committed + * @param docIterator the iterator which returns SolrInputDocument instances + * @param commitWithinMs the time in milliseconds before a commit automatically is triggered + * @return the response from the SolrServer + */ + public UpdateResponse add(Iterator docIterator, int commitWithinMs) + throws SolrServerException, IOException { UpdateRequest req = new UpdateRequest(); - req.setDocIterator(docIterator); + req.setDocIterator(docIterator); + req.setCommitWithin(commitWithinMs); return req.process(this); } /** - * Adds the beans supplied by the given iterator. - * + * Adds the beans supplied by the given iterator * @param beanIterator the iterator which returns Beans - * * @return the response from the SolrServer */ public UpdateResponse addBeans(final Iterator beanIterator) throws SolrServerException, IOException { + return addBeans(beanIterator, -1); + } + + /** + * Adds the beans supplied by the given iterator, specifying max time before they become committed + * @param commitWithinMs the time in milliseconds before a commit automatically is triggered + * @param beanIterator the iterator which returns Beans + * @return the response from the SolrServer + */ + public UpdateResponse addBeans(final Iterator beanIterator, int commitWithinMs) + throws SolrServerException, IOException { UpdateRequest req = new UpdateRequest(); req.setDocIterator(new Iterator() { @@ -651,6 +672,7 @@ public class CommonsHttpSolrServer extends SolrServer beanIterator.remove(); } }); + req.setCommitWithin(commitWithinMs); return req.process(this); } } diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.java b/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.java index 8c6263cecde..75cfe8045e4 100644 --- a/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.java +++ b/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.java @@ -377,6 +377,29 @@ abstract public class SolrExampleTests extends SolrJettyTestBase } Assert.assertEquals( 1, rsp.getResults().getNumFound() ); + + + // Now test the new convenience parameter on the add() for commitWithin + SolrInputDocument doc4 = new SolrInputDocument(); + doc4.addField( "id", "id4", 1.0f ); + doc4.addField( "name", "doc4", 1.0f ); + doc4.addField( "price", 10 ); + server.add(doc4, 500); + + Thread.sleep( 1000 ); // wait 1 sec + + // now check that it comes out... + rsp = server.query( new SolrQuery( "id:id4") ); + + if(rsp.getResults().getNumFound() == 0) { + // wait and try again for slower machines + Thread.sleep( 2000 ); // wait 2 seconds... + + rsp = server.query( new SolrQuery( "id:id4") ); + } + + Assert.assertEquals( 1, rsp.getResults().getNumFound() ); + }