/**
* Prepares Carrot2 documents for clustering.
*/
private List<Document> getDocuments(DocList docList,
Query query, final SolrQueryRequest sreq) throws IOException {
SolrHighlighter highligher = null;
SolrParams solrParams = sreq.getParams();
SolrCore core = sreq.getCore();
// Names of fields to deliver content for clustering
String urlField = solrParams.get(CarrotParams.URL_FIELD_NAME, "url");
String titleField = solrParams.get(CarrotParams.TITLE_FIELD_NAME, "title");
String snippetField = solrParams.get(CarrotParams.SNIPPET_FIELD_NAME,
titleField);
if (StringUtils.isBlank(snippetField)) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, CarrotParams.SNIPPET_FIELD_NAME
+ " must not be blank.");
}
Set<String> fieldsToLoad = Sets.newHashSet(urlField, titleField,
snippetField, idFieldName);
// Get the documents
DocIterator docsIter = docList.iterator();
boolean produceSummary = solrParams.getBool(CarrotParams.PRODUCE_SUMMARY,
false);
SolrQueryRequest req = null;
String[] snippetFieldAry = null;
if (produceSummary == true) {
highligher = core.getHighlighter();
Map args = new HashMap();
snippetFieldAry = new String[]{snippetField};
args.put(HighlightParams.FIELDS, snippetFieldAry);
args.put(HighlightParams.HIGHLIGHT, "true");
req = new LocalSolrQueryRequest(core, query.toString(), "", 0, 1, args) {
@Override
public SolrIndexSearcher getSearcher() {
return sreq.getSearcher();
}
};
}
SolrIndexSearcher searcher = sreq.getSearcher();
List<Document> result = new ArrayList<Document>(docList.size());
FieldSelector fieldSelector = new SetBasedFieldSelector(fieldsToLoad,
Collections.emptySet());
float[] scores = {1.0f};
int[] docsHolder = new int[1];
Query theQuery = query;
while (docsIter.hasNext()) {
Integer id = docsIter.next();
org.apache.lucene.document.Document doc = searcher.doc(id,
fieldSelector);
String snippet = getValue(doc, snippetField);
if (produceSummary == true) {
docsHolder[0] = id.intValue();
DocList docAsList = new DocSlice(0, 1, docsHolder, scores, 1, 1.0f);
highligher.doHighlighting(docAsList, theQuery, req, snippetFieldAry);
}
Document carrotDocument = new Document(getValue(doc, titleField),
snippet, doc.get(urlField));
carrotDocument.addField("solrId", doc.get(idFieldName));
result.add(carrotDocument);