Apache Lucene open-source search software
SOLR-3825: added in optional logging of documents in a response
Grant Ingersollb7297c7f8dfc
diff --git a/solr/core/src/java/org/apache/solr/handler/component/ResponseLogComponent.java b/solr/core/src/java/org/apache/solr/handler/component/ResponseLogComponent.javanew file mode 100644index 00000000000..b3c36627282--- /dev/null+++ b/solr/core/src/java/org/apache/solr/handler/component/ResponseLogComponent.java@@ -0,0 +1,107 @@+package org.apache.solr.handler.component;+/*+ * 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 java.io.IOException;++import org.apache.solr.common.params.SolrParams;+import org.apache.solr.response.ResultContext;+import org.apache.solr.search.DocIterator;+import org.apache.solr.search.DocList;++/**+ * Adds to the log file the document IDs that are sent in the query response.+ * If document scores are available in the response (by adding the pseudo-+ * column 'score' to the field list) then each document ID will be followed+ * by its score, as in:+ * <pre>+ * "... hits=55 responseLog=22:0.71231794,44:0.61231794 status=0 ..."+ * </pre>+ *+ * Add it to a requestHandler in solrconfig.xml like this:+ * <pre class="prettyprint">+ * <searchComponent name="responselog" class="solr.ResponseLogComponent"/>+ *+ * <requestHandler name="/select" class="solr.SearchHandler">+ * <lst name="defaults">+ *+ * ...+ *+ * </lst>+ * <arr name="components">+ * <str>responselog</str>+ * </arr>+ * </requestHandler></pre>+ *+ * It can then be enabled at query time by supplying <pre>responseLog=true</pre>+ * query parameter.+ */+public class ResponseLogComponent extends SearchComponent {++ public static final String COMPONENT_NAME = "responseLog";++ @Override+ public void prepare(ResponseBuilder rb) throws IOException {}++ @Override+ public void process(ResponseBuilder rb) throws IOException {+ SolrParams params = rb.req.getParams();+ if (!params.getBool(COMPONENT_NAME, false)) return;++ ResultContext rc = (ResultContext) rb.rsp.getValues().get("response");+ if (rc.docs.hasScores()) {+ processScores(rb, rc.docs);+ } else {+ processIds(rb, rc.docs);+ }+ }++ protected void processIds(ResponseBuilder rb, DocList dl) {+ StringBuilder sb = new StringBuilder();+ for(DocIterator iter = dl.iterator(); iter.hasNext();) {+ sb.append(iter.nextDoc())+ .append(',');+ }+ if (sb.length() > 0) {+ rb.rsp.addToLog("responseLog", sb.substring(0, sb.length() - 1));+ }+ }++ protected void processScores(ResponseBuilder rb, DocList dl) {+ StringBuilder sb = new StringBuilder();+ for(DocIterator iter = dl.iterator(); iter.hasNext();) {+ sb.append(iter.nextDoc())+ .append(':')+ .append(iter.score())+ .append(',');+ }+ if (sb.length() > 0) {+ rb.rsp.addToLog("responseLog", sb.substring(0, sb.length() - 1));+ }+ }++ @Override+ public String getDescription() {+ return "A component that inserts the retrieved documents (and optionally scores) into the response log entry";+ }++ @Override+ public String getSource() {+ return "$URL$";+ }++}\ No newline at end of filediff --git a/solr/core/src/test-files/solr/collection1/conf/solrconfig-response-log-component.xml b/solr/core/src/test-files/solr/collection1/conf/solrconfig-response-log-component.xmlnew file mode 100644index 00000000000..d4c623aa060--- /dev/null+++ b/solr/core/src/test-files/solr/collection1/conf/solrconfig-response-log-component.xml@@ -0,0 +1,60 @@+<?xml version="1.0" ?>++<!--+ 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.+-->++<!-- $Id: $+ $Source: $+ $Name: $++Config for testing responselog component+ -->++<config>+ <luceneMatchVersion>${tests.luceneMatchVersion:LUCENE_CURRENT}</luceneMatchVersion>+ <requestHandler name="standard"+ class="solr.StandardRequestHandler"></requestHandler>++ <!-- Used to specify an alternate directory to hold all index data.+ It defaults to "index" if not present, and should probably+ not be changed if replication is in use. -->+ <dataDir>${solr.data.dir:}</dataDir>++ <!-- The DirectoryFactory to use for indexes.+ solr.StandardDirectoryFactory, the default, is filesystem based.+ solr.RAMDirectoryFactory is memory based and not persistent. -->+ <directoryFactory name="DirectoryFactory" class="${solr.directoryFactory:solr.RAMDirectoryFactory}"/>++ <searchComponent name="responselog" class="org.apache.solr.handler.component.ResponseLogComponent" />++ <!-- Log retrievedDocs -->+ <requestHandler name="withlog" class="org.apache.solr.handler.component.SearchHandler">+ <lst name="defaults">+ <str name="defType">dismax</str>+ </lst>+ <arr name="last-components">+ <str>responselog</str>+ </arr>+ </requestHandler>++ <requestHandler name="nolog" class="org.apache.solr.handler.component.SearchHandler">+ <lst name="defaults">+ <str name="defType">dismax</str>+ </lst>+ </requestHandler>++</config>diff --git a/solr/core/src/test/org/apache/solr/handler/component/ResponseLogComponentTest.java b/solr/core/src/test/org/apache/solr/handler/component/ResponseLogComponentTest.javanew file mode 100644index 00000000000..b78e29516e4--- /dev/null+++ b/solr/core/src/test/org/apache/solr/handler/component/ResponseLogComponentTest.java@@ -0,0 +1,93 @@+package org.apache.solr.handler.component;++import org.apache.solr.SolrTestCaseJ4;+import org.apache.solr.common.util.NamedList;+import org.apache.solr.request.SolrQueryRequest;+import org.apache.solr.response.SolrQueryResponse;+import org.junit.BeforeClass;+import org.junit.Test;++/*+ * 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.+ */++public class ResponseLogComponentTest extends SolrTestCaseJ4 {++ @BeforeClass+ public static void beforeTest() throws Exception {+ initCore("solrconfig-response-log-component.xml","schema12.xml");+ assertNull(h.validateUpdate(adoc("id", "1", "subject", "aa")));+ assertNull(h.validateUpdate(adoc("id", "2", "subject", "aa")));+ assertNull(h.validateUpdate(adoc("id", "3", "subject", "aa")));+ assertU(commit());+ }++ @Test+ public void testToLogIds() throws Exception {+ SolrQueryRequest req = null;+ try {+ String handler="withlog";+ req = req("indent","true", "qt","withlog", "q","aa", "rows","2",+ "fl","id,subject", "responseLog","true");+ SolrQueryResponse qr = h.queryAndResponse(handler, req);+ NamedList<Object> entries = qr.getToLog();+ String responseLog = (String) entries.get("responseLog");+ assertNotNull(responseLog);+ assertTrue(responseLog.matches("\\d+,\\d+"));+ } finally {+ if (req != null) {+ req.close();+ }+ }+ }++ @Test+ public void testToLogScores() throws Exception {+ SolrQueryRequest req = null;+ try {+ String handler="withlog";+ req = req("indent","true", "qt","withlog", "q","aa", "rows","2",+ "fl","id,subject,score", "responseLog","true");+ SolrQueryResponse qr = h.queryAndResponse(handler, req);+ NamedList<Object> entries = qr.getToLog();+ String responseLog = (String) entries.get("responseLog");+ assertNotNull(responseLog);+ assertTrue(responseLog.matches("\\d+:\\d+\\.\\d+,\\d+:\\d+\\.\\d+"));+ } finally {+ if (req != null) {+ req.close();+ }+ }+ }++ @Test+ public void testDisabling() throws Exception {+ SolrQueryRequest req = null;+ try {+ String handler="withlog";+ req = req("indent","true", "qt","withlog", "q","aa", "rows","2",+ "fl","id,subject", "responseLog","false");+ SolrQueryResponse qr = h.queryAndResponse(handler, req);+ NamedList<Object> entries = qr.getToLog();+ String responseLog = (String) entries.get("responseLog");+ assertNull(responseLog);+ } finally {+ if (req != null) {+ req.close();+ }+ }+ }+}