add AccessTracker logging to SolrServlet

pull/1/head
reger 11 years ago
parent de1f02420b
commit b85f702f22

@ -47,7 +47,10 @@ import net.yacy.cora.federate.solr.responsewriter.GrepHTMLResponseWriter;
import net.yacy.cora.federate.solr.responsewriter.HTMLResponseWriter; import net.yacy.cora.federate.solr.responsewriter.HTMLResponseWriter;
import net.yacy.cora.federate.solr.responsewriter.OpensearchResponseWriter; import net.yacy.cora.federate.solr.responsewriter.OpensearchResponseWriter;
import net.yacy.cora.federate.solr.responsewriter.YJsonResponseWriter; import net.yacy.cora.federate.solr.responsewriter.YJsonResponseWriter;
import net.yacy.cora.util.ConcurrentLog;
import net.yacy.search.query.AccessTracker;
import net.yacy.search.schema.CollectionSchema; import net.yacy.search.schema.CollectionSchema;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrException; import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.CommonParams; import org.apache.solr.common.params.CommonParams;
@ -56,6 +59,7 @@ import org.apache.solr.core.SolrCore;
import org.apache.solr.request.SolrQueryRequest; import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.request.SolrRequestInfo; import org.apache.solr.request.SolrRequestInfo;
import org.apache.solr.response.QueryResponseWriter; import org.apache.solr.response.QueryResponseWriter;
import org.apache.solr.response.ResultContext;
import org.apache.solr.response.SolrQueryResponse; import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.servlet.SolrRequestParsers; import org.apache.solr.servlet.SolrRequestParsers;
import org.apache.solr.servlet.cache.HttpCacheHeaderUtil; import org.apache.solr.servlet.cache.HttpCacheHeaderUtil;
@ -107,7 +111,6 @@ public class SolrServlet implements Filter {
HttpServletRequest hrequest = (HttpServletRequest) request; HttpServletRequest hrequest = (HttpServletRequest) request;
HttpServletResponse hresponse = (HttpServletResponse) response; HttpServletResponse hresponse = (HttpServletResponse) response;
SolrQueryRequest req = null;
// check if this servlet was called correctly // check if this servlet was called correctly
String pathInfo = hrequest.getPathInfo(); String pathInfo = hrequest.getPathInfo();
@ -130,31 +133,31 @@ public class SolrServlet implements Filter {
throw new ServletException("Unsupported method: " + hrequest.getMethod()); throw new ServletException("Unsupported method: " + hrequest.getMethod());
} }
try {
SolrCore core = connector.getCore(); SolrCore core = connector.getCore();
if (core == null) { if (core == null) {
throw new UnsupportedOperationException("core not initialized"); hresponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "core not initialized");
return;
} }
// prepare request to solr // prepare request to solr
hrequest.setAttribute("org.apache.solr.CoreContainer", core); hrequest.setAttribute("org.apache.solr.CoreContainer", core);
// add default search field if missing // add default search field if missing (required by edismax)
String queryStr = hrequest.getQueryString(); String queryStr = hrequest.getQueryString();
if (!queryStr.contains("&df=")) { if (!queryStr.contains("&df=")) {
queryStr = queryStr + "&df=" + CollectionSchema.text_t.getSolrFieldName(); queryStr = queryStr + "&df=" + CollectionSchema.text_t.getSolrFieldName();
} }
MultiMapSolrParams mmsp = SolrRequestParsers.parseQueryString(queryStr); MultiMapSolrParams mmsp = SolrRequestParsers.parseQueryString(queryStr);
String wt = mmsp.get(CommonParams.WT, "xml"); // maybe use /solr/select?q=*:*&start=0&rows=10&wt=exml String wt = mmsp.get(CommonParams.WT, "xml"); // maybe use /solr/select?q=*:*&start=0&rows=10&wt=exml
QueryResponseWriter responseWriter = RESPONSE_WRITER.get(wt); QueryResponseWriter responseWriter = RESPONSE_WRITER.get(wt); // check local response writer
if (responseWriter == null) { if (responseWriter == null) {
// check default response writer
responseWriter = SolrCore.DEFAULT_RESPONSE_WRITERS.get(wt); responseWriter = SolrCore.DEFAULT_RESPONSE_WRITERS.get(wt);
if (responseWriter == null) { if (responseWriter == null) {
hresponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"Solr responsewriter not found for " + wt); hresponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Solr responsewriter not found for " + wt);
return; return;
} }
} }
req = connector.request(mmsp); SolrQueryRequest req = connector.request(mmsp);
SolrQueryResponse rsp = connector.query(req); SolrQueryResponse rsp = connector.query(req);
// prepare response // prepare response
@ -176,18 +179,24 @@ public class SolrServlet implements Filter {
} }
// write response body // write response body
Writer out = new FastWriter(new OutputStreamWriter(response.getOutputStream(),UTF8.charset)); Writer out = new FastWriter(new OutputStreamWriter(response.getOutputStream(), UTF8.charset));
responseWriter.write(out, req, rsp); responseWriter.write(out, req, rsp);
out.flush(); out.flush();
} catch (final Throwable ex) {
sendError(hresponse, ex); // log result
} finally { Object rv = rsp.getValues().get("response");
if (req != null) { int matches = 0;
req.close(); if (rv != null && rv instanceof ResultContext) {
matches = ((ResultContext) rv).docs.matches();
} else if (rv != null && rv instanceof SolrDocumentList) {
matches = (int) ((SolrDocumentList) rv).getNumFound();
} }
AccessTracker.addToDump(mmsp.get("q"), Integer.toString(matches));
ConcurrentLog.info("SOLR Query", "results: " + matches + ", for query:" + req.getParamString());
req.close();
SolrRequestInfo.clearRequestInfo(); SolrRequestInfo.clearRequestInfo();
} }
}
private static void sendError(HttpServletResponse hresponse, Throwable ex) throws IOException { private static void sendError(HttpServletResponse hresponse, Throwable ex) throws IOException {
int code = (ex instanceof SolrException) ? ((SolrException) ex).code() : 500; int code = (ex instanceof SolrException) ? ((SolrException) ex).code() : 500;

Loading…
Cancel
Save