Search in sources :

Example 11 with SolrReturnFields

use of org.apache.solr.search.SolrReturnFields in project lucene-solr by apache.

the class FieldOptions method getFields.

/**
   * Helper method for determining the list of fields that we should 
   * try to find term vectors on.  
   * <p>
   * Does simple (non-glob-supporting) parsing on the 
   * {@link TermVectorParams#FIELDS} param if specified, otherwise it returns 
   * the concrete field values specified in {@link CommonParams#FL} -- 
   * ignoring functions, transformers, or literals.  
   * </p>
   * <p>
   * If "fl=*" is used, or neither param is specified, then <code>null</code> 
   * will be returned.  If the empty set is returned, it means the "fl" 
   * specified consisted entirely of things that are not real fields 
   * (ie: functions, transformers, partial-globs, score, etc...) and not 
   * supported by this component. 
   * </p>
   */
private Set<String> getFields(ResponseBuilder rb) {
    SolrParams params = rb.req.getParams();
    String[] fldLst = params.getParams(TermVectorParams.FIELDS);
    if (null == fldLst || 0 == fldLst.length || (1 == fldLst.length && 0 == fldLst[0].length())) {
        // no tv.fl, parse the main fl
        ReturnFields rf = new SolrReturnFields(params.getParams(CommonParams.FL), rb.req);
        if (rf.wantsAllFields()) {
            return null;
        }
        Set<String> fieldNames = rf.getLuceneFieldNames();
        return (null != fieldNames) ? fieldNames : // return empty set indicating no fields should be used
        Collections.<String>emptySet();
    }
    // otherwise us the raw fldList as is, no special parsing or globs
    Set<String> fieldNames = new LinkedHashSet<>();
    for (String fl : fldLst) {
        fieldNames.addAll(Arrays.asList(SolrPluginUtils.split(fl)));
    }
    return fieldNames;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SolrReturnFields(org.apache.solr.search.SolrReturnFields) ReturnFields(org.apache.solr.search.ReturnFields) SolrParams(org.apache.solr.common.params.SolrParams) SolrReturnFields(org.apache.solr.search.SolrReturnFields)

Example 12 with SolrReturnFields

use of org.apache.solr.search.SolrReturnFields in project lucene-solr by apache.

the class TestXLSXResponseWriter method testStructuredDataViaBaseWriters.

@Test
public void testStructuredDataViaBaseWriters() throws IOException, Exception {
    SolrQueryResponse rsp = new SolrQueryResponse();
    // Don't send a ContentStream back, this will fall back to the configured base writer.
    // But abuse the CONTENT key to ensure writer is also checking type
    rsp.add(RawResponseWriter.CONTENT, "test");
    rsp.add("foo", "bar");
    SolrQueryRequest r = req();
    // check Content-Type
    assertEquals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", writerXlsx.getContentType(r, rsp));
    // test our basic types,and that fields come back in the requested order
    XSSFSheet resultSheet = getWSResultForQuery(req("q", "id:1", "wt", "xlsx", "fl", "id,foo_s,foo_i,foo_l,foo_b,foo_f,foo_d,foo_dt1"));
    assertEquals("id,foo_s,foo_i,foo_l,foo_b,foo_f,foo_d,foo_dt1\n1,hi,-1,12345678987654321,F,1.414,-1.0E300,2000-01-02T03:04:05Z\n", getStringFromSheet(resultSheet));
    resultSheet = getWSResultForQuery(req("q", "id:1^0", "wt", "xlsx", "fl", "id,score,foo_s"));
    // test retrieving score
    assertEquals("id,score,foo_s\n1,0.0,hi\n", getStringFromSheet(resultSheet));
    resultSheet = getWSResultForQuery(req("q", "id:1^0", "wt", "xlsx", "colname.id", "I.D.", "colwidth.id", "10", "fl", "id,score,foo_s"));
    // test override colname/width
    assertEquals("I.D.,score,foo_s\n1,0.0,hi\n", getStringFromSheet(resultSheet));
    // test colwidth (value returned is in 256ths of a character as per excel standard)
    assertEquals(10 * 256, resultSheet.getColumnWidth(0));
    resultSheet = getWSResultForQuery(req("q", "id:2", "wt", "xlsx", "fl", "id,v_ss"));
    // test multivalued
    assertEquals("id,v_ss\n2,hi; there\n", getStringFromSheet(resultSheet));
    // test retrieving fields from index
    resultSheet = getWSResultForQuery(req("q", "*:*", "wt", "xslx", "fl", "*,score"));
    String result = getStringFromSheet(resultSheet);
    for (String field : "id,foo_s,foo_i,foo_l,foo_b,foo_f,foo_d,foo_dt1,v_ss,v2_ss,score".split(",")) {
        assertTrue(result.indexOf(field) >= 0);
    }
    // test null values
    resultSheet = getWSResultForQuery(req("q", "id:2", "wt", "xlsx", "fl", "id,foo_s,v_ss"));
    assertEquals("id,foo_s,v_ss\n2,,hi; there\n", getStringFromSheet(resultSheet));
    // now test SolrDocumentList
    SolrDocument d = new SolrDocument();
    SolrDocument d1 = d;
    d.addField("id", "1");
    d.addField("foo_i", -1);
    d.addField("foo_s", "hi");
    d.addField("foo_l", "12345678987654321L");
    d.addField("foo_b", false);
    d.addField("foo_f", 1.414f);
    d.addField("foo_d", -1.0E300);
    d.addField("foo_dt1", new Date(Instant.parse("2000-01-02T03:04:05Z").toEpochMilli()));
    d.addField("score", "2.718");
    d = new SolrDocument();
    SolrDocument d2 = d;
    d.addField("id", "2");
    d.addField("v_ss", "hi");
    d.addField("v_ss", "there");
    d.addField("v2_ss", "nice");
    d.addField("v2_ss", "output");
    d.addField("score", "89.83");
    d.addField("shouldbeunstored", "foo");
    SolrDocumentList sdl = new SolrDocumentList();
    sdl.add(d1);
    sdl.add(d2);
    SolrQueryRequest req = req("q", "*:*");
    rsp = new SolrQueryResponse();
    rsp.addResponse(sdl);
    rsp.setReturnFields(new SolrReturnFields("id,foo_s", req));
    resultSheet = getWSResultForQuery(req, rsp);
    assertEquals("id,foo_s\n1,hi\n2,\n", getStringFromSheet(resultSheet));
    // try scores
    rsp.setReturnFields(new SolrReturnFields("id,score,foo_s", req));
    resultSheet = getWSResultForQuery(req, rsp);
    assertEquals("id,score,foo_s\n1,2.718,hi\n2,89.83,\n", getStringFromSheet(resultSheet));
    // get field values from docs... should be ordered and not include score unless requested
    rsp.setReturnFields(new SolrReturnFields("*", req));
    resultSheet = getWSResultForQuery(req, rsp);
    assertEquals("id,foo_i,foo_s,foo_l,foo_b,foo_f,foo_d,foo_dt1,v_ss,v2_ss\n" + "1,-1,hi,12345678987654321L,false,1.414,-1.0E300,2000-01-02T03:04:05Z,,\n" + "2,,,,,,,,hi; there,nice; output\n", getStringFromSheet(resultSheet));
    // get field values and scores - just check that the scores are there... we don't guarantee where
    rsp.setReturnFields(new SolrReturnFields("*,score", req));
    resultSheet = getWSResultForQuery(req, rsp);
    String s = getStringFromSheet(resultSheet);
    assertTrue(s.indexOf("score") >= 0 && s.indexOf("2.718") > 0 && s.indexOf("89.83") > 0);
    // Test field globs
    rsp.setReturnFields(new SolrReturnFields("id,foo*", req));
    resultSheet = getWSResultForQuery(req, rsp);
    assertEquals("id,foo_i,foo_s,foo_l,foo_b,foo_f,foo_d,foo_dt1\n" + "1,-1,hi,12345678987654321L,false,1.414,-1.0E300,2000-01-02T03:04:05Z\n" + "2,,,,,,,\n", getStringFromSheet(resultSheet));
    rsp.setReturnFields(new SolrReturnFields("id,*_d*", req));
    resultSheet = getWSResultForQuery(req, rsp);
    assertEquals("id,foo_d,foo_dt1\n" + "1,-1.0E300,2000-01-02T03:04:05Z\n" + "2,,\n", getStringFromSheet(resultSheet));
    // Test function queries
    rsp.setReturnFields(new SolrReturnFields("sum(1,1),id,exists(foo_s1),div(9,1),foo_f", req));
    resultSheet = getWSResultForQuery(req, rsp);
    assertEquals("sum(1,1),id,exists(foo_s1),div(9,1),foo_f\n" + ",1,,,1.414\n" + ",2,,,\n", getStringFromSheet(resultSheet));
    // Test transformers
    rsp.setReturnFields(new SolrReturnFields("mydocid:[docid],[explain]", req));
    resultSheet = getWSResultForQuery(req, rsp);
    assertEquals("mydocid,[explain]\n" + ",\n" + ",\n", getStringFromSheet(resultSheet));
    req.close();
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) SolrDocument(org.apache.solr.common.SolrDocument) SolrDocumentList(org.apache.solr.common.SolrDocumentList) Date(java.util.Date) SolrReturnFields(org.apache.solr.search.SolrReturnFields) Test(org.junit.Test)

Example 13 with SolrReturnFields

use of org.apache.solr.search.SolrReturnFields in project lucene-solr by apache.

the class RealTimeGetComponent method getInputDocumentFromTlog.

/** returns the SolrInputDocument from the current tlog, or DELETED if it has been deleted, or
   * null if there is no record of it in the current update log.  If null is returned, it could
   * still be in the latest index.
   * @param versionReturned If a non-null AtomicLong is passed in, it is set to the version of the update returned from the TLog.
   * @param resolveFullDocument In case the document is fetched from the tlog, it could only be a partial document if the last update
   *                  was an in-place update. In that case, should this partial document be resolved to a full document (by following
   *                  back prevPointer/prevVersion)?
   */
public static SolrInputDocument getInputDocumentFromTlog(SolrCore core, BytesRef idBytes, AtomicLong versionReturned, Set<String> onlyTheseNonStoredDVs, boolean resolveFullDocument) {
    UpdateLog ulog = core.getUpdateHandler().getUpdateLog();
    if (ulog != null) {
        Object o = ulog.lookup(idBytes);
        if (o != null) {
            // should currently be a List<Oper,Ver,Doc/Id>
            List entry = (List) o;
            assert entry.size() >= 3;
            int oper = (Integer) entry.get(0) & UpdateLog.OPERATION_MASK;
            if (versionReturned != null) {
                versionReturned.set((long) entry.get(UpdateLog.VERSION_IDX));
            }
            switch(oper) {
                case UpdateLog.UPDATE_INPLACE:
                    assert entry.size() == 5;
                    if (resolveFullDocument) {
                        SolrInputDocument doc = (SolrInputDocument) entry.get(entry.size() - 1);
                        try {
                            // For in-place update case, we have obtained the partial document till now. We need to
                            // resolve it to a full document to be returned to the user.
                            SolrDocument sdoc = resolveFullDocument(core, idBytes, new SolrReturnFields(), doc, entry, onlyTheseNonStoredDVs);
                            if (sdoc == null) {
                                return DELETED;
                            }
                            doc = toSolrInputDocument(sdoc, core.getLatestSchema());
                            return doc;
                        } catch (IOException ex) {
                            throw new SolrException(ErrorCode.SERVER_ERROR, "Error while resolving full document. ", ex);
                        }
                    } else {
                    // fall through to ADD, so as to get only the partial document
                    }
                case UpdateLog.ADD:
                    return (SolrInputDocument) entry.get(entry.size() - 1);
                case UpdateLog.DELETE:
                    return DELETED;
                default:
                    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown Operation! " + oper);
            }
        }
    }
    return null;
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrDocument(org.apache.solr.common.SolrDocument) UpdateLog(org.apache.solr.update.UpdateLog) SolrDocumentList(org.apache.solr.common.SolrDocumentList) DocList(org.apache.solr.search.DocList) List(java.util.List) ArrayList(java.util.ArrayList) NamedList(org.apache.solr.common.util.NamedList) IOException(java.io.IOException) IndexFingerprint(org.apache.solr.update.IndexFingerprint) SolrReturnFields(org.apache.solr.search.SolrReturnFields) SolrException(org.apache.solr.common.SolrException)

Aggregations

SolrReturnFields (org.apache.solr.search.SolrReturnFields)13 ReturnFields (org.apache.solr.search.ReturnFields)8 SolrDocument (org.apache.solr.common.SolrDocument)7 SolrDocumentList (org.apache.solr.common.SolrDocumentList)5 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 SolrParams (org.apache.solr.common.params.SolrParams)4 NamedList (org.apache.solr.common.util.NamedList)4 SolrException (org.apache.solr.common.SolrException)3 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)3 DocList (org.apache.solr.search.DocList)3 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 Date (java.util.Date)2 List (java.util.List)2 Map (java.util.Map)2 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 Query (org.apache.lucene.search.Query)2 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)2