Search in sources :

Example 16 with SolrDocumentList

use of org.apache.solr.common.SolrDocumentList in project lucene-solr by apache.

the class SolrEntityProcessor method buildIterator.

/**
   * The following method changes the rowIterator mutable field. It requires
   * external synchronization. 
   */
protected void buildIterator() {
    if (rowIterator != null) {
        SolrDocumentListIterator documentListIterator = (SolrDocumentListIterator) rowIterator;
        if (!documentListIterator.hasNext() && documentListIterator.hasMoreRows()) {
            nextPage();
        }
    } else {
        Boolean cursor = new Boolean(context.getResolvedEntityAttribute(CursorMarkParams.CURSOR_MARK_PARAM));
        rowIterator = !cursor ? new SolrDocumentListIterator(new SolrDocumentList()) : new SolrDocumentListCursor(new SolrDocumentList(), CursorMarkParams.CURSOR_MARK_START);
        nextPage();
    }
}
Also used : SolrDocumentList(org.apache.solr.common.SolrDocumentList)

Example 17 with SolrDocumentList

use of org.apache.solr.common.SolrDocumentList in project lucene-solr by apache.

the class TextResponseWriter method writeVal.

public final void writeVal(String name, Object val) throws IOException {
    // go in order of most common to least common
    if (val == null) {
        writeNull(name);
    } else if (val instanceof String) {
        writeStr(name, val.toString(), true);
    // micro-optimization... using toString() avoids a cast first
    } else if (val instanceof IndexableField) {
        IndexableField f = (IndexableField) val;
        SchemaField sf = schema.getFieldOrNull(f.name());
        if (sf != null) {
            sf.getType().write(this, name, f);
        } else {
            writeStr(name, f.stringValue(), true);
        }
    } else if (val instanceof Number) {
        writeNumber(name, (Number) val);
    } else if (val instanceof Boolean) {
        writeBool(name, (Boolean) val);
    } else if (val instanceof Date) {
        writeDate(name, (Date) val);
    } else if (val instanceof Document) {
        SolrDocument doc = DocsStreamer.convertLuceneDocToSolrDoc((Document) val, schema);
        writeSolrDocument(name, doc, returnFields, 0);
    } else if (val instanceof SolrDocument) {
        writeSolrDocument(name, (SolrDocument) val, returnFields, 0);
    } else if (val instanceof ResultContext) {
        // requires access to IndexReader
        writeDocuments(name, (ResultContext) val);
    } else if (val instanceof DocList) {
        // Should not happen normally
        ResultContext ctx = new BasicResultContext((DocList) val, returnFields, null, null, req);
        writeDocuments(name, ctx);
    // }
    // else if (val instanceof DocSet) {
    // how do we know what fields to read?
    // todo: have a DocList/DocSet wrapper that
    // restricts the fields to write...?
    } else if (val instanceof SolrDocumentList) {
        writeSolrDocumentList(name, (SolrDocumentList) val, returnFields);
    } else if (val instanceof Map) {
        writeMap(name, (Map) val, false, true);
    } else if (val instanceof NamedList) {
        writeNamedList(name, (NamedList) val);
    } else if (val instanceof Path) {
        writeStr(name, ((Path) val).toAbsolutePath().toString(), true);
    } else if (val instanceof IteratorWriter) {
        writeIterator((IteratorWriter) val);
    } else if (val instanceof Iterable) {
        writeArray(name, ((Iterable) val).iterator());
    } else if (val instanceof Object[]) {
        writeArray(name, (Object[]) val);
    } else if (val instanceof Iterator) {
        writeArray(name, (Iterator) val);
    } else if (val instanceof byte[]) {
        byte[] arr = (byte[]) val;
        writeByteArr(name, arr, 0, arr.length);
    } else if (val instanceof BytesRef) {
        BytesRef arr = (BytesRef) val;
        writeByteArr(name, arr.bytes, arr.offset, arr.length);
    } else if (val instanceof EnumFieldValue) {
        writeStr(name, val.toString(), true);
    } else if (val instanceof WriteableValue) {
        ((WriteableValue) val).write(name, this);
    } else if (val instanceof MapWriter) {
        writeMap((MapWriter) val);
    } else if (val instanceof MapSerializable) {
        //todo find a better way to reuse the map more efficiently
        writeMap(name, ((MapSerializable) val).toMap(new LinkedHashMap<>()), false, true);
    } else {
        // default... for debugging only
        writeStr(name, val.getClass().getName() + ':' + val.toString(), true);
    }
}
Also used : MapWriter(org.apache.solr.common.MapWriter) EnumFieldValue(org.apache.solr.common.EnumFieldValue) Document(org.apache.lucene.document.Document) SolrDocument(org.apache.solr.common.SolrDocument) SolrDocument(org.apache.solr.common.SolrDocument) IteratorWriter(org.apache.solr.common.IteratorWriter) Iterator(java.util.Iterator) BytesRef(org.apache.lucene.util.BytesRef) Path(java.nio.file.Path) MapSerializable(org.apache.solr.common.MapSerializable) NamedList(org.apache.solr.common.util.NamedList) SolrDocumentList(org.apache.solr.common.SolrDocumentList) Date(java.util.Date) IndexableField(org.apache.lucene.index.IndexableField) SchemaField(org.apache.solr.schema.SchemaField) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) DocList(org.apache.solr.search.DocList)

Example 18 with SolrDocumentList

use of org.apache.solr.common.SolrDocumentList in project lucene-solr by apache.

the class SubQueryAugmenter method transform.

@Override
public void transform(SolrDocument doc, int docid, float score) {
    final SolrParams docWithDeprefixed = SolrParams.wrapDefaults(new DocRowParams(doc, prefix, separator), baseSubParams);
    try {
        Callable<QueryResponse> subQuery = new Callable<QueryResponse>() {

            @Override
            public QueryResponse call() throws Exception {
                try {
                    return new QueryResponse(server.request(new QueryRequest(docWithDeprefixed), coreName), server);
                } finally {
                }
            }
        };
        QueryResponse response = SolrRequestInfoSuspender.doInSuspension(subQuery);
        final SolrDocumentList docList = (SolrDocumentList) response.getResults();
        doc.setField(getName(), new Result(docList));
    } catch (Exception e) {
        String docString = doc.toString();
        throw new SolrException(ErrorCode.BAD_REQUEST, "while invoking " + name + ":[subquery" + (coreName != null ? "fromIndex=" + coreName : "") + "] on doc=" + docString.substring(0, Math.min(100, docString.length())), e.getCause());
    } finally {
    }
}
Also used : QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrDocumentList(org.apache.solr.common.SolrDocumentList) Callable(java.util.concurrent.Callable) SolrException(org.apache.solr.common.SolrException) SolrException(org.apache.solr.common.SolrException)

Example 19 with SolrDocumentList

use of org.apache.solr.common.SolrDocumentList 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 20 with SolrDocumentList

use of org.apache.solr.common.SolrDocumentList in project lucene-solr by apache.

the class DebugComponent method getTrackResponse.

private NamedList<String> getTrackResponse(ShardResponse shardResponse) {
    NamedList<String> namedList = new SimpleOrderedMap<>();
    if (shardResponse.getException() != null) {
        namedList.add("Exception", shardResponse.getException().getMessage());
        return namedList;
    }
    NamedList<Object> responseNL = shardResponse.getSolrResponse().getResponse();
    @SuppressWarnings("unchecked") NamedList<Object> responseHeader = (NamedList<Object>) responseNL.get("responseHeader");
    if (responseHeader != null) {
        namedList.add("QTime", responseHeader.get("QTime").toString());
    }
    namedList.add("ElapsedTime", String.valueOf(shardResponse.getSolrResponse().getElapsedTime()));
    namedList.add("RequestPurpose", shardResponse.getShardRequest().params.get(CommonParams.REQUEST_PURPOSE));
    SolrDocumentList docList = (SolrDocumentList) shardResponse.getSolrResponse().getResponse().get("response");
    if (docList != null) {
        namedList.add("NumFound", String.valueOf(docList.getNumFound()));
    }
    namedList.add("Response", String.valueOf(responseNL));
    return namedList;
}
Also used : NamedList(org.apache.solr.common.util.NamedList) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap)

Aggregations

SolrDocumentList (org.apache.solr.common.SolrDocumentList)263 SolrDocument (org.apache.solr.common.SolrDocument)153 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)118 SolrQuery (org.apache.solr.client.solrj.SolrQuery)83 Test (org.junit.Test)73 ArrayList (java.util.ArrayList)61 SolrServerException (org.apache.solr.client.solrj.SolrServerException)50 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)44 NamedList (org.apache.solr.common.util.NamedList)44 IOException (java.io.IOException)43 SolrParams (org.apache.solr.common.params.SolrParams)32 SolrInputDocument (org.apache.solr.common.SolrInputDocument)26 HashMap (java.util.HashMap)22 Map (java.util.Map)21 SolrClient (org.apache.solr.client.solrj.SolrClient)17 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)17 SolrException (org.apache.solr.common.SolrException)16 List (java.util.List)15 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)14 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)14