Example usage for org.apache.solr.search SolrReturnFields SolrReturnFields

List of usage examples for org.apache.solr.search SolrReturnFields SolrReturnFields

Introduction

In this page you can find the example usage for org.apache.solr.search SolrReturnFields SolrReturnFields.

Prototype

public SolrReturnFields(String[] fl, SolrQueryRequest req) 

Source Link

Usage

From source file:com.zemanta.solrcassandrabridge.CassandraBridgeComponent.java

License:Apache License

@Override
public void process(ResponseBuilder rb) throws IOException {

    // First we need to get Documents, so we get the "id" of the field
    Set<String> fields = new HashSet<String>();
    fields.add(key_field_name);//from   www.  j a v  a2s  . c  om
    SolrDocumentList docs = SolrPluginUtils.docListToSolrDocumentList(rb.getResults().docList,
            rb.req.getSearcher(), fields, null);

    // Docid_list is an array of ids to be retrieved
    List<BigInteger> docid_list = new ArrayList<BigInteger>();
    // We'll be putting things into output map in the form of {id: {field_name: value, ...}, ...}
    HashMap<BigInteger, HashMap<String, String>> output_map = new HashMap<BigInteger, HashMap<String, String>>();

    // Iterate through documents and get values of their id field
    for (SolrDocument doc : docs) {
        int docid = (Integer) doc.getFieldValue(key_field_name);
        docid_list.add(BigInteger.valueOf(docid));
        // prepare an output map for this id - empty hashmaps to be filled
        output_map.put(BigInteger.valueOf(docid), new HashMap<String, String>());
    }

    // Intersection of requested fields and bridged fields is what we will ask cassandra for
    ReturnFields returnFields = new SolrReturnFields(rb.req.getParams().getParams(CommonParams.FL), rb.req);
    Set<String> cassandra_fields;
    if (returnFields.wantsAllFields()) {
        cassandra_fields = bridged_fields;
    } else {
        cassandra_fields = returnFields.getLuceneFieldNames();
        cassandra_fields.retainAll(bridged_fields);
    }
    log.warn("Fields." + String.valueOf(cassandra_fields));

    // Get specific fields from cassandra to output_map
    cassandraConnector.getFieldsFromCassandra(docid_list, output_map, new ArrayList<String>(cassandra_fields));

    // Iterate through documents for the second time
    // Add the fields that cassandra returned
    // We could skip intermediate map, but we prefer separation of code messing with cassandra from code messing with solr structures
    for (SolrDocument doc : docs) {
        int docid = (Integer) doc.getFieldValue(key_field_name);
        for (Map.Entry<String, String> entry : output_map.get(BigInteger.valueOf(docid)).entrySet()) {
            doc.setField(entry.getKey(), entry.getValue());
        }
    }

    /// We replace the current response
    @SuppressWarnings("unchecked")
    NamedList<SolrDocumentList> vals = rb.rsp.getValues();
    int idx = vals.indexOf("response", 0);
    if (idx >= 0) {
        // I am pretty sure we always take this code path
        log.debug("Replacing DocList with SolrDocumentList " + docs.size());
        vals.setVal(idx, docs);
    } else {
        log.debug("Adding SolrDocumentList response" + docs.size());
        vals.add("response", docs);
    }

}