/** * Find words for a more-like-this query former. * * @param docNum the id of the lucene document from which to find terms */ private PriorityQueue<ScoreTerm> retrieveTerms(int docNum) throws IOException { Map<String, Int> termFreqMap = new HashMap<>(); for (String fieldName : fieldNames) { final Fields vectors = ir.getTermVectors(docNum); final Terms vector; if (vectors != null) { vector = vectors.terms(fieldName); } else { vector = null; } // field does not store term vector info if (vector == null) { StoredDocument d = ir.document(docNum); StorableField[] fields = d.getFields(fieldName); for (StorableField field : fields) { final String stringValue = field.stringValue(); if (stringValue != null) { addTermFrequencies(new StringReader(stringValue), termFreqMap, fieldName); } } } else { addTermFrequencies(termFreqMap, vector); } } return createQueue(termFreqMap); }
/** * Given a {@link org.apache.solr.schema.SchemaField}, create one or more {@link * org.apache.lucene.index.IndexableField} instances * * @param field the {@link org.apache.solr.schema.SchemaField} * @param value The value to add to the field * @param boost The boost to apply * @return An array of {@link org.apache.lucene.index.IndexableField} * @see #createField(SchemaField, Object, float) * @see #isPolyField() */ public List<StorableField> createFields(SchemaField field, Object value, float boost) { StorableField f = createField(field, value, boost); if (field.hasDocValues() && f.fieldType().docValuesType() == null) { // field types that support doc values should either override createField // to return a field with doc values or extend createFields if this can't // be done in a single field instance (see StrField for example) throw new UnsupportedOperationException( "This field type does not support doc values: " + this); } return f == null ? Collections.<StorableField>emptyList() : Collections.singletonList(f); }
private static SolrInputDocument toSolrInputDocument(StoredDocument doc, IndexSchema schema) { SolrInputDocument out = new SolrInputDocument(); for (StorableField f : doc.getFields()) { String fname = f.name(); SchemaField sf = schema.getFieldOrNull(f.name()); Object val = null; if (sf != null) { if (!sf.stored() || schema.isCopyFieldTarget(sf)) continue; val = sf.getType().toObject(f); // object or external string? } else { val = f.stringValue(); if (val == null) val = f.numericValue(); if (val == null) val = f.binaryValue(); if (val == null) val = f; } // todo: how to handle targets of copy fields (including polyfield sub-fields)? out.addField(fname, val); } return out; }
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; }
@Override public void write(TextResponseWriter writer, String name, StorableField f) throws IOException { writer.writeStr(name, f.stringValue(), true); }
/** Given the stored field, return the indexed form */ public String storedToIndexed(StorableField f) { // right now, the transformation of single valued fields like SortableInt // is done when the Field is created, not at analysis time... this means // that the indexed form is the same as the stored field form. return f.stringValue(); }
/** * Convert the stored-field format to an external (string, human readable) value * * @see #toInternal */ public String toExternal(StorableField f) { // currently used in writing XML of the search result (but perhaps // a more efficient toXML(IndexableField f, Writer w) should be used // in the future. return f.stringValue(); }