Search in sources :

Example 11 with QueryResponseWriter

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

Example 12 with QueryResponseWriter

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']");
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) HashMap(java.util.HashMap) SolrCore(org.apache.solr.core.SolrCore) ArrayList(java.util.ArrayList) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) ContentStream(org.apache.solr.common.util.ContentStream) MapSolrParams(org.apache.solr.common.params.MapSolrParams) StringWriter(java.io.StringWriter) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase) Test(org.junit.Test)

Example 13 with QueryResponseWriter

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));
        });
    });
}
Also used : BitSetIterator(org.apache.lucene.util.BitSetIterator) Date(java.util.Date) EntryWriter(org.apache.solr.common.MapWriter.EntryWriter) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) IndexableField(org.apache.lucene.index.IndexableField) JSONResponseWriter(org.apache.solr.response.JSONResponseWriter) BoolField(org.apache.solr.schema.BoolField) StrField(org.apache.solr.schema.StrField) LoggerFactory(org.slf4j.LoggerFactory) LongValues(org.apache.lucene.util.LongValues) SolrParams(org.apache.solr.common.params.SolrParams) Collections.singletonList(java.util.Collections.singletonList) SolrException(org.apache.solr.common.SolrException) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) SortField(org.apache.lucene.search.SortField) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) PrintWriter(java.io.PrintWriter) MapWriter(org.apache.solr.common.MapWriter) Sort(org.apache.lucene.search.Sort) BytesRef(org.apache.lucene.util.BytesRef) SolrCore(org.apache.solr.core.SolrCore) MethodHandles(java.lang.invoke.MethodHandles) StandardCharsets(java.nio.charset.StandardCharsets) Utils.makeMap(org.apache.solr.common.util.Utils.makeMap) List(java.util.List) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) LeafReader(org.apache.lucene.index.LeafReader) IteratorWriter(org.apache.solr.common.IteratorWriter) JavaBinCodec(org.apache.solr.common.util.JavaBinCodec) TrieDateField(org.apache.solr.schema.TrieDateField) PushWriter(org.apache.solr.common.PushWriter) NumericDocValues(org.apache.lucene.index.NumericDocValues) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) FieldType(org.apache.solr.schema.FieldType) FixedBitSet(org.apache.lucene.util.FixedBitSet) CharsRefBuilder(org.apache.lucene.util.CharsRefBuilder) SortSpec(org.apache.solr.search.SortSpec) BinaryResponseParser(org.apache.solr.client.solrj.impl.BinaryResponseParser) SchemaField(org.apache.solr.schema.SchemaField) TrieFloatField(org.apache.solr.schema.TrieFloatField) TrieLongField(org.apache.solr.schema.TrieLongField) SyntaxError(org.apache.solr.search.SyntaxError) SortedSetDocValues(org.apache.lucene.index.SortedSetDocValues) TrieIntField(org.apache.solr.schema.TrieIntField) OutputStreamWriter(java.io.OutputStreamWriter) Collections.singletonMap(java.util.Collections.singletonMap) SortedDocValues(org.apache.lucene.index.SortedDocValues) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator) OutputStream(java.io.OutputStream) ArrayUtil(org.apache.lucene.util.ArrayUtil) Logger(org.slf4j.Logger) MultiDocValues(org.apache.lucene.index.MultiDocValues) BinaryResponseWriter(org.apache.solr.response.BinaryResponseWriter) IOException(java.io.IOException) TrieDoubleField(org.apache.solr.schema.TrieDoubleField) IndexSchema(org.apache.solr.schema.IndexSchema) DocValues(org.apache.lucene.index.DocValues) Closeable(java.io.Closeable) BinaryResponseWriter(org.apache.solr.response.BinaryResponseWriter) IOException(java.io.IOException) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) SolrException(org.apache.solr.common.SolrException) IOException(java.io.IOException) JavaBinCodec(org.apache.solr.common.util.JavaBinCodec) SyntaxError(org.apache.solr.search.SyntaxError) FixedBitSet(org.apache.lucene.util.FixedBitSet) IteratorWriter(org.apache.solr.common.IteratorWriter) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) Sort(org.apache.lucene.search.Sort) SolrParams(org.apache.solr.common.params.SolrParams) OutputStreamWriter(java.io.OutputStreamWriter) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) SortSpec(org.apache.solr.search.SortSpec)

Example 14 with QueryResponseWriter

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();
    }
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) ArrayList(java.util.ArrayList) ContentStream(org.apache.solr.common.util.ContentStream) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) MapSolrParams(org.apache.solr.common.params.MapSolrParams) StringWriter(java.io.StringWriter) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase)

Example 15 with QueryResponseWriter

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);
}
Also used : 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