Example usage for org.apache.solr.schema IndexSchema getDynamicPattern

List of usage examples for org.apache.solr.schema IndexSchema getDynamicPattern

Introduction

In this page you can find the example usage for org.apache.solr.schema IndexSchema getDynamicPattern.

Prototype

public String getDynamicPattern(String fieldName) 

Source Link

Usage

From source file:org.opencms.search.solr.CmsSolrDocument.java

License:Open Source License

/**
 * @see org.opencms.search.I_CmsSearchDocument#addSearchField(org.opencms.search.fields.CmsSearchField, java.lang.String)
 *///from  w  w  w. jav a 2 s  . c o m
public void addSearchField(CmsSearchField sfield, String value) {

    CmsSolrField field = (CmsSolrField) sfield;
    List<String> fieldsToAdd = new ArrayList<String>(Collections.singletonList(field.getName()));
    if ((field.getCopyFields() != null) && !field.getCopyFields().isEmpty()) {
        fieldsToAdd.addAll(field.getCopyFields());
    }
    IndexSchema schema = OpenCms.getSearchManager().getSolrServerConfiguration().getSolrSchema();
    for (String fieldName : fieldsToAdd) {
        try {
            List<String> splitedValues = new ArrayList<String>();
            boolean multi = false;

            try {
                SchemaField f = schema.getField(fieldName);
                if ((f != null) && (!field.getName().startsWith(CmsSearchField.FIELD_CONTENT))) {
                    multi = f.multiValued();
                }
            } catch (@SuppressWarnings("unused") SolrException e) {
                LOG.warn(Messages.get().getBundle().key(Messages.LOG_SOLR_FIELD_NOT_FOUND_1, field.toString()));
            }
            if (multi) {
                splitedValues = CmsStringUtil.splitAsList(value.toString(), "\n");
            } else {
                splitedValues.add(value);
            }
            for (String val : splitedValues) {
                if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(val)) {
                    try {
                        FieldType type = schema.getFieldType(fieldName);
                        if (type instanceof TrieDateField) {
                            val = DF.format(new Date(new Long(val).longValue()));
                        }
                    } catch (SolrException e) {
                        LOG.debug(e.getMessage(), e);
                        throw new RuntimeException(e);
                    }
                    if (fieldName.endsWith(CmsSearchField.FIELD_EXCERPT)) {
                        // TODO: make the length and the area configurable
                        val = CmsStringUtil.trimToSize(val, 1000, 50, "");
                    }
                    if (schema.hasExplicitField(fieldName) || (schema.getDynamicPattern(fieldName) != null)) {
                        m_doc.addField(fieldName, val);
                    } else {
                        m_doc.addField(fieldName, val, field.getBoost());
                    }
                }
            }
        } catch (SolrException e) {
            LOG.error(e.getMessage(), e);
        } catch (@SuppressWarnings("unused") RuntimeException e) {
            // noop
        }
    }
}