use of org.apache.solr.response.QueryResponseWriter in project lucene-solr by apache.
the class TestHarness method query.
/**
* Processes a "query" using a user constructed SolrQueryRequest, and closes the request at the end.
*
* @param handler the name of the request handler to process the request
* @param req the Query to process, will be closed.
* @return The XML response to the query
* @exception Exception any exception in the response.
* @exception IOException if there is a problem writing the XML
* @see LocalSolrQueryRequest
*/
public String query(String handler, SolrQueryRequest req) throws Exception {
try {
SolrCore core = req.getCore();
SolrQueryResponse rsp = new SolrQueryResponse();
SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));
core.execute(core.getRequestHandler(handler), req, rsp);
if (rsp.getException() != null) {
throw rsp.getException();
}
QueryResponseWriter responseWriter = core.getQueryResponseWriter(req);
if (responseWriter instanceof BinaryQueryResponseWriter) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(32000);
BinaryQueryResponseWriter writer = (BinaryQueryResponseWriter) responseWriter;
writer.write(byteArrayOutputStream, req, rsp);
return new String(byteArrayOutputStream.toByteArray(), "UTF-8");
} else {
StringWriter sw = new StringWriter(32000);
responseWriter.write(sw, req, rsp);
return sw.toString();
}
} finally {
req.close();
SolrRequestInfo.clearRequestInfo();
}
}
use of org.apache.solr.response.QueryResponseWriter in project lucene-solr by apache.
the class TestPHPSerializedResponseWriter method testSolrDocuments.
@Test
public void testSolrDocuments() throws IOException {
SolrQueryRequest req = req("q", "*:*");
SolrQueryResponse rsp = new SolrQueryResponse();
QueryResponseWriter w = new PHPSerializedResponseWriter();
StringWriter buf = new StringWriter();
SolrDocument d = new SolrDocument();
SolrDocument d1 = d;
d.addField("id", "1");
d.addField("data1", "hello");
d.addField("data2", 42);
d.addField("data3", true);
// multivalued fields:
// extremely odd edge case: value is a map
// we use LinkedHashMap because we are doing a string comparison
// later and we need predictible ordering
LinkedHashMap<String, String> nl = new LinkedHashMap<>();
nl.put("data4.1", "hashmap");
nl.put("data4.2", "hello");
d.addField("data4", nl);
// array value
d.addField("data5", Arrays.asList("data5.1", "data5.2", "data5.3"));
// adding one more document to test array indexes
d = new SolrDocument();
SolrDocument d2 = d;
d.addField("id", "2");
SolrDocumentList sdl = new SolrDocumentList();
sdl.add(d1);
sdl.add(d2);
rsp.addResponse(sdl);
w.write(buf, req, rsp);
assertEquals("a:1:{s:8:\"response\";a:3:{s:8:\"numFound\";i:0;s:5:\"start\";i:0;s:4:\"docs\";a:2:{i:0;a:6:{s:2:\"id\";s:1:\"1\";s:5:\"data1\";s:5:\"hello\";s:5:\"data2\";i:42;s:5:\"data3\";b:1;s:5:\"data4\";a:2:{s:7:\"data4.1\";s:7:\"hashmap\";s:7:\"data4.2\";s:5:\"hello\";}s:5:\"data5\";a:3:{i:0;s:7:\"data5.1\";i:1;s:7:\"data5.2\";i:2;s:7:\"data5.3\";}}i:1;a:1:{s:2:\"id\";s:1:\"2\";}}}}", buf.toString());
req.close();
}
use of org.apache.solr.response.QueryResponseWriter in project lucene-solr by apache.
the class GeoFieldUpdater method create.
@Override
public DocTransformer create(String display, SolrParams params, SolrQueryRequest req) {
String fname = params.get("f", display);
if (fname.startsWith("[") && fname.endsWith("]")) {
fname = display.substring(1, display.length() - 1);
}
SchemaField sf = req.getSchema().getFieldOrNull(fname);
if (sf == null) {
throw new SolrException(ErrorCode.BAD_REQUEST, this.getClass().getSimpleName() + " using unknown field: " + fname);
}
if (!(sf.getType() instanceof AbstractSpatialFieldType)) {
throw new SolrException(ErrorCode.BAD_REQUEST, "GeoTransformer requested non-spatial field: " + fname + " (" + sf.getType().getClass().getSimpleName() + ")");
}
final GeoFieldUpdater updater = new GeoFieldUpdater();
updater.field = fname;
updater.display = display;
updater.display_error = display + "_error";
ValueSource shapes = null;
AbstractSpatialFieldType<?> sdv = (AbstractSpatialFieldType<?>) sf.getType();
SpatialStrategy strategy = sdv.getStrategy(fname);
if (strategy instanceof CompositeSpatialStrategy) {
shapes = ((CompositeSpatialStrategy) strategy).getGeometryStrategy().makeShapeValueSource();
} else if (strategy instanceof SerializedDVStrategy) {
shapes = ((SerializedDVStrategy) strategy).makeShapeValueSource();
}
String writerName = params.get("w", "GeoJSON");
updater.formats = strategy.getSpatialContext().getFormats();
updater.writer = updater.formats.getWriter(writerName);
if (updater.writer == null) {
StringBuilder str = new StringBuilder();
str.append("Unknown Spatial Writer: ").append(writerName);
str.append(" [");
for (ShapeWriter w : updater.formats.getWriters()) {
str.append(w.getFormatName()).append(' ');
}
str.append("]");
throw new SolrException(ErrorCode.BAD_REQUEST, str.toString());
}
QueryResponseWriter qw = req.getCore().getQueryResponseWriter(req);
updater.isJSON = (qw.getClass() == JSONResponseWriter.class) && (updater.writer instanceof GeoJSONWriter);
// Using ValueSource
if (shapes != null) {
// we don't really need the qparser... just so we can reuse valueSource
QParser parser = new QParser(null, null, params, req) {
@Override
public Query parse() throws SyntaxError {
return new MatchAllDocsQuery();
}
};
return new ValueSourceAugmenter(display, parser, shapes) {
@Override
protected void setValue(SolrDocument doc, Object val) {
updater.setValue(doc, val);
}
};
}
// Using the raw stored values
return new DocTransformer() {
@Override
public void transform(SolrDocument doc, int docid, float score) throws IOException {
Object val = doc.remove(updater.field);
if (val != null) {
updater.setValue(doc, val);
}
}
@Override
public String getName() {
return updater.display;
}
@Override
public String[] getExtraRequestFields() {
return new String[] { updater.field };
}
};
}
use of org.apache.solr.response.QueryResponseWriter in project lucene-solr by apache.
the class TestXLSXResponseWriter method beforeClass.
@BeforeClass
public static void beforeClass() throws Exception {
System.setProperty("enable.update.log", "false");
initCore("solrconfig.xml", "schema.xml", getFile("extraction/solr").getAbsolutePath());
createIndex();
//find a reference to the default response writer so we can redirect its output later
SolrCore testCore = h.getCore();
QueryResponseWriter writer = testCore.getQueryResponseWriter("xlsx");
if (writer instanceof XLSXResponseWriter) {
writerXlsx = (XLSXResponseWriter) testCore.getQueryResponseWriter("xlsx");
} else {
throw new Exception("XLSXResponseWriter not registered with solr core");
}
}
use of org.apache.solr.response.QueryResponseWriter in project lucene-solr by apache.
the class HttpSolrCall method handleAdminRequest.
private void handleAdminRequest() throws IOException {
SolrQueryResponse solrResp = new SolrQueryResponse();
SolrCore.preDecorateResponse(solrReq, solrResp);
handleAdmin(solrResp);
SolrCore.postDecorateResponse(handler, solrReq, solrResp);
if (log.isInfoEnabled() && solrResp.getToLog().size() > 0) {
log.info(solrResp.getToLogAsString("[admin]"));
}
QueryResponseWriter respWriter = SolrCore.DEFAULT_RESPONSE_WRITERS.get(solrReq.getParams().get(CommonParams.WT));
if (respWriter == null)
respWriter = getResponseWriter();
writeResponse(solrResp, respWriter, Method.getMethod(req.getMethod()));
}
Aggregations