Search in sources :

Example 1 with QueryResponseWriter

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();
    }
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) StringWriter(java.io.StringWriter) SolrCore(org.apache.solr.core.SolrCore) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) BinaryQueryResponseWriter(org.apache.solr.response.BinaryQueryResponseWriter) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BinaryQueryResponseWriter(org.apache.solr.response.BinaryQueryResponseWriter)

Example 2 with QueryResponseWriter

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();
}
Also used : SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SolrDocument(org.apache.solr.common.SolrDocument) StringWriter(java.io.StringWriter) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) PHPSerializedResponseWriter(org.apache.solr.response.PHPSerializedResponseWriter) SolrDocumentList(org.apache.solr.common.SolrDocumentList) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 3 with QueryResponseWriter

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 };
        }
    };
}
Also used : CompositeSpatialStrategy(org.apache.lucene.spatial.composite.CompositeSpatialStrategy) JSONResponseWriter(org.apache.solr.response.JSONResponseWriter) ShapeWriter(org.locationtech.spatial4j.io.ShapeWriter) AbstractSpatialFieldType(org.apache.solr.schema.AbstractSpatialFieldType) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) CompositeSpatialStrategy(org.apache.lucene.spatial.composite.CompositeSpatialStrategy) SpatialStrategy(org.apache.lucene.spatial.SpatialStrategy) SerializedDVStrategy(org.apache.lucene.spatial.serialized.SerializedDVStrategy) SchemaField(org.apache.solr.schema.SchemaField) SolrDocument(org.apache.solr.common.SolrDocument) ValueSource(org.apache.lucene.queries.function.ValueSource) QParser(org.apache.solr.search.QParser) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) GeoJSONWriter(org.locationtech.spatial4j.io.GeoJSONWriter) SolrException(org.apache.solr.common.SolrException)

Example 4 with QueryResponseWriter

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");
    }
}
Also used : SolrCore(org.apache.solr.core.SolrCore) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Example 5 with QueryResponseWriter

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()));
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter)

Aggregations

QueryResponseWriter (org.apache.solr.response.QueryResponseWriter)15 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)11 StringWriter (java.io.StringWriter)6 SolrCore (org.apache.solr.core.SolrCore)5 SolrRequestInfo (org.apache.solr.request.SolrRequestInfo)5 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)4 Test (org.junit.Test)4 IOException (java.io.IOException)3 MapSolrParams (org.apache.solr.common.params.MapSolrParams)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 BinaryResponseParser (org.apache.solr.client.solrj.impl.BinaryResponseParser)2 SolrDocument (org.apache.solr.common.SolrDocument)2 SolrException (org.apache.solr.common.SolrException)2 ContentStream (org.apache.solr.common.util.ContentStream)2 ContentStreamBase (org.apache.solr.common.util.ContentStreamBase)2 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1