Example usage for org.apache.solr.search DocSlice iterator

List of usage examples for org.apache.solr.search DocSlice iterator

Introduction

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

Prototype

@Override
    public DocIterator iterator() 

Source Link

Usage

From source file:com.sn.solr.plugin.common.SolrHelper.java

License:Apache License

/**
 * Constructs {@link SolrDocumentList} from the current {@link SolrQueryRequest}
 * and {@link SolrQueryResponse}./*w w  w.j a v  a 2 s .  c om*/
 * 
 * @param req {@link SolrQueryRequest}
 * @param res {@link SolrQueryResponse}
 * @return
 * @throws CorruptIndexException
 * @throws IOException
 */
public static SolrDocumentList getSolrDocList(SolrQueryRequest req, SolrQueryResponse res)
        throws CorruptIndexException, IOException {
    DocSlice slice = (DocSlice) res.getValues().get(RESP_EL_TAG);
    Set<String> returnFields = SolrHelper.getReturnFields(req);
    SolrDocumentList docList = new SolrDocumentList();
    for (DocIterator it = slice.iterator(); it.hasNext();) {
        int docId = it.nextDoc();
        Document doc = req.getSearcher().getReader().document(docId);
        SolrDocument sdoc = new SolrDocument();
        for (Fieldable f : doc.getFields()) {
            String fn = f.name();
            if (returnFields.contains(fn)) {
                sdoc.addField(fn, doc.get(fn));
            }
        }
        docList.add(sdoc);
    }
    docList.setMaxScore(slice.maxScore());
    docList.setNumFound(slice.matches());
    docList.setStart(slice.offset());
    return docList;
}

From source file:com.sn.solr.utils.common.SolrHelper.java

License:Apache License

/**
 * Constructs {@link SolrDocumentList} from the current {@link SolrQueryRequest}
 * and {@link SolrQueryResponse}./*w  w w .j ava2 s.c om*/
 * 
 * @param req {@link SolrQueryRequest}
 * @param res {@link SolrQueryResponse}
 * @return
 * @throws CorruptIndexException
 * @throws IOException
 */
public static SolrDocumentList getSolrDocList(SolrQueryRequest req, SolrQueryResponse res)
        throws CorruptIndexException, IOException {
    DocSlice slice = (DocSlice) res.getValues().get(RESP_EL_TAG);
    Set<String> returnFields = SolrHelper.getReturnFields(req);
    SolrDocumentList docList = new SolrDocumentList();
    for (DocIterator it = slice.iterator(); it.hasNext();) {
        int docId = it.nextDoc();
        Document doc = req.getSearcher().getReader().document(docId);
        SolrDocument sdoc = new SolrDocument();
        for (Fieldable f : doc.getFields()) {
            String fn = f.name();
            if (returnFields.contains(fn)) {
                sdoc.addField(fn, doc.get(fn));
            }
        }
        docList.add(sdoc);
    }
    docList.setMaxScore(slice.maxScore());
    docList.setNumFound(slice.matches());
    return docList;
}

From source file:geocluster.GeoclusterComponent.java

License:Apache License

private Map<String, SolrDocument> clusterByHashes(Map<Integer, SolrDocument> docIds,
        ArrayList<NamedList> groups) {
    int size = docIds.size(); // will be less, but no way to calc in advance?
    Map<String, SolrDocument> clusterMap = new HashMap<String, SolrDocument>(size);

    // Add all points within the core geohash to a cluster.
    for (NamedList group : groups) {
        String geohashPrefix = (String) group.get("groupValue");
        log.info("Prefix: " + geohashPrefix);

        SolrDocument cluster = null;//  w w w.  j  a va 2s.  c  om
        DocSlice docList = (DocSlice) group.get("doclist");
        DocIterator iterator = docList.iterator();
        while (iterator.hasNext()) {
            Integer docId = iterator.next();
            SolrDocument doc = docIds.get(docId);

            String geohash = (String) doc.getFieldValue(this.geohashField);
            String latlon = (String) doc.getFieldValue(this.latlonField);
            String id = (String) doc.getFieldValue(this.idField);

            // Init cluster
            if (cluster == null) {
                cluster = initCluster(doc, docId, clusterMap, geohashPrefix, docList);
                log.info("Parent: " + id + ", geohash: " + geohash + ", latlon: " + latlon);
            } else {
                addCluster(cluster, doc, docId);
                log.info("Child : " + id + ", geohash: " + geohash + ", latlon: " + latlon);
            }
        }

        updateCluster(cluster);
    }
    return clusterMap;
}