private void returnFields(ResponseBuilder rb, ShardRequest sreq) { // Keep in mind that this could also be a shard in a multi-tiered system. // TODO: if a multi-tiered system, it seems like some requests // could/should bypass middlemen (like retrieving stored fields) // TODO: merge fsv to if requested if ((sreq.purpose & ShardRequest.PURPOSE_GET_FIELDS) != 0) { boolean returnScores = (rb.getFieldFlags() & SolrIndexSearcher.GET_SCORES) != 0; assert (sreq.responses.size() == 1); ShardResponse srsp = sreq.responses.get(0); SolrDocumentList docs = (SolrDocumentList) srsp.getSolrResponse().getResponse().get("response"); String keyFieldName = rb.req.getSchema().getUniqueKeyField().getName(); for (SolrDocument doc : docs) { Object id = doc.getFieldValue(keyFieldName); ShardDoc sdoc = rb.resultIds.get(id.toString()); if (sdoc != null) { if (returnScores && sdoc.score != null) { doc.setField("score", sdoc.score); } rb._responseDocs.set(sdoc.positionInResponse, doc); } } } }
/** * @param d SolrInputDocument to convert * @return a SolrDocument with the same fields and values as the SolrInputDocument */ public static SolrDocument toSolrDocument(SolrInputDocument d) { SolrDocument doc = new SolrDocument(); for (SolrInputField field : d) { doc.setField(field.getName(), field.getValue()); } return doc; }
@Override public void transform(SolrDocument doc, int docid) { if (context != null && context.query != null) { try { Explanation exp = context.searcher.explain(context.query, docid); if (style == Style.nl) { doc.setField(name, SolrPluginUtils.explanationToNamedList(exp)); } else if (style == Style.html) { doc.setField(name, exp.toHtml()); } else { doc.setField(name, exp.toString()); } } catch (IOException e) { e.printStackTrace(); } } }
public static final SolrDocument toSolrDocument(Document doc, final IndexSchema schema) { SolrDocument out = new SolrDocument(); for (IndexableField f : doc.getFields()) { // Make sure multivalued fields are represented as lists Object existing = out.get(f.name()); if (existing == null) { SchemaField sf = schema.getFieldOrNull(f.name()); if (sf != null && sf.multiValued()) { List<Object> vals = new ArrayList<>(); vals.add(f); out.setField(f.name(), vals); } else { out.setField(f.name(), f); } } else { out.addField(f.name(), f); } } return out; }
/** Prepend the server context path to the location of search result documents. */ private void setSearchResultContext(NamedList<Object> values, String contextPath) { // find the search result documents in the search response SolrDocumentList documents = (SolrDocumentList) values.get("response"); // $NON-NLS-1$ if (documents == null) return; for (SolrDocument doc : documents) { String location = (String) doc.getFieldValue(ProtocolConstants.KEY_LOCATION); if (location != null) { // prepend the context path and update the document location = contextPath + location; doc.setField(ProtocolConstants.KEY_LOCATION, location); } } }
private static SolrDocument toSolrDoc(StoredDocument doc, IndexSchema schema) { SolrDocument out = new SolrDocument(); for (StorableField f : doc.getFields()) { // Make sure multivalued fields are represented as lists Object existing = out.get(f.name()); if (existing == null) { SchemaField sf = schema.getFieldOrNull(f.name()); // don't return copyField targets if (sf != null && schema.isCopyFieldTarget(sf)) continue; if (sf != null && sf.multiValued()) { List<Object> vals = new ArrayList<Object>(); vals.add(f); out.setField(f.name(), vals); } else { out.setField(f.name(), f); } } else { out.addField(f.name(), f); } } return out; }
public SolrDocument readSolrDocument(DataInputInputStream dis) throws IOException { tagByte = dis.readByte(); int size = readSize(dis); SolrDocument doc = new SolrDocument(); for (int i = 0; i < size; i++) { String fieldName; Object obj = readVal(dis); // could be a field name, or a child document if (obj instanceof SolrDocument) { doc.addChildDocument((SolrDocument) obj); continue; } else { fieldName = (String) obj; } Object fieldVal = readVal(dis); doc.setField(fieldName, fieldVal); } return doc; }
@Override public void transform(SolrDocument doc, int docid, float score) { if (context != null && context.wantsScores()) { doc.setField(name, score); } }
protected void setValue(SolrDocument doc, Object val) { if (val != null) { doc.setField(name, val); } }