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.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.common.params.SolrParams;
|
||||||
import org.apache.solr.response.ResultContext;
|
import org.apache.solr.response.ResultContext;
|
||||||
|
import org.apache.solr.schema.IndexSchema;
|
||||||
import org.apache.solr.search.DocIterator;
|
import org.apache.solr.search.DocIterator;
|
||||||
import org.apache.solr.search.DocList;
|
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.
|
* Adds to the log file the document IDs that are sent in the query response.
|
||||||
|
@ -62,18 +67,28 @@ public class ResponseLogComponent extends SearchComponent {
|
||||||
SolrParams params = rb.req.getParams();
|
SolrParams params = rb.req.getParams();
|
||||||
if (!params.getBool(COMPONENT_NAME, false)) return;
|
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");
|
ResultContext rc = (ResultContext) rb.rsp.getValues().get("response");
|
||||||
|
SolrIndexSearcher searcher = rb.req.getSearcher();
|
||||||
|
|
||||||
if (rc.docs.hasScores()) {
|
if (rc.docs.hasScores()) {
|
||||||
processScores(rb, rc.docs);
|
processScores(rb, rc.docs, schema, searcher);
|
||||||
} else {
|
} 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();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
|
Set<String> fields = Collections.singleton(schema.getUniqueKeyField().getName());
|
||||||
for(DocIterator iter = dl.iterator(); iter.hasNext();) {
|
for(DocIterator iter = dl.iterator(); iter.hasNext();) {
|
||||||
sb.append(iter.nextDoc())
|
|
||||||
|
sb.append(schema.printableUniqueKey(searcher.doc(iter.nextDoc(), fields)))
|
||||||
.append(',');
|
.append(',');
|
||||||
}
|
}
|
||||||
if (sb.length() > 0) {
|
if (sb.length() > 0) {
|
||||||
|
@ -81,10 +96,13 @@ public class ResponseLogComponent extends SearchComponent {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void processScores(ResponseBuilder rb, DocList dl) {
|
protected void processScores(ResponseBuilder rb, DocList dl, IndexSchema schema,
|
||||||
|
SolrIndexSearcher searcher) throws IOException {
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
Set<String> fields = Collections.singleton(schema.getUniqueKeyField().getName());
|
||||||
for(DocIterator iter = dl.iterator(); iter.hasNext();) {
|
for(DocIterator iter = dl.iterator(); iter.hasNext();) {
|
||||||
sb.append(iter.nextDoc())
|
sb.append(schema.printableUniqueKey(searcher.doc(iter.nextDoc(), fields)))
|
||||||
.append(':')
|
.append(':')
|
||||||
.append(iter.score())
|
.append(iter.score())
|
||||||
.append(',');
|
.append(',');
|
||||||
|
|
|
@ -30,7 +30,7 @@ public class ResponseLogComponentTest extends SolrTestCaseJ4 {
|
||||||
public static void beforeTest() throws Exception {
|
public static void beforeTest() throws Exception {
|
||||||
initCore("solrconfig-response-log-component.xml","schema12.xml");
|
initCore("solrconfig-response-log-component.xml","schema12.xml");
|
||||||
assertNull(h.validateUpdate(adoc("id", "1", "subject", "aa")));
|
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")));
|
assertNull(h.validateUpdate(adoc("id", "3", "subject", "aa")));
|
||||||
assertU(commit());
|
assertU(commit());
|
||||||
}
|
}
|
||||||
|
@ -46,13 +46,11 @@ public class ResponseLogComponentTest extends SolrTestCaseJ4 {
|
||||||
NamedList<Object> entries = qr.getToLog();
|
NamedList<Object> entries = qr.getToLog();
|
||||||
String responseLog = (String) entries.get("responseLog");
|
String responseLog = (String) entries.get("responseLog");
|
||||||
assertNotNull(responseLog);
|
assertNotNull(responseLog);
|
||||||
assertTrue(responseLog.matches("\\d+,\\d+"));
|
assertTrue(responseLog.matches("\\w+,\\w+"));
|
||||||
} finally {
|
} finally {
|
||||||
if (req != null) {
|
|
||||||
req.close();
|
req.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testToLogScores() throws Exception {
|
public void testToLogScores() throws Exception {
|
||||||
|
@ -65,13 +63,11 @@ public class ResponseLogComponentTest extends SolrTestCaseJ4 {
|
||||||
NamedList<Object> entries = qr.getToLog();
|
NamedList<Object> entries = qr.getToLog();
|
||||||
String responseLog = (String) entries.get("responseLog");
|
String responseLog = (String) entries.get("responseLog");
|
||||||
assertNotNull(responseLog);
|
assertNotNull(responseLog);
|
||||||
assertTrue(responseLog.matches("\\d+:\\d+\\.\\d+,\\d+:\\d+\\.\\d+"));
|
assertTrue(responseLog.matches("\\w+:\\d+\\.\\d+,\\w+:\\d+\\.\\d+"));
|
||||||
} finally {
|
} finally {
|
||||||
if (req != null) {
|
|
||||||
req.close();
|
req.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDisabling() throws Exception {
|
public void testDisabling() throws Exception {
|
||||||
|
@ -85,9 +81,7 @@ public class ResponseLogComponentTest extends SolrTestCaseJ4 {
|
||||||
String responseLog = (String) entries.get("responseLog");
|
String responseLog = (String) entries.get("responseLog");
|
||||||
assertNull(responseLog);
|
assertNull(responseLog);
|
||||||
} finally {
|
} finally {
|
||||||
if (req != null) {
|
|
||||||
req.close();
|
req.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue