mirror of https://github.com/apache/lucene.git
SOLR-3825: use unique key
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1387259 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
88adeb0b28
commit
4577c1696c
|
@ -17,11 +17,16 @@ package org.apache.solr.handler.component;
|
|||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.lucene.index.StoredFieldVisitor;
|
||||
import org.apache.solr.common.params.SolrParams;
|
||||
import org.apache.solr.response.ResultContext;
|
||||
import org.apache.solr.schema.IndexSchema;
|
||||
import org.apache.solr.search.DocIterator;
|
||||
import org.apache.solr.search.DocList;
|
||||
import org.apache.solr.search.SolrIndexSearcher;
|
||||
|
||||
/**
|
||||
* Adds to the log file the document IDs that are sent in the query response.
|
||||
|
@ -62,32 +67,45 @@ public class ResponseLogComponent extends SearchComponent {
|
|||
SolrParams params = rb.req.getParams();
|
||||
if (!params.getBool(COMPONENT_NAME, false)) return;
|
||||
|
||||
IndexSchema schema = rb.req.getSchema();
|
||||
if (schema.getUniqueKeyField() == null) return;
|
||||
|
||||
ResultContext rc = (ResultContext) rb.rsp.getValues().get("response");
|
||||
SolrIndexSearcher searcher = rb.req.getSearcher();
|
||||
|
||||
if (rc.docs.hasScores()) {
|
||||
processScores(rb, rc.docs);
|
||||
processScores(rb, rc.docs, schema, searcher);
|
||||
} else {
|
||||
processIds(rb, rc.docs);
|
||||
processIds(rb, rc.docs, schema, searcher);
|
||||
}
|
||||
}
|
||||
|
||||
protected void processIds(ResponseBuilder rb, DocList dl) {
|
||||
protected void processIds(ResponseBuilder rb, DocList dl, IndexSchema schema,
|
||||
SolrIndexSearcher searcher) throws IOException {
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
Set<String> fields = Collections.singleton(schema.getUniqueKeyField().getName());
|
||||
for(DocIterator iter = dl.iterator(); iter.hasNext();) {
|
||||
sb.append(iter.nextDoc())
|
||||
.append(',');
|
||||
|
||||
sb.append(schema.printableUniqueKey(searcher.doc(iter.nextDoc(), fields)))
|
||||
.append(',');
|
||||
}
|
||||
if (sb.length() > 0) {
|
||||
rb.rsp.addToLog("responseLog", sb.substring(0, sb.length() - 1));
|
||||
}
|
||||
}
|
||||
|
||||
protected void processScores(ResponseBuilder rb, DocList dl) {
|
||||
protected void processScores(ResponseBuilder rb, DocList dl, IndexSchema schema,
|
||||
SolrIndexSearcher searcher) throws IOException {
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Set<String> fields = Collections.singleton(schema.getUniqueKeyField().getName());
|
||||
for(DocIterator iter = dl.iterator(); iter.hasNext();) {
|
||||
sb.append(iter.nextDoc())
|
||||
.append(':')
|
||||
.append(iter.score())
|
||||
.append(',');
|
||||
sb.append(schema.printableUniqueKey(searcher.doc(iter.nextDoc(), fields)))
|
||||
.append(':')
|
||||
.append(iter.score())
|
||||
.append(',');
|
||||
}
|
||||
if (sb.length() > 0) {
|
||||
rb.rsp.addToLog("responseLog", sb.substring(0, sb.length() - 1));
|
||||
|
@ -104,4 +122,4 @@ public class ResponseLogComponent extends SearchComponent {
|
|||
return "$URL$";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ public class ResponseLogComponentTest extends SolrTestCaseJ4 {
|
|||
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", "two", "subject", "aa")));
|
||||
assertNull(h.validateUpdate(adoc("id", "3", "subject", "aa")));
|
||||
assertU(commit());
|
||||
}
|
||||
|
@ -46,11 +46,9 @@ public class ResponseLogComponentTest extends SolrTestCaseJ4 {
|
|||
NamedList<Object> entries = qr.getToLog();
|
||||
String responseLog = (String) entries.get("responseLog");
|
||||
assertNotNull(responseLog);
|
||||
assertTrue(responseLog.matches("\\d+,\\d+"));
|
||||
assertTrue(responseLog.matches("\\w+,\\w+"));
|
||||
} finally {
|
||||
if (req != null) {
|
||||
req.close();
|
||||
}
|
||||
req.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -65,11 +63,9 @@ public class ResponseLogComponentTest extends SolrTestCaseJ4 {
|
|||
NamedList<Object> entries = qr.getToLog();
|
||||
String responseLog = (String) entries.get("responseLog");
|
||||
assertNotNull(responseLog);
|
||||
assertTrue(responseLog.matches("\\d+:\\d+\\.\\d+,\\d+:\\d+\\.\\d+"));
|
||||
assertTrue(responseLog.matches("\\w+:\\d+\\.\\d+,\\w+:\\d+\\.\\d+"));
|
||||
} finally {
|
||||
if (req != null) {
|
||||
req.close();
|
||||
}
|
||||
req.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -85,9 +81,7 @@ public class ResponseLogComponentTest extends SolrTestCaseJ4 {
|
|||
String responseLog = (String) entries.get("responseLog");
|
||||
assertNull(responseLog);
|
||||
} finally {
|
||||
if (req != null) {
|
||||
req.close();
|
||||
}
|
||||
req.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue