use of org.apache.solr.response.ResultContext in project lucene-solr by apache.
the class XLSXWriter method writeResponse.
public void writeResponse(OutputStream out, LinkedHashMap<String, String> colNamesMap, LinkedHashMap<String, Integer> colWidthsMap) throws IOException {
SolrParams params = req.getParams();
Collection<String> fields = returnFields.getRequestedFieldNames();
Object responseObj = rsp.getValues().get("response");
boolean returnOnlyStored = false;
if (fields == null || returnFields.hasPatternMatching()) {
if (responseObj instanceof SolrDocumentList) {
// get the list of fields from the SolrDocumentList
if (fields == null) {
fields = new LinkedHashSet<String>();
}
for (SolrDocument sdoc : (SolrDocumentList) responseObj) {
fields.addAll(sdoc.getFieldNames());
}
} else {
// get the list of fields from the index
Iterable<String> all = req.getSearcher().getFieldNames();
if (fields == null) {
fields = Sets.newHashSet(all);
} else {
Iterables.addAll(fields, all);
}
}
if (returnFields.wantsScore()) {
fields.add("score");
} else {
fields.remove("score");
}
returnOnlyStored = true;
}
for (String field : fields) {
if (!returnFields.wantsField(field)) {
continue;
}
if (field.equals("score")) {
XLField xlField = new XLField();
xlField.name = "score";
xlFields.put("score", xlField);
continue;
}
SchemaField sf = schema.getFieldOrNull(field);
if (sf == null) {
FieldType ft = new StrField();
sf = new SchemaField(field, ft);
}
// Return only stored fields, unless an explicit field list is specified
if (returnOnlyStored && sf != null && !sf.stored()) {
continue;
}
XLField xlField = new XLField();
xlField.name = field;
xlField.sf = sf;
xlFields.put(field, xlField);
}
wb.addRow();
//write header
for (XLField xlField : xlFields.values()) {
String printName = xlField.name;
int colWidth = 14;
String niceName = colNamesMap.get(xlField.name);
if (niceName != null) {
printName = niceName;
}
Integer niceWidth = colWidthsMap.get(xlField.name);
if (niceWidth != null) {
colWidth = niceWidth.intValue();
}
writeStr(xlField.name, printName, false);
wb.setColWidth(colWidth);
wb.setHeaderCell();
}
wb.setHeaderRow();
wb.addRow();
if (responseObj instanceof ResultContext) {
writeDocuments(null, (ResultContext) responseObj);
} else if (responseObj instanceof DocList) {
ResultContext ctx = new BasicResultContext((DocList) responseObj, returnFields, null, null, req);
writeDocuments(null, ctx);
} else if (responseObj instanceof SolrDocumentList) {
writeSolrDocumentList(null, (SolrDocumentList) responseObj, returnFields);
}
wb.flush(out);
wb = null;
}
use of org.apache.solr.response.ResultContext in project lucene-solr by apache.
the class TestGroupingSearch method testGroupingSimpleFormatArrayIndexOutOfBoundsExceptionWithJavaBin.
@Test
public void testGroupingSimpleFormatArrayIndexOutOfBoundsExceptionWithJavaBin() throws Exception {
assertU(add(doc("id", "1", "nullfirst", "1")));
assertU(add(doc("id", "2", "nullfirst", "1")));
assertU(add(doc("id", "3", "nullfirst", "2")));
assertU(add(doc("id", "4", "nullfirst", "2")));
assertU(add(doc("id", "5", "nullfirst", "2")));
assertU(add(doc("id", "6", "nullfirst", "3")));
assertU(commit());
SolrQueryRequest request = req("q", "*:*", "group", "true", "group.field", "nullfirst", "group.main", "true", "wt", "javabin", "start", "4", "rows", "10");
SolrQueryResponse response = new SolrQueryResponse();
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
SolrRequestInfo.setRequestInfo(new SolrRequestInfo(request, response));
String handlerName = request.getParams().get(CommonParams.QT);
h.getCore().execute(h.getCore().getRequestHandler(handlerName), request, response);
BinaryResponseWriter responseWriter = new BinaryResponseWriter();
responseWriter.write(out, request, response);
} finally {
request.close();
SolrRequestInfo.clearRequestInfo();
}
assertEquals(6, ((ResultContext) response.getResponse()).getDocList().matches());
new BinaryResponseParser().processResponse(new ByteArrayInputStream(out.toByteArray()), "");
out.close();
}
use of org.apache.solr.response.ResultContext in project lucene-solr by apache.
the class BasicFunctionalityTest method testNotLazyField.
@Test
public void testNotLazyField() throws IOException {
assertU(adoc("id", "7777", "title", "keyword", "test_hlt", mkstr(20000)));
assertU(commit());
SolrCore core = h.getCore();
SolrQueryRequest req = req("q", "id:7777", "fl", "id,title,test_hlt");
SolrQueryResponse rsp = new SolrQueryResponse();
core.execute(core.getRequestHandler(req.getParams().get(CommonParams.QT)), req, rsp);
DocList dl = ((ResultContext) rsp.getResponse()).getDocList();
Document d = req.getSearcher().doc(dl.iterator().nextDoc());
// ensure field in fl is not lazy
assertFalse(((Field) d.getField("test_hlt")).getClass().getSimpleName().equals("LazyField"));
assertFalse(((Field) d.getField("title")).getClass().getSimpleName().equals("LazyField"));
req.close();
}
use of org.apache.solr.response.ResultContext in project lucene-solr by apache.
the class BasicFunctionalityTest method testLazyField.
@Test
public void testLazyField() throws IOException {
assertU(adoc("id", "7777", "title", "keyword", "test_hlt", mkstr(10000), "test_hlt", mkstr(20000), "test_hlt", mkstr(30000), "test_hlt", mkstr(40000)));
assertU(commit());
SolrCore core = h.getCore();
// initial request
SolrQueryRequest req = req("q", "id:7777", "fl", "id,title");
SolrQueryResponse rsp = new SolrQueryResponse();
core.execute(core.getRequestHandler(req.getParams().get(CommonParams.QT)), req, rsp);
DocList dl = ((ResultContext) rsp.getResponse()).getDocList();
DocIterator di = dl.iterator();
Document d1 = req.getSearcher().doc(di.nextDoc());
IndexableField[] values1 = null;
// ensure fl field is non lazy, and non-fl field is lazy
assertFalse(d1.getField("title") instanceof LazyDocument.LazyField);
assertFalse(d1.getField("id") instanceof LazyDocument.LazyField);
values1 = d1.getFields("test_hlt");
assertEquals(4, values1.length);
for (int i = 0; i < values1.length; i++) {
assertTrue(values1[i] instanceof LazyDocument.LazyField);
LazyDocument.LazyField f = (LazyDocument.LazyField) values1[i];
assertFalse(f.hasBeenLoaded());
}
req.close();
// followup request, different fl
req = req("q", "id:7777", "fl", "id,test_hlt");
rsp = new SolrQueryResponse();
core.execute(core.getRequestHandler(req.getParams().get(CommonParams.QT)), req, rsp);
dl = ((ResultContext) rsp.getResponse()).getDocList();
di = dl.iterator();
Document d2 = req.getSearcher().doc(di.nextDoc());
// ensure same doc, same lazy field now
assertTrue("Doc was not cached", d1 == d2);
IndexableField[] values2 = d2.getFields("test_hlt");
assertEquals(values1.length, values2.length);
for (int i = 0; i < values1.length; i++) {
assertSame("LazyField wasn't reused", values1[i], values2[i]);
LazyDocument.LazyField f = (LazyDocument.LazyField) values1[i];
// still not a real boy, no response writer in play
assertFalse(f.hasBeenLoaded());
}
// actuallize one value
assertNotNull(values2[0].stringValue());
for (int i = 0; i < values2.length; i++) {
// now all values for this field should be loaded & cached
LazyDocument.LazyField f = (LazyDocument.LazyField) values2[i];
assertTrue(f.hasBeenLoaded());
}
req.close();
}
use of org.apache.solr.response.ResultContext in project SearchServices by Alfresco.
the class Cloud method getResultContext.
/**
* @param requestHandler the handler that handles the request
* @param request the request object to put the params on
* @param params Solr parameters
* @return the result context from the handled request
*/
ResultContext getResultContext(SolrRequestHandler requestHandler, SolrQueryRequest request, SolrParams params) {
SolrQueryResponse solrRsp = getResponse(requestHandler, request, params);
ResultContext rc = (ResultContext) solrRsp.getValues().get("response");
return rc;
}
Aggregations