use of org.apache.solr.response.QueryResponseWriter in project lucene-solr by apache.
the class TestPHPSerializedResponseWriter method testSimple.
@Test
public void testSimple() throws IOException {
SolrQueryRequest req = req("dummy");
SolrQueryResponse rsp = new SolrQueryResponse();
QueryResponseWriter w = new PHPSerializedResponseWriter();
StringWriter buf = new StringWriter();
rsp.add("data1", "hello");
rsp.add("data2", 42);
rsp.add("data3", true);
w.write(buf, req, rsp);
assertEquals("a:3:{s:5:\"data1\";s:5:\"hello\";s:5:\"data2\";i:42;s:5:\"data3\";b:1;}", buf.toString());
req.close();
}
use of org.apache.solr.response.QueryResponseWriter in project lucene-solr by apache.
the class XsltUpdateRequestHandlerTest method testUpdate.
@Test
public void testUpdate() throws Exception {
String xml = "<random>" + " <document>" + " <node name=\"id\" value=\"12345\"/>" + " <node name=\"name\" value=\"kitten\"/>" + " <node name=\"text\" enhance=\"3\" value=\"some other day\"/>" + " <node name=\"title\" enhance=\"4\" value=\"A story\"/>" + " <node name=\"timestamp\" enhance=\"5\" value=\"2011-07-01T10:31:57.140Z\"/>" + " </document>" + "</random>";
Map<String, String> args = new HashMap<>();
args.put(CommonParams.TR, "xsl-update-handler-test.xsl");
SolrCore core = h.getCore();
LocalSolrQueryRequest req = new LocalSolrQueryRequest(core, new MapSolrParams(args));
ArrayList<ContentStream> streams = new ArrayList<>();
streams.add(new ContentStreamBase.StringStream(xml));
req.setContentStreams(streams);
SolrQueryResponse rsp = new SolrQueryResponse();
UpdateRequestHandler handler = new UpdateRequestHandler();
handler.init(new NamedList<String>());
handler.handleRequestBody(req, rsp);
StringWriter sw = new StringWriter(32000);
QueryResponseWriter responseWriter = core.getQueryResponseWriter(req);
responseWriter.write(sw, req, rsp);
req.close();
String response = sw.toString();
assertU(response);
assertU(commit());
assertQ("test document was correctly committed", req("q", "*:*"), "//result[@numFound='1']", "//int[@name='id'][.='12345']");
}
use of org.apache.solr.response.QueryResponseWriter in project lucene-solr by apache.
the class ExportWriter method write.
public void write(OutputStream os) throws IOException {
QueryResponseWriter rw = req.getCore().getResponseWriters().get(wt);
if (rw instanceof BinaryResponseWriter) {
//todo add support for other writers after testing
writer = new JavaBinCodec(os, null);
} else {
respWriter = new OutputStreamWriter(os, StandardCharsets.UTF_8);
writer = JSONResponseWriter.getPushWriter(respWriter, req, res);
}
Exception exception = res.getException();
if (exception != null) {
if (!(exception instanceof IgnoreException)) {
writeException(exception, writer, false);
}
return;
}
SolrRequestInfo info = SolrRequestInfo.getRequestInfo();
SortSpec sortSpec = info.getResponseBuilder().getSortSpec();
if (sortSpec == null) {
writeException((new IOException(new SyntaxError("No sort criteria was provided."))), writer, true);
return;
}
SolrIndexSearcher searcher = req.getSearcher();
Sort sort = searcher.weightSort(sortSpec.getSort());
if (sort == null) {
writeException((new IOException(new SyntaxError("No sort criteria was provided."))), writer, true);
return;
}
if (sort != null && sort.needsScores()) {
writeException((new IOException(new SyntaxError("Scoring is not currently supported with xsort."))), writer, true);
return;
}
// This came to light in the very artifical case of indexing a single doc to Cloud.
if (req.getContext().get("totalHits") != null) {
totalHits = ((Integer) req.getContext().get("totalHits")).intValue();
sets = (FixedBitSet[]) req.getContext().get("export");
if (sets == null) {
writeException((new IOException(new SyntaxError("xport RankQuery is required for xsort: rq={!xport}"))), writer, true);
return;
}
}
SolrParams params = req.getParams();
String fl = params.get("fl");
String[] fields = null;
if (fl == null) {
writeException((new IOException(new SyntaxError("export field list (fl) must be specified."))), writer, true);
return;
} else {
fields = fl.split(",");
for (int i = 0; i < fields.length; i++) {
fields[i] = fields[i].trim();
if (fields[i].equals("score")) {
writeException((new IOException(new SyntaxError("Scoring is not currently supported with xsort."))), writer, true);
return;
}
}
}
try {
fieldWriters = getFieldWriters(fields, req.getSearcher());
} catch (Exception e) {
writeException(e, writer, true);
return;
}
writer.writeMap(m -> {
m.put("responseHeader", singletonMap("status", 0));
m.put("response", (MapWriter) mw -> {
mw.put("numFound", totalHits);
mw.put("docs", (IteratorWriter) iw -> writeDocs(req, iw, sort));
});
});
}
use of org.apache.solr.response.QueryResponseWriter in project lucene-solr by apache.
the class DirectSolrConnection method request.
public String request(SolrRequestHandler handler, SolrParams params, String body) throws Exception {
if (params == null)
params = new MapSolrParams(new HashMap<String, String>());
// Make a stream for the 'body' content
List<ContentStream> streams = new ArrayList<>(1);
if (body != null && body.length() > 0) {
streams.add(new ContentStreamBase.StringStream(body));
}
SolrQueryRequest req = null;
try {
req = parser.buildRequestFrom(core, params, streams);
SolrQueryResponse rsp = new SolrQueryResponse();
SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));
core.execute(handler, req, rsp);
if (rsp.getException() != null) {
throw rsp.getException();
}
// Now write it out
QueryResponseWriter responseWriter = core.getQueryResponseWriter(req);
StringWriter out = new StringWriter();
responseWriter.write(out, req, rsp);
return out.toString();
} finally {
if (req != null) {
req.close();
}
SolrRequestInfo.clearRequestInfo();
}
}
use of org.apache.solr.response.QueryResponseWriter in project lucene-solr by apache.
the class RawValueTransformerFactory method create.
@Override
public DocTransformer create(String display, SolrParams params, SolrQueryRequest req) {
String field = params.get("f");
if (Strings.isNullOrEmpty(field)) {
field = display;
}
// When a 'wt' is specified in the transformer, only apply it to the same wt
boolean apply = true;
if (applyToWT != null) {
String qwt = req.getParams().get(CommonParams.WT);
if (qwt == null) {
QueryResponseWriter qw = req.getCore().getQueryResponseWriter(req);
QueryResponseWriter dw = req.getCore().getQueryResponseWriter(applyToWT);
if (qw != dw) {
apply = false;
}
} else {
apply = applyToWT.equals(qwt);
}
}
if (apply) {
return new RawTransformer(field, display);
}
if (field.equals(display)) {
// nothing
return null;
}
return new RenameFieldTransformer(field, display, false);
}
Aggregations