Search in sources :

Example 6 with SolrDocumentList

use of org.apache.solr.common.SolrDocumentList in project lucene-solr by apache.

the class IterativeMergeStrategy method merge.

public void merge(ResponseBuilder rb, ShardRequest sreq) {
    // Null pointers will occur otherwise.
    rb._responseDocs = new SolrDocumentList();
    // Turn off the second pass distributed.
    rb.onePassDistributedQuery = true;
    executorService = ExecutorUtil.newMDCAwareCachedThreadPool(new SolrjNamedThreadFactory("IterativeMergeStrategy"));
    try {
        process(rb, sreq);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        executorService.shutdownNow();
    }
}
Also used : SolrjNamedThreadFactory(org.apache.solr.common.util.SolrjNamedThreadFactory) SolrDocumentList(org.apache.solr.common.SolrDocumentList)

Example 7 with SolrDocumentList

use of org.apache.solr.common.SolrDocumentList in project lucene-solr by apache.

the class MoreLikeThisComponent method mergeSolrDocumentList.

public SolrDocumentList mergeSolrDocumentList(SolrDocumentList one, SolrDocumentList two, int maxSize, String idField) {
    List<SolrDocument> l = new ArrayList<>();
    // De-dup records sets. Shouldn't happen if indexed correctly.
    Map<String, SolrDocument> map = new HashMap<>();
    for (SolrDocument doc : one) {
        Object id = doc.getFieldValue(idField);
        assert id != null : doc.toString();
        map.put(id.toString(), doc);
    }
    for (SolrDocument doc : two) {
        map.put(doc.getFieldValue(idField).toString(), doc);
    }
    l = new ArrayList<>(map.values());
    // Comparator to sort docs based on score. null scores/docs are set to 0.
    // hmm...we are ordering by scores that are not really comparable...
    Comparator<SolrDocument> c = new Comparator<SolrDocument>() {

        public int compare(SolrDocument o1, SolrDocument o2) {
            Float f1 = getFloat(o1);
            Float f2 = getFloat(o2);
            return f2.compareTo(f1);
        }

        private Float getFloat(SolrDocument doc) {
            Float f = 0f;
            if (doc != null) {
                Object o = doc.getFieldValue("score");
                if (o != null && o instanceof Float) {
                    f = (Float) o;
                }
            }
            return f;
        }
    };
    Collections.sort(l, c);
    // Truncate list to maxSize
    if (l.size() > maxSize) {
        l = l.subList(0, maxSize);
    }
    // Create SolrDocumentList Attributes from originals
    SolrDocumentList result = new SolrDocumentList();
    result.addAll(l);
    result.setMaxScore(Math.max(one.getMaxScore(), two.getMaxScore()));
    result.setNumFound(one.getNumFound() + two.getNumFound());
    result.setStart(Math.min(one.getStart(), two.getStart()));
    return result;
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) SolrDocumentList(org.apache.solr.common.SolrDocumentList) Comparator(java.util.Comparator)

Example 8 with SolrDocumentList

use of org.apache.solr.common.SolrDocumentList in project lucene-solr by apache.

the class MoreLikeThisComponent method finishStage.

@Override
public void finishStage(ResponseBuilder rb) {
    // segment ahead of result/response.
    if (rb.stage == ResponseBuilder.STAGE_GET_FIELDS && rb.req.getParams().getBool(COMPONENT_NAME, false)) {
        Map<Object, SolrDocumentList> tempResults = new LinkedHashMap<>();
        int mltcount = rb.req.getParams().getInt(MoreLikeThisParams.DOC_COUNT, MoreLikeThisParams.DEFAULT_DOC_COUNT);
        String keyName = rb.req.getSchema().getUniqueKeyField().getName();
        for (ShardRequest sreq : rb.finished) {
            if ((sreq.purpose & ShardRequest.PURPOSE_GET_MLT_RESULTS) != 0) {
                for (ShardResponse r : sreq.responses) {
                    log.debug("ShardRequest.response.shard: " + r.getShard());
                    String key = r.getShardRequest().params.get(MoreLikeThisComponent.DIST_DOC_ID);
                    SolrDocumentList shardDocList = (SolrDocumentList) r.getSolrResponse().getResponse().get("response");
                    if (shardDocList == null) {
                        continue;
                    }
                    log.info("MLT: results added for key: " + key + " documents: " + shardDocList.toString());
                    //            if (log.isDebugEnabled()) {
                    //              for (SolrDocument doc : shardDocList) {
                    //                doc.addField("shard", "=" + r.getShard());
                    //              }
                    //            }
                    SolrDocumentList mergedDocList = tempResults.get(key);
                    if (mergedDocList == null) {
                        mergedDocList = new SolrDocumentList();
                        mergedDocList.addAll(shardDocList);
                        mergedDocList.setNumFound(shardDocList.getNumFound());
                        mergedDocList.setStart(shardDocList.getStart());
                        mergedDocList.setMaxScore(shardDocList.getMaxScore());
                    } else {
                        mergedDocList = mergeSolrDocumentList(mergedDocList, shardDocList, mltcount, keyName);
                    }
                    log.debug("Adding docs for key: " + key);
                    tempResults.put(key, mergedDocList);
                }
            }
        }
        NamedList<SolrDocumentList> list = buildMoreLikeThisNamed(tempResults, rb.resultIds);
        rb.rsp.add("moreLikeThis", list);
    }
    super.finishStage(rb);
}
Also used : SolrDocumentList(org.apache.solr.common.SolrDocumentList) LinkedHashMap(java.util.LinkedHashMap)

Example 9 with SolrDocumentList

use of org.apache.solr.common.SolrDocumentList in project lucene-solr by apache.

the class MoreLikeThisComponent method buildMoreLikeThisNamed.

/**
   * Returns NamedList based on the order of
   * resultIds.shardDoc.positionInResponse
   */
NamedList<SolrDocumentList> buildMoreLikeThisNamed(Map<Object, SolrDocumentList> allMlt, Map<Object, ShardDoc> resultIds) {
    NamedList<SolrDocumentList> result = new NamedList<>();
    TreeMap<Integer, Object> sortingMap = new TreeMap<>();
    for (Entry<Object, ShardDoc> next : resultIds.entrySet()) {
        sortingMap.put(next.getValue().positionInResponse, next.getKey());
    }
    for (Object key : sortingMap.values()) {
        SolrDocumentList sdl = allMlt.get(key);
        if (sdl == null) {
            sdl = new SolrDocumentList();
            sdl.setNumFound(0);
            sdl.setStart(0);
        }
        result.add(key.toString(), sdl);
    }
    return result;
}
Also used : NamedList(org.apache.solr.common.util.NamedList) SolrDocumentList(org.apache.solr.common.SolrDocumentList) TreeMap(java.util.TreeMap)

Example 10 with SolrDocumentList

use of org.apache.solr.common.SolrDocumentList in project lucene-solr by apache.

the class RealTimeGetComponent method mergeResponses.

private void mergeResponses(ResponseBuilder rb) {
    SolrDocumentList docList = new SolrDocumentList();
    for (ShardRequest sreq : rb.finished) {
        // can get more than one response
        for (ShardResponse srsp : sreq.responses) {
            SolrResponse sr = srsp.getSolrResponse();
            NamedList nl = sr.getResponse();
            SolrDocumentList subList = (SolrDocumentList) nl.get("response");
            docList.addAll(subList);
        }
    }
    addDocListToResponse(rb, docList);
}
Also used : NamedList(org.apache.solr.common.util.NamedList) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrResponse(org.apache.solr.client.solrj.SolrResponse)

Aggregations

SolrDocumentList (org.apache.solr.common.SolrDocumentList)263 SolrDocument (org.apache.solr.common.SolrDocument)153 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)118 SolrQuery (org.apache.solr.client.solrj.SolrQuery)83 Test (org.junit.Test)73 ArrayList (java.util.ArrayList)61 SolrServerException (org.apache.solr.client.solrj.SolrServerException)50 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)44 NamedList (org.apache.solr.common.util.NamedList)44 IOException (java.io.IOException)43 SolrParams (org.apache.solr.common.params.SolrParams)32 SolrInputDocument (org.apache.solr.common.SolrInputDocument)26 HashMap (java.util.HashMap)22 Map (java.util.Map)21 SolrClient (org.apache.solr.client.solrj.SolrClient)17 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)17 SolrException (org.apache.solr.common.SolrException)16 List (java.util.List)15 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)14 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)14