private void writeResponse(SolrQueryResponse solrRsp, ServletResponse response, QueryResponseWriter responseWriter, SolrQueryRequest solrReq, Method reqMethod) throws IOException { if (solrRsp.getException() != null) { sendError((HttpServletResponse) response, solrRsp.getException()); } else { // Now write it out response.setContentType(responseWriter.getContentType(solrReq, solrRsp)); if (Method.HEAD != reqMethod) { if (responseWriter instanceof BinaryQueryResponseWriter) { BinaryQueryResponseWriter binWriter = (BinaryQueryResponseWriter) responseWriter; binWriter.write(response.getOutputStream(), solrReq, solrRsp); } else { PrintWriter out = response.getWriter(); responseWriter.write(out, solrReq, solrRsp); } } // else http HEAD request, nothing to write out, waited this long // just to get ContentType } }
Set<String> returnFields = rb.rsp.getReturnFields(); if (returnFields != null)
/** * Adds some values (most likely from (List) to response which will be sent to the client. The values will be added into * root level under tag name specified by tagName * * @param rb . * @param tagName . * @param value . */ public static void addValueToResult(ResponseBuilder rb, String tagName, Object value) { rb.rsp.add(tagName, value); }
public static void addComponentResponsesToResult(SolrQueryResponse rsp, SolrQueryResponse newRsp, String prefix) { rsp.add(prefix + "_response", newRsp.getValues().get("response")); rsp.add(prefix + "_grouped", newRsp.getValues().get("grouped")); rsp.add(prefix + "_facet_counts", newRsp.getValues().get("facet_counts")); rsp.add(prefix + "_terms", newRsp.getValues().get("terms")); rsp.add(prefix + "_termVectors", newRsp.getValues().get("termVectors")); rsp.add(prefix + "_highlighting", newRsp.getValues().get("highlighting")); rsp.add(prefix + "_stats", newRsp.getValues().get("stats")); } }
/** * Returns spellchecker's suggestions from original response. In case there are no suggestions, returns null. * * @param rb . * @return . */ public static NamedList extractSpellcheckerSuggestions(ResponseBuilder rb) { if (rb.rsp.getValues().get("spellcheck") == null) { return null; } return (NamedList) ((SimpleOrderedMap) rb.rsp.getValues().get("spellcheck")).get("suggestions"); }
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(); 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();
/** It is the users responsibility to close the request object when done with it. * This method does not set/clear SolrRequestInfo */ public SolrQueryResponse queryAndResponse(String handler, SolrQueryRequest req) throws Exception { try (SolrCore core = getCoreInc()) { SolrQueryResponse rsp = new SolrQueryResponse(); core.execute(core.getRequestHandler(handler), req, rsp); if (rsp.getException() != null) { throw rsp.getException(); } return rsp; } }
@Override public void write(final TextResponseWriter writer, final String name, final IndexableField f) throws IOException { writer.writeStr(name, f.stringValue(), true); }
@Override public void write(Writer writer) throws IOException { QueryResponseWriter qrWriter; if (MediaType.APPLICATION_JSON.isCompatible(getMediaType()) || MediaType.APPLICATION_JAVASCRIPT .isCompatible(getMediaType())) { qrWriter = new JSONResponseWriter(); } else { qrWriter = new XMLResponseWriter(); } qrWriter.write(writer, solrQueryRequest, solrQueryResponse); }
public static long extractOriginalQueryHits(ResponseBuilder rb) { long hits = 0; if (rb.rsp != null) { Number hitsInteger = (Number) rb.rsp.getToLog().get("hits"); if (hitsInteger == null) { hits = rb.getNumberDocumentsFound(); } else { hits = hitsInteger.longValue(); } } return hits; }
@Override public void close() throws IOException { super.close(); }
@SuppressWarnings("PMD.SignatureDeclareThrowsException") @Override public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception { super.handleRequestBody(req, rsp); // Add empty 'errors' field, otherwise the KairosDB client crashes. rsp.add("errors", new String[0]); } }
private String getSpellCheckedQuery(SolrQueryResponse rsp) { //check if spellcheck result exists. if(rsp.getValues().get("spellcheck") == null) return null; final NamedList collations = (NamedList)((NamedList)rsp.getValues().get("spellcheck")).get("collations"); if(collations != null && collations.size() > 0) { String s = (String)collations.get("collation"); return s.substring(0,s.length()-1); } else { final String s = (String)((NamedList)((NamedList)rsp.getValues().get("spellcheck")).get("suggestions")).get("collation"); return s != null ? s.substring(0,s.length()-1) : null; } }
@Override public void write(final TextResponseWriter writer, final String name, final IndexableField f) throws IOException { writer.writeStr(name, f.stringValue(), true); }
private void addFacet(SolrQueryRequest req, SolrQueryResponse rsp, SolrParams params, DocListAndSet mltDocs) { if( mltDocs.docSet == null ) { rsp.add( "facet_counts", null ); } else { FacetComponent fct = new FacetComponent(); rsp.add( "facet_counts", fct.getFacetCounts(new SimpleFacets(req, mltDocs.docSet, params )) ); } }
/** * Extract spellchecker's collation from original response or null if there was no collation. * @param rb . * @return . */ public static String extractSpellcheckerCollation(ResponseBuilder rb) { if (rb.rsp.getValues().get("spellcheck") != null) { if (((SimpleOrderedMap) rb.rsp.getValues().get("spellcheck")).get("collations") != null) { return (String) (((NamedList) ((SimpleOrderedMap) rb.rsp.getValues().get("spellcheck")).get("collations")).get("collation")); } } return null; }
@Override public void write(TextResponseWriter writer, String name, IndexableField f) throws IOException { writer.writeStr(name, toBase64String(toObject(f)), false); }
private void addFacet(SolrQueryRequest req, SolrQueryResponse rsp, SolrParams params, DocListAndSet mltDocs) { if( mltDocs.docSet == null ) { rsp.add( "facet_counts", null ); } else { FacetComponent fct = new FacetComponent(); rsp.add( "facet_counts", fct.getFacetCounts(new SimpleFacets(req, mltDocs.docSet, params )) ); } }
private long getResultNumFound (SolrQueryResponse rsp) { Object docs = response.getValues().get("response"); if (docs != null) { if (docs instanceof DocList) { return ((DocList)docs).matches(); } else if (docs instanceof SolrDocumentList) { return ((SolrDocumentList)docs).getNumFound(); } } return 0; }