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();
}
}
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;
}
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);
}
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;
}
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);
}
Aggregations