Search in sources :

Example 1 with ResultContext

use of org.apache.solr.response.ResultContext in project lucene-solr by apache.

the class XLSXWriter method writeResponse.

public void writeResponse(OutputStream out, LinkedHashMap<String, String> colNamesMap, LinkedHashMap<String, Integer> colWidthsMap) throws IOException {
    SolrParams params = req.getParams();
    Collection<String> fields = returnFields.getRequestedFieldNames();
    Object responseObj = rsp.getValues().get("response");
    boolean returnOnlyStored = false;
    if (fields == null || returnFields.hasPatternMatching()) {
        if (responseObj instanceof SolrDocumentList) {
            // get the list of fields from the SolrDocumentList
            if (fields == null) {
                fields = new LinkedHashSet<String>();
            }
            for (SolrDocument sdoc : (SolrDocumentList) responseObj) {
                fields.addAll(sdoc.getFieldNames());
            }
        } else {
            // get the list of fields from the index
            Iterable<String> all = req.getSearcher().getFieldNames();
            if (fields == null) {
                fields = Sets.newHashSet(all);
            } else {
                Iterables.addAll(fields, all);
            }
        }
        if (returnFields.wantsScore()) {
            fields.add("score");
        } else {
            fields.remove("score");
        }
        returnOnlyStored = true;
    }
    for (String field : fields) {
        if (!returnFields.wantsField(field)) {
            continue;
        }
        if (field.equals("score")) {
            XLField xlField = new XLField();
            xlField.name = "score";
            xlFields.put("score", xlField);
            continue;
        }
        SchemaField sf = schema.getFieldOrNull(field);
        if (sf == null) {
            FieldType ft = new StrField();
            sf = new SchemaField(field, ft);
        }
        // Return only stored fields, unless an explicit field list is specified
        if (returnOnlyStored && sf != null && !sf.stored()) {
            continue;
        }
        XLField xlField = new XLField();
        xlField.name = field;
        xlField.sf = sf;
        xlFields.put(field, xlField);
    }
    wb.addRow();
    //write header
    for (XLField xlField : xlFields.values()) {
        String printName = xlField.name;
        int colWidth = 14;
        String niceName = colNamesMap.get(xlField.name);
        if (niceName != null) {
            printName = niceName;
        }
        Integer niceWidth = colWidthsMap.get(xlField.name);
        if (niceWidth != null) {
            colWidth = niceWidth.intValue();
        }
        writeStr(xlField.name, printName, false);
        wb.setColWidth(colWidth);
        wb.setHeaderCell();
    }
    wb.setHeaderRow();
    wb.addRow();
    if (responseObj instanceof ResultContext) {
        writeDocuments(null, (ResultContext) responseObj);
    } else if (responseObj instanceof DocList) {
        ResultContext ctx = new BasicResultContext((DocList) responseObj, returnFields, null, null, req);
        writeDocuments(null, ctx);
    } else if (responseObj instanceof SolrDocumentList) {
        writeSolrDocumentList(null, (SolrDocumentList) responseObj, returnFields);
    }
    wb.flush(out);
    wb = null;
}
Also used : BasicResultContext(org.apache.solr.response.BasicResultContext) ResultContext(org.apache.solr.response.ResultContext) StrField(org.apache.solr.schema.StrField) SolrDocumentList(org.apache.solr.common.SolrDocumentList) FieldType(org.apache.solr.schema.FieldType) SchemaField(org.apache.solr.schema.SchemaField) BasicResultContext(org.apache.solr.response.BasicResultContext) SolrDocument(org.apache.solr.common.SolrDocument) SolrParams(org.apache.solr.common.params.SolrParams) DocList(org.apache.solr.search.DocList)

Example 2 with ResultContext

use of org.apache.solr.response.ResultContext in project lucene-solr by apache.

the class TestGroupingSearch method testGroupingSimpleFormatArrayIndexOutOfBoundsExceptionWithJavaBin.

@Test
public void testGroupingSimpleFormatArrayIndexOutOfBoundsExceptionWithJavaBin() throws Exception {
    assertU(add(doc("id", "1", "nullfirst", "1")));
    assertU(add(doc("id", "2", "nullfirst", "1")));
    assertU(add(doc("id", "3", "nullfirst", "2")));
    assertU(add(doc("id", "4", "nullfirst", "2")));
    assertU(add(doc("id", "5", "nullfirst", "2")));
    assertU(add(doc("id", "6", "nullfirst", "3")));
    assertU(commit());
    SolrQueryRequest request = req("q", "*:*", "group", "true", "group.field", "nullfirst", "group.main", "true", "wt", "javabin", "start", "4", "rows", "10");
    SolrQueryResponse response = new SolrQueryResponse();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        SolrRequestInfo.setRequestInfo(new SolrRequestInfo(request, response));
        String handlerName = request.getParams().get(CommonParams.QT);
        h.getCore().execute(h.getCore().getRequestHandler(handlerName), request, response);
        BinaryResponseWriter responseWriter = new BinaryResponseWriter();
        responseWriter.write(out, request, response);
    } finally {
        request.close();
        SolrRequestInfo.clearRequestInfo();
    }
    assertEquals(6, ((ResultContext) response.getResponse()).getDocList().matches());
    new BinaryResponseParser().processResponse(new ByteArrayInputStream(out.toByteArray()), "");
    out.close();
}
Also used : ResultContext(org.apache.solr.response.ResultContext) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) BinaryResponseParser(org.apache.solr.client.solrj.impl.BinaryResponseParser) ByteArrayInputStream(java.io.ByteArrayInputStream) BinaryResponseWriter(org.apache.solr.response.BinaryResponseWriter) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 3 with ResultContext

use of org.apache.solr.response.ResultContext in project lucene-solr by apache.

the class BasicFunctionalityTest method testNotLazyField.

@Test
public void testNotLazyField() throws IOException {
    assertU(adoc("id", "7777", "title", "keyword", "test_hlt", mkstr(20000)));
    assertU(commit());
    SolrCore core = h.getCore();
    SolrQueryRequest req = req("q", "id:7777", "fl", "id,title,test_hlt");
    SolrQueryResponse rsp = new SolrQueryResponse();
    core.execute(core.getRequestHandler(req.getParams().get(CommonParams.QT)), req, rsp);
    DocList dl = ((ResultContext) rsp.getResponse()).getDocList();
    Document d = req.getSearcher().doc(dl.iterator().nextDoc());
    // ensure field in fl is not lazy
    assertFalse(((Field) d.getField("test_hlt")).getClass().getSimpleName().equals("LazyField"));
    assertFalse(((Field) d.getField("title")).getClass().getSimpleName().equals("LazyField"));
    req.close();
}
Also used : ResultContext(org.apache.solr.response.ResultContext) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SolrCore(org.apache.solr.core.SolrCore) LazyDocument(org.apache.lucene.document.LazyDocument) Document(org.apache.lucene.document.Document) DocList(org.apache.solr.search.DocList) Test(org.junit.Test)

Example 4 with ResultContext

use of org.apache.solr.response.ResultContext in project lucene-solr by apache.

the class BasicFunctionalityTest method testLazyField.

@Test
public void testLazyField() throws IOException {
    assertU(adoc("id", "7777", "title", "keyword", "test_hlt", mkstr(10000), "test_hlt", mkstr(20000), "test_hlt", mkstr(30000), "test_hlt", mkstr(40000)));
    assertU(commit());
    SolrCore core = h.getCore();
    // initial request
    SolrQueryRequest req = req("q", "id:7777", "fl", "id,title");
    SolrQueryResponse rsp = new SolrQueryResponse();
    core.execute(core.getRequestHandler(req.getParams().get(CommonParams.QT)), req, rsp);
    DocList dl = ((ResultContext) rsp.getResponse()).getDocList();
    DocIterator di = dl.iterator();
    Document d1 = req.getSearcher().doc(di.nextDoc());
    IndexableField[] values1 = null;
    // ensure fl field is non lazy, and non-fl field is lazy
    assertFalse(d1.getField("title") instanceof LazyDocument.LazyField);
    assertFalse(d1.getField("id") instanceof LazyDocument.LazyField);
    values1 = d1.getFields("test_hlt");
    assertEquals(4, values1.length);
    for (int i = 0; i < values1.length; i++) {
        assertTrue(values1[i] instanceof LazyDocument.LazyField);
        LazyDocument.LazyField f = (LazyDocument.LazyField) values1[i];
        assertFalse(f.hasBeenLoaded());
    }
    req.close();
    // followup request, different fl
    req = req("q", "id:7777", "fl", "id,test_hlt");
    rsp = new SolrQueryResponse();
    core.execute(core.getRequestHandler(req.getParams().get(CommonParams.QT)), req, rsp);
    dl = ((ResultContext) rsp.getResponse()).getDocList();
    di = dl.iterator();
    Document d2 = req.getSearcher().doc(di.nextDoc());
    // ensure same doc, same lazy field now
    assertTrue("Doc was not cached", d1 == d2);
    IndexableField[] values2 = d2.getFields("test_hlt");
    assertEquals(values1.length, values2.length);
    for (int i = 0; i < values1.length; i++) {
        assertSame("LazyField wasn't reused", values1[i], values2[i]);
        LazyDocument.LazyField f = (LazyDocument.LazyField) values1[i];
        // still not a real boy, no response writer in play
        assertFalse(f.hasBeenLoaded());
    }
    // actuallize one value
    assertNotNull(values2[0].stringValue());
    for (int i = 0; i < values2.length; i++) {
        // now all values for this field should be loaded & cached
        LazyDocument.LazyField f = (LazyDocument.LazyField) values2[i];
        assertTrue(f.hasBeenLoaded());
    }
    req.close();
}
Also used : ResultContext(org.apache.solr.response.ResultContext) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) DocIterator(org.apache.solr.search.DocIterator) SolrCore(org.apache.solr.core.SolrCore) LazyDocument(org.apache.lucene.document.LazyDocument) Document(org.apache.lucene.document.Document) IndexableField(org.apache.lucene.index.IndexableField) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) LazyDocument(org.apache.lucene.document.LazyDocument) DocList(org.apache.solr.search.DocList) Test(org.junit.Test)

Example 5 with ResultContext

use of org.apache.solr.response.ResultContext in project SearchServices by Alfresco.

the class Cloud method getResultContext.

/**
 * @param requestHandler the handler that handles the request
 * @param request the request object to put the params on
 * @param params Solr parameters
 * @return the result context from the handled request
 */
ResultContext getResultContext(SolrRequestHandler requestHandler, SolrQueryRequest request, SolrParams params) {
    SolrQueryResponse solrRsp = getResponse(requestHandler, request, params);
    ResultContext rc = (ResultContext) solrRsp.getValues().get("response");
    return rc;
}
Also used : ResultContext(org.apache.solr.response.ResultContext) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse)

Aggregations

ResultContext (org.apache.solr.response.ResultContext)17 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)10 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)10 DocList (org.apache.solr.search.DocList)9 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)8 NamedList (org.apache.solr.common.util.NamedList)6 SolrDocumentList (org.apache.solr.common.SolrDocumentList)5 SolrException (org.apache.solr.common.SolrException)5 SolrParams (org.apache.solr.common.params.SolrParams)5 DocIterator (org.apache.solr.search.DocIterator)5 Document (org.apache.lucene.document.Document)4 SolrCore (org.apache.solr.core.SolrCore)4 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)4 SolrIndexSearcher (org.apache.solr.search.SolrIndexSearcher)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 SolrRequestInfo (org.apache.solr.request.SolrRequestInfo)3 SchemaField (org.apache.solr.schema.SchemaField)3 IOException (java.io.IOException)2