fixed problem with remote luke requests

pull/1/head
Michael Peter Christen 11 years ago
parent c15aa758dc
commit 31483c47e1

@ -34,11 +34,12 @@ import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;
import org.apache.solr.client.solrj.response.LukeResponse.FieldInfo;
import org.apache.solr.client.solrj.request.LukeRequest;
import org.apache.solr.client.solrj.response.LukeResponse.FieldInfo;
import org.apache.solr.client.solrj.response.LukeResponse;
public abstract class SolrServerConnector extends AbstractSolrConnector implements SolrConnector {
@ -92,10 +93,16 @@ public abstract class SolrServerConnector extends AbstractSolrConnector implemen
}
}
/**
* get the number of segments.
* @return the number of segments, or 0 if unknown
*/
public int getSegmentCount() {
if (this.server == null) return 0;
try {
NamedList<Object> info = getIndexBrowser().getIndexInfo();
LukeResponse lukeResponse = getIndexBrowser(false);
NamedList<Object> info = lukeResponse.getIndexInfo();
if (info == null) return 0;
Integer segmentCount = (Integer) info.get("segmentCount");
if (segmentCount == null) return 1;
return segmentCount.intValue();
@ -121,7 +128,11 @@ public abstract class SolrServerConnector extends AbstractSolrConnector implemen
public long getSize() {
if (this.server == null) return 0;
try {
return getIndexBrowser().getNumDocs();
LukeResponse lukeResponse = getIndexBrowser(false);
if (lukeResponse == null) return 0;
Integer numDocs = lukeResponse.getNumDocs();
if (numDocs == null) return 0;
return numDocs.longValue();
} catch (final Throwable e) {
log.warn(e);
return 0;
@ -277,13 +288,42 @@ public abstract class SolrServerConnector extends AbstractSolrConnector implemen
public Collection<FieldInfo> getFields() throws SolrServerException {
// get all fields contained in index
return getIndexBrowser().getFieldInfo().values();
return getIndexBrowser(true).getFieldInfo().values();
}
private LukeResponse getIndexBrowser() throws SolrServerException {
private LukeResponse getIndexBrowser(final boolean showSchema) throws SolrServerException {
// get all fields contained in index
final LukeRequest lukeRequest = new LukeRequest();
lukeRequest.setResponseParser(new XMLResponseParser());
lukeRequest.setNumTerms(1);
lukeRequest.setShowSchema(showSchema);
/*
final SolrRequest lukeRequest = new SolrRequest(METHOD.GET, "/admin/luke") {
private static final long serialVersionUID = 1L;
@Override
public Collection<ContentStream> getContentStreams() throws IOException {
return null;
}
@Override
public SolrParams getParams() {
ModifiableSolrParams params = new ModifiableSolrParams();
//params.add("numTerms", "1");
params.add("_", "" + System.currentTimeMillis()); // cheat a proxy
if (showSchema) params.add("show", "schema");
return params;
}
@Override
public LukeResponse process(SolrServer server) throws SolrServerException, IOException {
long startTime = System.currentTimeMillis();
LukeResponse res = new LukeResponse();
this.setResponseParser(new XMLResponseParser());
NamedList<Object> response = server.request(this);
res.setResponse(response);
res.setElapsedTime(System.currentTimeMillis() - startTime);
return res;
}
};
*/
LukeResponse lukeResponse = null;
try {
lukeResponse = lukeRequest.process(this.server);

@ -30,12 +30,16 @@ import org.apache.solr.common.util.SimpleOrderedMap;
public class ResponseAccumulator {
final SimpleOrderedMap<Object> fieldsAcc;
final SimpleOrderedMap<Object> index_countsAcc;
final SimpleOrderedMap<Object> facet_countsAcc;
final SimpleOrderedMap<Object> highlightingAcc;
final SimpleOrderedMap<Object> headerAcc;
final SolrDocumentList resultsAcc;
public ResponseAccumulator() {
this.fieldsAcc = new SimpleOrderedMap<Object>();
this.index_countsAcc = new SimpleOrderedMap<Object>();
this.facet_countsAcc = new SimpleOrderedMap<Object>();
this.highlightingAcc = new SimpleOrderedMap<Object>();
this.headerAcc = new SimpleOrderedMap<Object>();
@ -56,10 +60,12 @@ public class ResponseAccumulator {
// accumulate the results
SolrDocumentList results = (SolrDocumentList) response.get("response");
long found = results.size();
for (int i = 0; i < found; i++) resultsAcc.add(results.get(i));
resultsAcc.setNumFound(resultsAcc.getNumFound() + results.getNumFound());
resultsAcc.setMaxScore(Math.max(resultsAcc.getMaxScore() == null ? 0f : resultsAcc.getMaxScore().floatValue(), results.getMaxScore() == null ? 0f : results.getMaxScore().floatValue()));
if (results != null) {
long found = results.size();
for (int i = 0; i < found; i++) resultsAcc.add(results.get(i));
resultsAcc.setNumFound(resultsAcc.getNumFound() + results.getNumFound());
resultsAcc.setMaxScore(Math.max(resultsAcc.getMaxScore() == null ? 0f : resultsAcc.getMaxScore().floatValue(), results.getMaxScore() == null ? 0f : results.getMaxScore().floatValue()));
}
// accumulate the highlighting
@SuppressWarnings("unchecked")
@ -74,6 +80,29 @@ public class ResponseAccumulator {
if (facet_counts != null) {
for (Map.Entry<String, Object> e: facet_counts) facet_countsAcc.add(e.getKey(), e.getValue());
}
// accumulate the index (thats the result from a luke request)
@SuppressWarnings("unchecked")
SimpleOrderedMap<Object> index_counts = (SimpleOrderedMap<Object>) response.get("index");
if (index_counts != null) {
for (Map.Entry<String, Object> e: index_counts) index_countsAcc.add(e.getKey(), e.getValue());
}
// accumulate the fields (thats the result from a luke request)
@SuppressWarnings("unchecked")
SimpleOrderedMap<Object> schema = (SimpleOrderedMap<Object>) response.get("schema");
if (schema != null) {
@SuppressWarnings("unchecked")
SimpleOrderedMap<Object> fields = (SimpleOrderedMap<Object>) schema.get("fields");
if (fields != null) {
for (Map.Entry<String, Object> e: fields) fieldsAcc.add(e.getKey(), e.getValue());
}
}
@SuppressWarnings("unchecked")
SimpleOrderedMap<Object> fields = (SimpleOrderedMap<Object>) response.get("fields");
if (fields != null) {
for (Map.Entry<String, Object> e: fields) fieldsAcc.add(e.getKey(), e.getValue());
}
}
public NamedList<Object> getAccumulatedResponse() {
@ -83,6 +112,8 @@ public class ResponseAccumulator {
responsesAcc.add("response", resultsAcc);
if (highlightingAcc != null && highlightingAcc.size() > 0) responsesAcc.add("highlighting", highlightingAcc);
if (facet_countsAcc != null && facet_countsAcc.size() > 0) responsesAcc.add("facet_counts", facet_countsAcc);
if (index_countsAcc != null && index_countsAcc.size() > 0) responsesAcc.add("index", index_countsAcc);
if (fieldsAcc != null && fieldsAcc.size() > 0) responsesAcc.add("fields", fieldsAcc);
return responsesAcc;
}

Loading…
Cancel
Save