Example usage for org.apache.solr.common SolrInputField setValue

List of usage examples for org.apache.solr.common SolrInputField setValue

Introduction

In this page you can find the example usage for org.apache.solr.common SolrInputField setValue.

Prototype

public void setValue(Object v) 

Source Link

Document

Set the value for a field.

Usage

From source file:org.intermine.api.searchengine.solr.SolrObjectHandler.java

License:GNU General Public License

/**
 * Add object references to search document.
 *///from  w ww  . j  a  va  2  s .c o  m
private void addReferences(InterMineObject object, HashSet<String> references,
        HashMap<String, InterMineResultsContainer> referenceResults,
        HashMap<String, KeywordSearchFacetData> referenceFacetFields, SolrInputDocument doc)
        throws IllegalAccessException {

    // find all references and add them
    for (String reference : references) {
        InterMineResultsContainer resultsContainer = referenceResults.get(reference);
        //step through the reference results (ordered) while ref.id = obj.id
        while (resultsContainer.getIterator().hasNext()) {
            @SuppressWarnings("rawtypes")
            ResultsRow next = resultsContainer.getIterator().next();

            // It is possible that the inner loop iterator "lags behind" the
            // current object's id. See:
            // https://github.com/intermine/intermine/issues/473
            while (resultsContainer.getIterator().hasNext()
                    && ((Integer) next.get(0)).compareTo(object.getId()) == -1) {
                next = resultsContainer.getIterator().next();
            }

            //reference is not for the current object?
            if (!next.get(0).equals(object.getId())) {
                // go back one step
                if (resultsContainer.getIterator().hasPrevious()) {
                    resultsContainer.getIterator().previous();
                }

                break;
            }

            // add reference to doc
            addObjectToDocument((InterMineObject) next.get(1), null, doc);

            //check if this reference contains an attribute we need for a facet
            KeywordSearchFacetData referenceFacet = referenceFacetFields.get(reference);
            if (referenceFacet != null) {
                //handle PATH facets FIXME: UNTESTED!
                if (referenceFacet.getType() == KeywordSearchFacetType.PATH) {
                    String virtualPathField = "path_" + referenceFacet.getName().toLowerCase();
                    for (String field : referenceFacet.getFields()) {
                        if (field.startsWith(reference + ".")) {
                            String facetAttribute = field.substring(field.lastIndexOf('.') + 1);
                            Object facetValue = ((InterMineObject) next.get(1)).getFieldValue(facetAttribute);

                            if (facetValue instanceof String && !StringUtils.isBlank((String) facetValue)) {
                                SolrInputField f = doc.getField(virtualPathField);

                                if (f != null) {
                                    f.setValue(f.toString() + "/" + facetValue);
                                } else {
                                    doc.addField(virtualPathField, (String) facetValue);
                                    addFieldNameToSchema(virtualPathField, RAW_FIELD_TYPE_NAME, false, true);
                                }
                            }
                        }
                    }
                } else {
                    //SINGLE/MULTI facet
                    //add attribute to document a second time, but unstemmed
                    //and with the field name corresponding to the facet name
                    String facetAttribute = referenceFacet.getField()
                            .substring(referenceFacet.getField().lastIndexOf('.') + 1);
                    Object facetValue = ((InterMineObject) next.get(1)).getFieldValue(facetAttribute);

                    if (facetValue instanceof String && !StringUtils.isBlank((String) facetValue)) {
                        doc.addField(referenceFacet.getField(), (String) facetValue);
                        addFieldNameToSchema(referenceFacet.getField(), RAW_FIELD_TYPE_NAME, false, true);
                    }
                }
            }
        }
    }
}

From source file:org.intermine.api.searchengine.solr.SolrObjectHandler.java

License:GNU General Public License

private SolrInputField addToDocument(SolrInputDocument doc, String fieldName, String value, boolean raw) {
    if (!StringUtils.isBlank(fieldName) && !StringUtils.isBlank(value)) {
        SolrInputField f;

        if (!raw) {
            f = new SolrInputField(fieldName);
            f.setValue(value);
        } else {//from  w  ww  . j a  v a 2s  .  c o m
            f = new SolrInputField(fieldName + "_raw");
            f.setValue(value);
        }

        doc.addField(f.getName(), f.getValue());

        if (raw) {
            addFieldNameToSchema(f.getName(), RAW_FIELD_TYPE_NAME, false, true);
        } else {
            addFieldNameToSchema(f.getName(), ANALYZED_FIELD_TYPE_NAME, false, true);
        }

        return f;
    }

    return null;
}