SOLR-536 - adding the binder to SolrServer.

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@666684 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Ryan McKinley 2008-06-11 14:38:49 +00:00
parent 2ce5049e36
commit a00f94c4a9
3 changed files with 50 additions and 4 deletions

View File

@ -20,6 +20,7 @@ package org.apache.solr.client.solrj;
import java.io.IOException;
import java.io.Serializable;
import java.util.Collection;
import java.util.ArrayList;
import org.apache.solr.client.solrj.request.QueryRequest;
import org.apache.solr.client.solrj.request.SolrPing;
@ -27,6 +28,7 @@ import org.apache.solr.client.solrj.request.UpdateRequest;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.SolrPingResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.client.solrj.beans.DocumentObjectBinder;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.NamedList;
@ -37,6 +39,8 @@ import org.apache.solr.common.util.NamedList;
*/
public abstract class SolrServer implements Serializable
{
private DocumentObjectBinder binder;
public UpdateResponse add(Collection<SolrInputDocument> docs, boolean overwrite ) throws SolrServerException, IOException {
UpdateRequest req = new UpdateRequest();
req.add(docs);
@ -44,6 +48,15 @@ public abstract class SolrServer implements Serializable
return req.process(this);
}
public UpdateResponse addBeans(Collection<Object> beans, boolean overwrite ) throws SolrServerException, IOException {
DocumentObjectBinder binder = this.getBinder();
ArrayList<SolrInputDocument> docs = new ArrayList<SolrInputDocument>(beans.size());
for (Object bean : beans) {
docs.add(binder.toSolrInputDocument(bean));
}
return add(docs,overwrite);
}
public UpdateResponse add(SolrInputDocument doc, boolean overwrite ) throws SolrServerException, IOException {
UpdateRequest req = new UpdateRequest();
req.add(doc);
@ -51,14 +64,26 @@ public abstract class SolrServer implements Serializable
return req.process(this);
}
public UpdateResponse addBean(Object obj, boolean overwrite) throws IOException, SolrServerException {
return add(getBinder().toSolrInputDocument(obj), overwrite);
}
public UpdateResponse add(SolrInputDocument doc) throws SolrServerException, IOException {
return add(doc, true);
}
public UpdateResponse addBean(Object obj) throws IOException, SolrServerException {
return add(getBinder().toSolrInputDocument(obj), true);
}
public UpdateResponse add(Collection<SolrInputDocument> docs) throws SolrServerException, IOException {
return add(docs, true);
}
public UpdateResponse addBeans(Collection<Object> beans ) throws SolrServerException, IOException {
return addBeans(beans,true);
}
/** waitFlush=true and waitSearcher=true to be inline with the defaults for plain HTTP access
* @throws IOException
*/
@ -100,5 +125,12 @@ public abstract class SolrServer implements Serializable
/**
* SolrServer implementations need to implement a how a request is actually processed
*/
public abstract NamedList<Object> request( final SolrRequest request ) throws SolrServerException, IOException;
public abstract NamedList<Object> request( final SolrRequest request ) throws SolrServerException, IOException;
public DocumentObjectBinder getBinder() {
if(binder == null){
binder = new DocumentObjectBinder();
}
return binder;
}
}

View File

@ -81,7 +81,7 @@ public class QueryRequest extends SolrRequest
try
{
long startTime = System.currentTimeMillis();
QueryResponse res = new QueryResponse( server.request( this ) );
QueryResponse res = new QueryResponse( server.request( this ), server);
res.setElapsedTime( System.currentTimeMillis()-startTime );
return res;
}

View File

@ -26,6 +26,8 @@ import java.util.Map;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.beans.DocumentObjectBinder;
/**
*
@ -47,15 +49,21 @@ public class QueryResponse extends SolrResponseBase
private List<FacetField> _facetFields = null;
private List<FacetField> _limitingFacets = null;
private List<FacetField> _facetDates = null;
// Highlight Info
private Map<String,Map<String,List<String>>> _highlighting = null;
// Debug Info
private Map<String,Object> _debugMap = null;
private Map<String,String> _explainMap = null;
private SolrServer solrServer;
public QueryResponse( NamedList<Object> res , SolrServer solrServer){
this(res);
this.solrServer = solrServer;
}
public QueryResponse( NamedList<Object> res )
public QueryResponse( NamedList<Object> res )
{
super( res );
@ -241,6 +249,12 @@ public class QueryResponse extends SolrResponseBase
public List<FacetField> getLimitingFacets() {
return _limitingFacets;
}
public <T> List<T> getBeans(Class<T> type){
return solrServer == null ?
new DocumentObjectBinder().getBeans(type,_results):
solrServer.getBinder().getBeans(type, _results);
}
}